如何使用ZXing C#端口
注意: 我最初的问题是关于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 = …”开头的行上遇到exceptionReaderException指出: "Unable to cast object of type 'com.google.zxing.oned.MultiFormatOneDReader' to type 'com.google.zxing.Reader'.
强制转换"Unable to cast object of type 'com.google.zxing.oned.MultiFormatOneDReader' to type 'com.google.zxing.Reader'.
那么,我做错了什么?
更新:我将尝试建议的想法,但与此同时,我在ZXing小组中发现了这个问题 。
这是生成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);
请参阅http://code.google.com/p/zxing/wiki/BarcodeContents上的条形码格式
我认为这必定是端口的缺陷,因为在原始Java中这些类是强制兼容的。 也许只是使用MultiFormatOneDReader作为代码中的引用类型而不是Reader,尽管该行应该是原样的。 如果您以其他方式修复源并想要提交更改,请告知我们(项目)。
我怀疑你只是错过了一个演员/正在使用错误的类型,尝试改变
Result result = reader.decode(image);
列入以下之一
Result result = (Result)reader.decode(image);
或者可能
MultiFormatOneDResult result = reader.decode(image);
我担心我现在无法访问ac#编译器,所以我无法对此进行validation – 所以如果我离开标记,我会道歉!