如何使用 ZXing C# 端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1655953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:44:08  来源:igfitidea点击:

How To Use ZXing C# Port

c#javabarcodezxing

提问by Maxim Zaslavsky

NOTE: My original questionwas 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.

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

I'm trying to use the ZXingC# 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.

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

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'.

我在以“Result result = ...”开头的行上收到一个异常 ReaderException 状态: "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 issuein the ZXing group.

更新:我将尝试建议的想法,但与此同时,我在 ZXing 组中发现了这个问题

回答by Lee

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!

恐怕我现在无法访问 ac# 编译器,所以我无法验证这一点 - 所以如果我偏离了目标,我深表歉意!

回答by Sean Owen

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.

我认为这一定是端口的缺陷,因为在原始 Java 中,这些类是强制转换兼容的。也许只是使用 MultiFormatOneDReader 作为代码中的引用类型而不是 Reader,尽管该行应该按原样很好。如果您以其他方式修复源代码并希望提交更改,请让我们(项目)知道。

回答by Benoit

This is a sample to generate a QRCode.

这是生成 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:[email protected];; ", 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

请参阅http://code.google.com/p/zxing/wiki/BarcodeContents 上的条形码格式