Google開源項目二維碼讀取與生成工具ZXing


上周五,閑逛Google Code的時候,查看了一下Google參與的開源項目,

在code.google.com上點擊"開源計划"然后點擊使用 Google 的代碼

即可查看Google所有的開源項目列表

 

翻了幾頁,發現一個zxing以前沒聽說過(孤陋寡聞了)

原來是個二維碼的識別程序庫,剛好前幾個月還困惑火車票上的防偽碼是怎么做的(才知道那種碼叫QRcode),

於是把代碼下載了下來,順便說一下,這個庫的示例數據是圖片,所以體積較大,大概130M,

我用tortoise SVN, 由於網速太慢,下了三個小時,

順便在網上也查了查相關資料,編譯了java版本的試了一下

效果不錯,可以使用,於是又把其.net版的工程編譯了一下,是一個dll,debug版的212K

參照javase中的GUIRunner.java代碼(很短的幾句代碼),在C#中實現了一個二維碼的讀取識別(QRCode)

 

參照javase中的MatrixToImageWriter.java(代碼也很短),實現了二維碼圖片的生成(QRCode)

 

貼一下代碼,這是識別的:

[c-sharp] view plain copy print ?
  1. using System;  
  2. using System.Drawing;  
  3. using System.Windows.Forms;  
  4. using com.google.zxing;  
  5. using COMMON = com.google.zxing.common;  
  6. private void button1_Click(object sender, EventArgs e)  
  7.         {  
  8.             if (this.openFileDialog1.ShowDialog() != DialogResult.OK)  
  9.             {  
  10.                 return;  
  11.             }  
  12.             Image img = Image.FromFile(this.openFileDialog1.FileName);                          
  13.             Bitmap bmap;  
  14.             try  
  15.             {  
  16.                 bmap = new Bitmap(img);  
  17.             }  
  18.             catch (System.IO.IOException ioe)  
  19.             {  
  20.                 MessageBox.Show(ioe.ToString());  
  21.                 return;  
  22.             }  
  23.             if (bmap == null)  
  24.             {  
  25.                 MessageBox.Show("Could not decode image");  
  26.                 return;  
  27.             }  
  28.             LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height);  
  29.             com.google.zxing.BinaryBitmap bitmap = new com.google.zxing.BinaryBitmap(new COMMON.HybridBinarizer(source));  
  30.             Result result;  
  31.             try  
  32.             {  
  33.                 result = new MultiFormatReader().decode(bitmap);  
  34.             }  
  35.             catch(ReaderException re)  
  36.             {  
  37.                 MessageBox.Show(re.ToString());  
  38.                 return;  
  39.             }  
  40.               
  41.             MessageBox.Show(result.Text);          
  42.         }  

 

 

 

生成圖片的代碼:

[c-sharp] view plain copy print ?
  1. private void button2_Click(object sender, EventArgs e)  
  2. {  
  3.     SaveFileDialog sFD = new SaveFileDialog();  
  4.     sFD.DefaultExt = "*.png|*.png";  
  5.     sFD.AddExtension = true;              
  6.       
  7.     try  
  8.     {  
  9.         if (sFD.ShowDialog() == DialogResult.OK)  
  10.         {  
  11.             string content = "我的電話號碼:110119112118;手機型號:Blackberry 8100";  
  12.             COMMON.ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 350, 350);  
  13.             writeToFile(byteMatrix, System.Drawing.Imaging.ImageFormat.Png, sFD.FileName);  
  14.         }  
  15.     }  
  16.     catch (Exception ex)  
  17.     {  
  18.         MessageBox.Show(ex.Message);  
  19.     }  
  20. }  
  21. public static void writeToFile(COMMON.ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, string file)  
  22. {  
  23.     Bitmap bmap = toBitmap(matrix);  
  24.     bmap.Save(file, format);  
  25. }  
  26. public static Bitmap toBitmap(COMMON.ByteMatrix matrix)  
  27. {  
  28.     int width = matrix.Width;  
  29.     int height = matrix.Height;  
  30.     Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);  
  31.     for (int x = 0; x < width; x++)  
  32.     {  
  33.         for (int y = 0; y < height; y++)  
  34.         {  
  35.             bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));  
  36.         }  
  37.     }  
  38.     return bmap;              
  39. }  

 

 

 

源代碼中有兩處UTF-8的問題,會導致亂碼,

其一:com.google.zxing.qrcode.encoder.encoder類中的

internal const System.String DEFAULT_BYTE_MODE_ENCODING = "ISO-8859-1";

此處,將ISO-8859-1改為UTF-8

其二:com.google.zxing.qrcode.decoder.DecodedBitStreamParser類的成員

private const System.String UTF8 = "UTF8";

應將UTF8改為UTF-8

 

手機像素,理論上30萬的就可以,不過可能得有自動對焦的才行,

我的Blackberry 8100,130萬的像素,拍出來的是無法識別,有rim的示例程序,

但是j2me的還沒編譯成功,所以還沒辦法嘗試,提示找不到javax的一堆symbol(可能是wtk2.1版本有點舊)

 

生成的QRCode圖:

這張圖片里面的信息如下:

 

qrCodeInfo

 

 

=======================分割線2012-06-27分割線======================================

很抱歉不能給留言的同學一一回復,參考代碼和編譯好的dll我放到github上了,

需要的請移步下載  https://github.com/F-Sidney/ZXingGUI

 

參考資料:

Zxing調研 

使用ZXing進行二維碼的生成


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM