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)

 

贴一下代码,这是识别的:

  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.         }  

 

 

 

生成图片的代码:

  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进行二维码的生成

posted @ 2013-06-09 10:59  郑文亮  阅读(47366)  评论(2编辑  收藏  举报