如何使用ZXing C#端口

8
<注意: 我的原始问题 是关于ZXing C#端口是否可靠的,但在这里,我正在尝试弄清楚如何使用它。因此,它们不是重复的。

我正在尝试使用ZXing C#模块,但遇到了问题。有没有使用过ZXing的人知道如何正确地使用它?不幸的是,C#文档非常少。

我的当前代码是:

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();

我在以“Result result = ...”开头的那一行遇到了异常。ReaderException指出:"无法将类型为 'com.google.zxing.oned.MultiFormatOneDReader' 的对象强制转换为类型 'com.google.zxing.Reader'。

那么,我做错了什么?

更新: 我将尝试建议的方法,但同时,我在ZXing组中找到了这个issue

3个回答

11

这是一个生成QR码的示例。

        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);

请访问http://code.google.com/p/zxing/wiki/BarcodeContents了解条形码格式。


这个问题是关于读取条形码,而不是创建条形码,所以主题不对,但回答很好 :) - Sam

2

我认为这可能是端口中的缺陷,因为在原始的Java中,这些类是可以进行强制类型转换的。也许只需在代码中使用MultiFormatOneDReader作为参考类型而不是Reader,尽管本来的代码行应该没问题。如果您修复了源代码并想提交更改,请告知我们(项目组)。


1

我猜你可能只是缺少了一个强制类型转换/使用了错误的数据类型,尝试更改一下

Result result = reader.decode(image);

将行输入其中之一

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

或者可能

MultiFormatOneDResult result = reader.decode(image);

很抱歉,我现在没有访问C#编译器的权限,所以无法验证这个问题 - 如果我完全错了,请谅解!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接