Required Qr code Detector sample using zxing - c#

I'm a beginer in QR code application and now I'm trying to develop application to detect QR code in the image. but i count not find any reference/sample in it.I'm using Zbar crossing zxing library and C# for this .kindly guide me in this or refer me some sample code.

We've also experienced that it is hard to find working samples for zxing.Net. After combining some of them (I can't remember where we found them) and some trial and error we found this to be satisfactory (excerpt):
[DebuggerHidden]
string findQrCodeText(com.google.zxing.Reader decoder, Bitmap bitmap)
{
var rgb = new RGBLuminanceSource(bitmap, bitmap.Width, bitmap.Height);
var hybrid = new com.google.zxing.common.HybridBinarizer(rgb);
com.google.zxing.BinaryBitmap binBitmap = new com.google.zxing.BinaryBitmap(hybrid);
string decodedString = decoder.decode(binBitmap, null).Text;
return decodedString;
}
which is called by
findQrCodeText(new com.google.zxing.qrcode.QRCodeReader(), bitmap);
We do some image voodoo around that to obtain just a little better results, but I'm afraid can't disclose that. This piece of code is the zxing part, though.
We added the DebuggerHiddenAttribute because zxing throws and swallows tons of exceptions internally, which is a real PITA when running in debug mode.

Related

How to use the functions Emgu.CV.QRCodeDetector.Detect and Emgu.CV.QRCodeDetector.Decode?

