如何使用ZXing C#端口

发布于 2024-08-10 10:46:35 字数 1390 浏览 8 评论 0原文

注意:我最初的问题是关于 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 组中。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ˉ厌 2024-08-17 10:46:35

这是生成 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);

请访问 http://code.google.com/p/zxing/wiki/BarcodeContents 查看条形码格式

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:[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

温柔嚣张 2024-08-17 10:46:35

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

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.

泪是无色的血 2024-08-17 10:46:35

我怀疑您只是缺少强制转换/使用了错误的类型,请尝试将

Result result = reader.decode(image);

行更改为以下之一

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

,或者可能

MultiFormatOneDResult result = reader.decode(image);

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

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文