2009-10-31 70 views
8

注意:My original question是关于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(); 

我越来越对行开头的异常“结果的结果= ...”的ReaderException指出:"Unable to cast object of type 'com.google.zxing.oned.MultiFormatOneDReader' to type 'com.google.zxing.Reader'.

所以,我是什么做错了?

UPDATE:我会尝试一下建议的想法,但是在此期间,我在ZXing组中发现了这个issue

回答

1

我怀疑你只是缺少强制/使用了错误类型,尝试改变

Result result = reader.decode(image); 

线中的以下

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

或可能

MultiFormatOneDResult result = reader.decode(image); 
一个

恐怕我现在无法访问ac#编译器,所以我无法验证这一点 - 所以我很抱歉,如果我离开了大关!

2

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

11

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

+0

问题上的条形码格式有关读取条形码,而不是创建它们,那么错题,但不错的答案:) – Sam 2012-06-13 08:22:49