I'm using Emgu CV v4.6 with C# 6.0 on Visual Studio. I need to scan a QR code using Emgu CV but I have no clue about how to use the functions Emgu.CV.QRCodeDetector.Detect and Emgu.CV.QRCodeDetector.Decode. In the documentation, it is said that Decode() needs Quadrangle vertices found by Detect() method whereas Detect() only returns a bool (that tells wether a QR code is detected or not) and we cannot access anything in a QRCodeDetector object.
Here is my current code for context :
Image<Bgr, Byte> img1 = new Image<Bgr, Byte>(#"C:\Users\Dacapo\source\repos\qrcode_reading\qrcode_reading\qr.jpg");
IInputArray img1arr = img1;
Mat pos = new Mat(1, 2, Emgu.CV.CvEnum.DepthType.Cv32S, 1);
QRCodeDetector detector = new QRCodeDetector();
bool decoded = detector.Detect(img1arr, pos); //this returns true
And here is qr.jpg :
According to their docs at: https://www.emgu.com/wiki/files/4.6.0/document/html/Methods_T_Emgu_CV_BarcodeDetector.htm there is now a DetectAndDecode() method which can take two overloads. Hope this helps. I am currently working on a similar implementation so I'll report any progress here.

BadImage Error When Loading Tiff CCITTv4 Via SharpDX.Direct2D1.Bitmap.FromWicBitmap

I'm using SharpDX and its accompanying WIC and Direct2D wrappers to do some serverside image manipulation.
The following code works great with JPEG images and is modeled after the SharpDX docs and this Microsoft sample using D2D directly via C++.
However, I get a BadImage error when I try to load a TIFF CCITT (bitonal 1bpp) image. The BadImage error is only thrown at EndDraw, (which happens later on in the commented DrawEndorsement function), or at this line of code which I inserted to make the point at which the issue occurs more obvious:
SharpDX.Direct2D1.Bitmap bitmap = SharpDX.Direct2D1.Bitmap.FromWicBitmap(_renderTarget, _wicBitmap);
The JPEG image I pass in gets to this point and continues with no issues, but the TIFF I pass in gets to this point and causes FromWicBitmap to barf with a BadImage error.
I'm using FormatConverter to convert the TIFF/JPEG pixel formats to an appropriate and supported D2D pixel format, and the converter does change the pixel format GUID for both images, but, again, FromWicBitmap barfs only on the TIFF.
I assumed I was doing something wrong with conversion or misusing SharpDX/D2D, but when I built and ran the aforementioned Microsoft C++ D2D image viewer sample, it loaded and rendered this same TIFF file with no errors. I double checked the sample's code to verify that I was using all the same pixel formats, options, etc, and it looks like I'm doing almost exactly the same thing with SharpDX that the sample is doing with D2D directly.
Clearly Direct2D doesn't like the pixel format of the TIFF image that WIC is handing it, but why didn't the MS sample exhibit the same behavior, and why didn't FormatConverter fix it?
Am I missing something that the D2D sample code is doing?
Am I missing some trick with SharpDX?
Is this a SharpDX bug?
Thanks!
public byte[] BuildImage(byte[] image, Format saveFormat)
{
SharpDX.WIC.Bitmap _wicBitmap;
WicRenderTarget _renderTarget;
BitmapFrameDecode bSource;
FormatConverter converter = new FormatConverter(_factoryManager.WicFactory);
using (MemoryStream systemStream = new MemoryStream(image))
using (WICStream wicStream = new WICStream(_factoryManager.WicFactory, systemStream))
{
BitmapDecoder inDecoder = new BitmapDecoder(_factoryManager.WicFactory, wicStream, DecodeOptions.CacheOnLoad);
if (inDecoder.FrameCount > 0)
{
bSource = inDecoder.GetFrame(0);
converter.Initialize(bSource, SharpDX.WIC.PixelFormat.Format32bppPRGBA, BitmapDitherType.Solid, null, 0.0f, BitmapPaletteType.MedianCut);
_imageWidth = bSource.Size.Width;
_imageHeight = bSource.Size.Height;
}
else
{
throw new Exception("No frames found!");
}
}
_wicBitmap = new SharpDX.WIC.Bitmap(
_factoryManager.WicFactory,
converter,
BitmapCreateCacheOption.CacheOnDemand
);
_renderTarget = new WicRenderTarget(_factoryManager.D2DFactory, _wicBitmap, new RenderTargetProperties());
SharpDX.Direct2D1.Bitmap bitmap = SharpDX.Direct2D1.Bitmap.FromWicBitmap(_renderTarget, _wicBitmap);
//DrawEndorsement(_renderTarget);
_renderTarget.Dispose();
bSource.Dispose();
converter.Dispose();
return SaveImage(saveFormat, _wicBitmap);
}
As xoofx pointed out, turns out that this was caused by my disposing of the WIC/MemoryStreams underlying the FormatConverter while it was still in use.
This was causing JPEGs to be corrupted on write, and weirdly causing the TIFFs to fail even before that.
Extended the using scope accordingly and that fixed it.

Decoding Barcode using Zxing library works on 1 tablet but does not work on another tablet

I have written a windows store app in XAML & C# to read image from tablet's webcam and decode the barcode using Zxing's lbrary. The code is working fine on a given tablet having an i5 processor while it fails to run on an actual tablet with 2MP camera and "Intel Baytrail Quad-Core" processor.
Any ideas on why this could happen?
Please let me know if you need to see my code for this issue ad I will share.
I am wondering how can the same code work on 1 tablet while fail on another tablet.
Thanks in advance for any help provided.
EDIT
Code used to scan the barcode and read as below - The last if/else block is what I get to. No exception raised :(
string barcodeData = string.Empty;
using (var imageStream = new InMemoryRandomAccessStream())
{
processingImage = true;
var encodingProperties = new ImageEncodingProperties();
encodingProperties.Subtype = "Jpeg";
encodingProperties.Width = 400;
encodingProperties.Height = 400;
await captureMgr.CapturePhotoToStreamAsync(encodingProperties, imageStream);
await imageStream.FlushAsync();
imageStream.Seek(0);
var bitmap = new WriteableBitmap(400, 400);
bitmap.SetSource(imageStream);
preview1.Source = bitmap; //preview1 is an Image control to display the captured image
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(imageStream);
imageStream.Seek(0);
var bitmapDecoder = await BitmapDecoder.CreateAsync(BitmapDecoder.JpegDecoderId, imageStream);
var data = await bitmapDecoder.GetPixelDataAsync(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Straight,
new BitmapTransform(),
ExifOrientationMode.IgnoreExifOrientation,
ColorManagementMode.DoNotColorManage
);
if (data != null)
{
BarcodeReader barcodeReader = new BarcodeReader();
var result = barcodeReader.Decode(
data.DetachPixelData(),
(int)bitmapDecoder.PixelWidth,
(int)bitmapDecoder.PixelHeight,
ZXing.RGBLuminanceSource.BitmapFormat.BGR32
);
if (result != null)
{
//Barcode found
}
else
//No data found.
}
}
I guess you are using ZXing.NET library.
Have you ever considered moving to another barcode scanner library?
Accessing the "ISSUES" section in ZXing.NET Library, you can see that there's a lot of bugs still opened for Windows Phone (and should be Window Store also).
http://zxingnet.codeplex.com/workitem/list/basic
One of it called my attention. Check out this comment:
While the WP samples all target Silverlight, you must not forget that the new WP8.1 base is WinRT - so I suggest you use the WinRT sample as a base.
I tried to do the same, but truth to be told, ZXing lacks a lot ATM for WinRT Universal Apps - it's slow, unreliable, and barely ever recognizes a thing.
http://zxingnet.codeplex.com/workitem/13311
I don't know how reliable this is, but the last time the project was updated was on April 7th!!!!
You should really consider changing you library!
Hi,
I made a lib for WinRT using ZXing & Imaging SDK.
It works well (but does not include any additional focus feature).
https://github.com/stepheUp/VideoScanZXing4WP81
There is a lib and a sample app that you can try.
It works for barcodes and QRCode (barcode by default but just change the optional parameter in the scan function code to use QRCode)
Hope it helps,
Stéphanie

Zxing qr code decode can't get it work

I've searched all the forum and I cannot find the answer (and this is my first post, so sorry for possible errors).
I've downloaded Zxing pack and I would like to get the qr code decoder work (in c#, a WPF application). The problem is that I get no error while compiling, and the application goes well (I take the input stream from kinect rgb camera) but it seems it does nothing.
I report some code (probably I've made some error...):
RGBLuminanceSource ls = new RGBLuminanceSource(bit, frame.Width,frame.Height);
Result result = new QRCodeReader().decode(new BinaryBitmap(new HybridBinarizer(ls)));
if (result != null)
Console.WriteLine("yahoo!");
else
Console.WriteLine("oh no");
with frame that is the frame captured by the kinect, and bit is the byte[] array that RGBLuminanceSource want as input, and I've made it by this:
byte[] bit= new byte[frame.PixelDataLength];
colorFrame.CopyPixelDataTo(bit);
What pass is that, when I put the paper with the qr code in front of the camera, it always return me the message "oh no", and I cannot figure out what I'm doing wrong.
As far as I know the image which you get from the kinect camera is flipped.
You have to flip it back before decoding.
You can find some more detail here:
http://zxingnet.codeplex.com/discussions/401772
In case someone stumbles upon this question, There are few things you can check.
Download the QRCodeDecoder sample from ZXing git and have it at ready
Save your Bitmap file to a folder location.
Check whether the saved Image decodes correctly from the QRCodeDecoder.
If you are using this for a QR Code scanner via the webcam or other video source, check whether you are converting the bitmap image in the correct pixel format.
hope this helps

Getting MP4 File Duration with DirectShow

I need to get the duration of an mp4 file, preferably as a double in seconds. I was using DirectShow (see code below), but it keeps throwing a particularly unhelpful error. I'm wondering if someone has an easy solution to this. (Seriously, who knew that getting that information would be so difficult)
public static void getDuration(string moviePath)
{
FilgraphManager m_objFilterGraph = null;
m_objFilterGraph = new FilgraphManager();
m_objFilterGraph.RenderFile(moviePath);
IMediaPosition m_objMediaPosition = null;
m_objMediaPosition = m_objFilterGraph as IMediaPosition;
Console.WriteLine(m_objMediaPosition.Duration);
}
Whenever I run this code, I get the error: "Exception from HRESULT: 0x80040265"
I also tried using this: Getting length of video
but it doesn't work either because I don't think that it works on MP4 files.
Seriously, I feel like there has to be a much easier way to do this.
Note: I would prefer to avoid using exe's like ffmpeg and then parsing the output to get the information.
You are approaching the problem correctly. You need to build a good pipeline starting from source .MP4 file and up to video and audio renderers. Then IMediaPosition.Duration will get you what you want. Currently you are getting VFW_E_UNSUPPORTED_STREAM because you cannot build the pipeline.
Note that there is no good support for MPEG-4 in DirectShow in clean Windows, you need a third party parser installed to add missing blocks. This is the likely cause of your problem. There are good Free DirectShow Mpeg-4 Filters available to fill this gap.
The code sample under the link Getting length of video is basically valid too, however it uses deprecated component which in additional make additional assumptions onto the media file in question. Provided that there is support for .MP4 in the system, IMediaPosition.Duration is to give you what you look for.
You can use get_Duration() from IMediaPosition interface.
This return a double value with the video duration in seconds.
Double Lenght;
m_FilterGraph = new FilterGraph()
//Configure the FilterGraph()
m_mediaPosition = m_FilterGraph as IMediaPosition;
m_mediaPosition.get_Duration(out Length);
Using Windows Media Player Component also, we can get the duration of the video.
I hope that following code snippet may help you guys :
using WMPLib;
// ...
var player = new WindowsMediaPlayer();
var clip = player.newMedia(filePath);
Console.WriteLine(TimeSpan.FromSeconds(clip.duration));
and don't forget to add the reference of wmp.dll which will be
present in System32 folder.

Categories

Resources