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


免責聲明!

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



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