8

NOTE: My original question was about whether the ZXing C# port is reliable, but here, I'm trying to figure out how to use it. Thus, they are not duplicates.

I'm trying to use the ZXing C# module, but I'm having trouble. Does anyone who has used ZXing before know how to do it correctly? Unfortunately, the C# documentation is quite small.

My current code is:

using com.google.zxing;
using com.google.zxing.client.j2se;
using com.google.zxing.common;

//...

Reader reader = new MultiFormatReader();
MonochromeBitmapSource image = new BufferedImageMonochromeBitmapSource(new Bitmap(Image.FromFile("barcode.jpg")),false);

Result result = reader.decode(image);
string text = result.getText();
sbyte[] rawbytes = result.getRawBytes();
BarcodeFormat format = result.getBarcodeFormat();
ResultPoint[] points = result.getResultPoints();
Console.WriteLine("barcode text: {0}", text);
Console.WriteLine("raw bytes: {0}", rawbytes);
Console.WriteLine("format: {0}", format);
Console.ReadLine();

I'm getting an exception on the line that starts with "Result result = ..." The ReaderException states: "Unable to cast object of type 'com.google.zxing.oned.MultiFormatOneDReader' to type 'com.google.zxing.Reader'.

So, what am I doing wrong?

UPDATE: I'm going to try the suggested ideas, but in the meantime, I found this issue in the ZXing group.

Community
  • 1
  • 1
Maxim Zaslavsky
  • 17,787
  • 30
  • 107
  • 173

3 Answers3

11

This is a sample to generate a QRCode.

        QRCodeWriter writer = new QRCodeWriter();
        com.google.zxing.common.ByteMatrix matrix;

        int size = 180;
        matrix = writer.encode("MECARD:N:Owen,Sean;ADR:76 9th Avenue, 4th Floor, New York, NY 10011;TEL:+12125551212;EMAIL:srowen@example.com;; ", BarcodeFormat.QR_CODE, size, size, null);


        Bitmap img = new Bitmap(size, size);
        Color Color = Color.FromArgb(0, 0, 0);

        for (int y = 0; y < matrix.Height; ++y)
        {
            for (int x = 0; x < matrix.Width; ++x)
            {
                Color pixelColor = img.GetPixel(x, y);

                //Find the colour of the dot
                if (matrix.get_Renamed(x, y) == -1)
                {
                    img.SetPixel(x, y, Color.White );
                }
                else
                {
                    img.SetPixel(x, y, Color.Black);
                }
            }
        }


        img.Save(@"c:\test.bmp",ImageFormat.Bmp);

See the Barcode format at http://code.google.com/p/zxing/wiki/BarcodeContents

Benoit
  • 119
  • 1
  • 2
  • The Question is about reading barcodes, not creating them, so wrong topic, but nice answer :) – Sam Jun 13 '12 at 08:22
2

I think that must be a deficiency in the port, since in the original Java these classes are cast-compatible. Perhaps just use MultiFormatOneDReader as the reference type in the code rather than Reader, though the line should have been fine as-is. If you otherwise fix the source and want to submit the change let us (the project) know.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
1

I suspect you are just missing a cast/are using the wrong type, try changing

Result result = reader.decode(image);

line in to one of the following

Result result = (Result)reader.decode(image);

or possibly

MultiFormatOneDResult result = reader.decode(image);

I'm afraid I don't have access to a c# compiler right now, so I can't verify this - so I apologise if I'm way off the mark!

Lee
  • 878
  • 7
  • 16