C#生成二维条码zxing类库使用

一、简介   
ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码。
该项目可实现的条形码编码和解码。我们目前支持以下格式:
UPC-A,UPC-E
EAN-8,EAN-13
39码
93码
代码128
创新及科技基金
库德巴
RSS-14(所有的变体)
RSS扩展(大多数变体)
QR码
数据矩阵
阿兹台克人('测试版'质量)
PDF 417('阿尔法'的质量)
Zxing库的主要部分支持以下几个功能:核心代码的使用、适用于J2SE客户端的版本、适用于Android客户端的版本(即BarcodeScanner)、Android的集成(通过Intent支持和BarcodeScanner的集成)等      
 
二、使用
到谷歌code下载相应的代码
1.下载zxing最新的包
到zxing的主页: http://code.google.com/p/zxing/
找到其中的CSharp文件夹,在vs中打开并编译,将obj下debug中的zxing.dll复制并粘帖到你的项目中的bin文件目录下,

右击添加项目引用。将zxing.dll引用到项目中,就可以在需要的地方使用了。
源代码中有两处UTF-8的问题,会导致中文出现乱码(编译.dll之前修改)
其一: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            
 
生成代码://引用

 using com.google.zxing.qrcode;
 using com.google.zxing;
 using com.google.zxing.common;
 using ByteMatrix = com.google.zxing.common.ByteMatrix;
 using EAN13Writer = com.google.zxing.oned.EAN13Writer;
 using EAN8Writer = com.google.zxing.oned.EAN8Writer;
 using MultiFormatWriter = com.google.zxing.MultiFormatWriter;


 方法:
        string content = "二维码信息";
        ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300);
        Bitmap bitmap = toBitmap(byteMatrix);
        pictureBox1.Image = bitmap;
        SaveFileDialog sFD = new SaveFileDialog();
        sFD.Filter = "保存图片(*.png) |*.png|所有文件(*.*) |*.*";
        sFD.DefaultExt = "*.png|*.png";
        sFD.AddExtension = true;
        if (sFD.ShowDialog() == DialogResult.OK)
        {
            if (sFD.FileName != "")
            {
                writeToFile(byteMatrix, System.Drawing.Imaging.ImageFormat.Png, sFD.FileName);
            }

        }


 解析:
    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 bitmap1 = new com.google.zxing.BinaryBitmap(new HybridBinarizer(source));
        Result result;
        try
        {
            result = new MultiFormatReader().decode(bitmap1);
        }
        catch (ReaderException re)
        {
            MessageBox.Show(re.ToString());
            return;
        }

        MessageBox.Show(result.Text); 

    public static void writeToFile(ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, string file)
    {
        Bitmap bmap = toBitmap(matrix);
        bmap.Save(file, format);
    }

    public static Bitmap toBitmap(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;
    }

posted @ 2015-03-26 16:31  蓝色妖精  阅读(6141)  评论(1编辑  收藏  举报