Last information:
After reminding @Michael
I to successfully capture and decode the QR code using
zxingnet-88246 \ trunk \ Clients \ WindowsFormsDemo
EmguCVDemo (successful decoding of a QR code in a camera frame today)
I will try to compare the difference between the demo code and my code. Thanks, Michael, so happy to successfully decode it.
p / s: these example codes work with net4.0 zxing.dll but not net4.5 zxing.dll
Old questions:
Using Zxing.Net, I can decode the original image of the QR code encoded by ZXing.Net.
But when I get the image from Emgu.CV capture , it cannot be decoded by ZXing.Net even I | try cropping, resizing and adding canvas size.
But the magic is the Android QR code scanner . Scan the QR code even directly from the camera. I tried to parse the Android source code, but I did not find anything special. I wonder if the Android version uses one autofocus function of the camera?
Below is my code:
DecoderResult decoderResult;
Bitmap bitmap = new Bitmap(@"C:\testfunny678.bmp");
LuminanceSource source = new BitmapLuminanceSource(bitmap);
BinaryBitmap binBitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
try
{
DetectorResult detectorResult = new Detector(binBitmap.BlackMatrix).detect(null);
Result result = decoder.decode(binBitmap);
if (result.Text != null)
{
MessageBox.Show("result is: " + result.Text);
}
else
{
MessageBox.Show("bitmap is null");
}
}
catch (Exception e)
{
}
My code can decode this

Camera capture like this

After cropping, resizing and adding the canvas, it will look like this (testfunny678.bmp)

canvas QR-, QR- , , Android- QR-.
HybridBinarizer QR-.
LuminanceSource source = new RGBLuminanceSource(GetRGBValues(bitmap), bitmap.Width, bitmap.Height);
BinaryBitmap binBitmap = new BinaryBitmap(new HybridBinarizer(source));
- QR- ( QR-), QR-, . sharpen , ZXing QR-.
, QR- ( (23.5, 76.5), (23.5, 23.5), (75, 24.5))
THIRD QR
public string Detect(Bitmap bitmap)
{
try
{
ZXing.LuminanceSource source = new RGBLuminanceSource(GetRGBValues(bitmap), bitmap.Width, bitmap.Height);
var binarizer = new HybridBinarizer(source);
var binBitmap = new BinaryBitmap(binarizer);
BitMatrix bm = binBitmap.BlackMatrix;
Detector detector = new Detector(bm);
DetectorResult result = detector.detect();
string retStr = "Found at points ";
foreach (ResultPoint point in result.Points)
{
retStr += point.ToString() + ", ";
}
return retStr;
}
catch
{
return "Failed to detect QR code.";
}
}
private byte[] GetRGBValues(Bitmap bmp)
{
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = bmpData.Stride * bmp.Height;
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
bmp.UnlockBits(bmpData);
return rgbValues;
}
DetectorResult.Points Detect() Detect() DetectorResult , fail , Result.
public DetectorResult Detect(Bitmap bitmap)
{
try
{
ZXing.LuminanceSource source = new RGBLuminanceSource(GetRGBValues(bitmap), bitmap.Width, bitmap.Height);
var binarizer = new HybridBinarizer(source);
var binBitmap = new BinaryBitmap(binarizer);
BitMatrix bm = binBitmap.BlackMatrix;
Detector detector = new Detector(bm);
DetectorResult result = detector.detect();
return result;
}
catch
{
return null;
}
}
void Decode3()
{
DecoderResult decoderResult;
Bitmap bitmap = new Bitmap(@"C:\testfunny678.bmp");
LuminanceSource source = new BitmapLuminanceSource(bitmap);
BinaryBitmap binBitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
ResultPoint[] points;
ZXing.QrCode.Internal.Decoder decoder = new ZXing.QrCode.Internal.Decoder();
try
{
DetectorResult detectorResult = new Detector(binBitmap.BlackMatrix).detect(null);
decoderResult = decoder.decode(detectorResult.Bits,null);
points = detectorResult.Points;
Result result = new Result(decoderResult.Text, decoderResult.RawBytes, points, BarcodeFormat.QR_CODE);
if (result.Text != null)
{
MessageBox.Show("result is: " + result.Text);
}
else
{
MessageBox.Show("bitmap is null");
}
}
catch (Exception e)
{
}
}
2- QR QR ZXing.NET QR- , .