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)

 

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

using System;
using System.Drawing;
using System.Windows.Forms;
using com.google.zxing;
using COMMON = com.google.zxing.common;
private void button1_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            Image img = Image.FromFile(this.openFileDialog1.FileName);                        
            Bitmap bmap;
            try
            {
                bmap = new Bitmap(img);
            }
            catch (System.IO.IOException ioe)
            {
                MessageBox.Show(ioe.ToString());
                return;
            }
            if (bmap == null)
            {
                MessageBox.Show("Could not decode image");
                return;
            }
            LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height);
            com.google.zxing.BinaryBitmap bitmap = new com.google.zxing.BinaryBitmap(new COMMON.HybridBinarizer(source));
            Result result;
            try
            {
                result = new MultiFormatReader().decode(bitmap);
            }
            catch(ReaderException re)
            {
                MessageBox.Show(re.ToString());
                return;
            }
            
            MessageBox.Show(result.Text);        
        }
生成图片的代码:

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog sFD = new SaveFileDialog();
            sFD.DefaultExt = "*.png|*.png";
            sFD.AddExtension = true;            
            
            try
            {
                if (sFD.ShowDialog() == DialogResult.OK)
                {
                    string content = "我的电话号码:110119112118;手机型号:Blackberry 8100";
                    COMMON.ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 350, 350);
                    writeToFile(byteMatrix, System.Drawing.Imaging.ImageFormat.Png, sFD.FileName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public static void writeToFile(COMMON.ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, string file)
        {
            Bitmap bmap = toBitmap(matrix);
            bmap.Save(file, format);
        }
        public static Bitmap toBitmap(COMMON.ByteMatrix matrix)
        {
            int width = matrix.Width;
            int height = matrix.Height;
            Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));
                }
            }
            return bmap;            
        }

源代码中有两处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

 

参考资料:

Zxing调研 

使用ZXing进行二维码的生成



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值