ZXing barcode scanner for .NET
I finally made a total restart as is was not working as expected.
I implemented the following algorythm: If the decoder doesn't read the barcode, split the image in 4 and restart.
It worked pretty well after and I think this is how the website you mentionned is working. Too bad it doesn't use this method from scratch.
note: this code is far from perfect, makes a lot of assumptions, if you copy it and use it as is, it may crash if your image is not in the same format that the one provided by the OP
internal class Program
{
private static readonly List<BarcodeFormat> Fmts = new List<BarcodeFormat> { BarcodeFormat.All_1D };
static void Main(string[] args)
{
Bitmap originalBitmap = new Bitmap(@"C:\Users\me\Desktop\6.jpg");
Bitmap img = CropImage(originalBitmap, new Rectangle(0 , 0, originalBitmap.Width, originalBitmap.Height));
int width = img.Width;
int heigth = img.Height;
int nbOfFrames = 1;
bool found = false;
while (!found && width > 10 && heigth > 10)
{
if (DecodeImg(img))
{
break;
}
nbOfFrames *= 4;
width /= 2;
heigth /= 2;
var x = 0;
var y = 0;
for (int i = 0; i < nbOfFrames; i++)
{
img.Dispose();
img = new Bitmap(CropImage(originalBitmap, new Rectangle(x, y, width, heigth)));
if (DecodeImg(img))
{
found = true;
}
x += width;
if (x < originalBitmap.Width)
{
continue;
}
x = 0;
y += heigth;
if (y >= originalBitmap.Height)
{
y = 0;
}
}
}
}
public static Bitmap CropImage(Image img, Rectangle cropArea)
{
Bitmap bmpImage = new Bitmap(img);
return bmpImage.Clone(cropArea, PixelFormat.Format24bppRgb);
}
public static bool DecodeImg(Bitmap img)
{
BarcodeReader reader = new BarcodeReader
{
AutoRotate = true,
TryInverted = true,
Options =
{
PossibleFormats = Fmts,
TryHarder = true,
ReturnCodabarStartEnd = true,
PureBarcode = false
}
};
Result result = reader.Decode(img);
if (result != null)
{
Console.WriteLine(result.BarcodeFormat);
Console.WriteLine(result.Text);
return true;
}
Console.Out.WriteLine("Raté");
return false;
}
}
Giorgi Nakeuri
Updated on September 02, 2022Comments
-
Giorgi Nakeuri over 1 year
I am trying to use this library https://zxingnet.codeplex.com/ . Images are successfully decoded on this site https://zxing.org/w/decode.jspx, but not in my code.
Here are two ways that I have tried:
BarcodeReader reader = new BarcodeReader { AutoRotate = true, TryHarder = true, TryInverted = true, PossibleFormats = fmts }; Result result = reader.Decode(new Bitmap(@"D:\\6.jpg"));
and:
public static byte[] ImageToByte(Bitmap img) { ImageConverter converter = new ImageConverter(); return (byte[])converter.ConvertTo(img, typeof(byte[])); }
MultiFormatReader m_BarcodeReader = new MultiFormatReader(); var hints = new Dictionary<DecodeHintType, object>(); var fmts = new List<BarcodeFormat>(); fmts.Add(BarcodeFormat.EAN_13); hints.Add(DecodeHintType.TRY_HARDER_WITHOUT_ROTATION, false); hints.Add(DecodeHintType.POSSIBLE_FORMATS, fmts); Result rawResult; Bitmap image = new Bitmap(@"D:\\6.jpg"); RGBLuminanceSource r = new RGBLuminanceSource(ImageToByte(image), image.Width, image.Height); GlobalHistogramBinarizer x = new HybridBinarizer(r); BinaryBitmap bitmap = new BinaryBitmap(x); try { rawResult = m_BarcodeReader.decode(bitmap, hints); if (rawResult != null) { return rawResult.Text; } } catch (ReaderException e) { }
In both cases the result of decoding is
null
. What am I doing wrong here? Here is the sample image: -
Giorgi Nakeuri about 8 yearsHmmm, it results in null on my machine. Can you please upload the solution somewhere?
-
Thomas Ayoub about 8 years@GiorgiNakeuri I will, just give me an hour
-
Giorgi Nakeuri about 8 yearsmay be I am using some incorrect library version? I have this nuget package installed
<package id="ZXing.Net" version="0.14.0.1" targetFramework="net45" />
. Can you check yours? -
Giorgi Nakeuri about 8 yearsIn case someone will search for this, I gave up to make this working and went with another one freebarcode.codeplex.com . Last is working perfectly.
-
Thomas Ayoub about 8 years@GiorgiNakeuri I was not using the correct file in the previous answer, sorry. Now it works pretty well
-
Giorgi Nakeuri about 8 yearsthank you very much. Now it works, but can not decode some files, while another sdk(that I provided earlier) is working fine.
-
Jaydeep Karena almost 6 years@ThomasAyoub sir plz upload solution somewhere. Thanks.
-
Thomas Ayoub almost 6 years@JaydeepKarena the code I provided is one solution. I'm not sure to understand what you mean.
-
Thomas Ayoub almost 6 years@JaydeepKarena then you can ask a new question and explain what's wrong with your implementation. I'll be glad to take a look at it.