2009-10-31 16 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