1、ZXing.Net介紹:
? ???????????ZXing是一個開放源碼的蚓曼,用Java實現(xiàn)的多種格式的1D/2D條碼圖像處理庫亲澡,它包含了聯(lián)系到其他語言的端口。而ZXing.Net是ZXing的端口之一纫版。這里是Zxing文件下載地址床绪,提取密碼是56jn;
2其弊、在項目的引用中添加這個dll文件的引用癞己,如下圖所示:
3、添加關鍵代碼展示:
? ? (1):這個方法是生成打印的二維碼圖片梭伐,需要修改的地方就是你自己電腦保存圖片文件的位置痹雅,在Save()方法處修改。
? ??????/// <summary>
? ? ? ? /// 生成二維碼圖片
? ? ? ? /// </summary>
? ? ? ? /// <param name="strMessage">要生成二維碼的字符串</param>
? ? ? ? /// <param name="width">二維碼圖片寬度</param>
? ? ? ? /// <param name="height">二維碼圖片高度</param>
? ? ? ? /// <returns></returns>
? ? ? ? private Bitmap GetQRCodeByZXingNet(String strMessage, Int32 width, Int32 height)
? ? ? ? {
? ? ? ? ? ? Bitmap result = null;
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? BarcodeWriter barCodeWriter = new BarcodeWriter();
? ? ? ? ? ? ? ? barCodeWriter.Format = BarcodeFormat.QR_CODE; //是個枚舉類型,可以選擇打印條碼的內(nèi)容
? ? ? ? ? ? ? ? barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
? ? ? ? ? ? ? ? barCodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
? ? ? ? ? ? ? ? barCodeWriter.Options.Height = height;
? ? ? ? ? ? ? ? barCodeWriter.Options.Width = width;
? ? ? ? ? ? ? ? barCodeWriter.Options.Margin = 0;
? ? ? ? ? ? ? ? ZXing.Common.BitMatrix bm = barCodeWriter.Encode(strMessage);
? ? ? ? ? ? ? ? result = barCodeWriter.Write(bm);
? ? ? ? ? ? ? ? string imgName = DateTime.Now.Millisecond.ToString();
? ? ? ? ? ? ? ? result.Save(@"E:\測試\" + imgName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? this.textBox2.Text = Convert.ToString(ex);
? ? ? ? ? ? }
? ? ? ? ? ? return result;
? ? ? ? }
(2):這個方法是生成打印的二維碼圖片糊识,需要修改的地方就是你自己電腦保存圖片文件的位置绩社,在Save()方法處修改。
????????/// <summary>
? ? ? ? /// 生成帶Logo的二維碼
? ? ? ? /// </summary>
? ? ? ? /// <param name="text">內(nèi)容</param>
? ? ? ? /// <param name="width">寬度</param>
? ? ? ? /// <param name="height">高度</param>
? ? ? ? private Bitmap GetQRCodeByZXingNetTwo(String strMessage, Int32 width, Int32 height)
? ? ? ? {
? ? ? ? ? ? Bitmap result = null;
? ? ? ? ? ? Bitmap bmpimg = null;
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? BarcodeWriter barCodeWriter = new BarcodeWriter();
? ? ? ? ? ? ? ? barCodeWriter.Format = BarcodeFormat.QR_CODE; //是個枚舉類型,可以選擇打印條碼的內(nèi)容
? ? ? ? ? ? ? ? barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
? ? ? ? ? ? ? ? barCodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
? ? ? ? ? ? ? ? barCodeWriter.Options.Height = height;
? ? ? ? ? ? ? ? barCodeWriter.Options.Width = width;
? ? ? ? ? ? ? ? barCodeWriter.Options.Margin = 0;
? ? ? ? ? ? ? ? ZXing.Common.BitMatrix bm = barCodeWriter.Encode(strMessage);
? ? ? ? ? ? ? ? result = barCodeWriter.Write(bm);
? ? ? ? ? ? ? ? string imgName = DateTime.Now.Hour.ToString() + DateTime.Now.Millisecond.ToString();
? ? ? ? ? ? ? ? result.Save(@"E:\測試\" + imgName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
? ? ? ? ? ? ? ? string logoPath = @"E:\VS2013Projects\BaPrint\ZXingTest\img\timg.jpg";
? ? ? ? ? ? ? ? Bitmap logo = new Bitmap(logoPath);
? ? ? ? ? ? ? ? //獲取二維碼實際尺寸(去掉二維碼兩邊空白后的實際尺寸)
? ? ? ? ? ? ? ? int[] rectangle = bm.getEnclosingRectangle();
? ? ? ? ? ? ? ? //計算插入圖片的大小和位置
? ? ? ? ? ? ? ? int middleW = Math.Min((int)(rectangle[2] / 3), logo.Width);
? ? ? ? ? ? ? ? int middleH = Math.Min((int)(rectangle[3] / 3), logo.Height);
? ? ? ? ? ? ? ? int middleL = (result.Width - middleW) / 2;
? ? ? ? ? ? ? ? int middleT = (result.Height - middleH) / 2;
? ? ? ? ? ? ? ? //Bitmap bmpimg = new Bitmap(result.Width, result.Height, PixelFormat.Format32bppArgb);
? ? ? ? ? ? ? ? bmpimg = new Bitmap(result.Width, result.Height, PixelFormat.Format32bppArgb);
? ? ? ? ? ? ? ? using (Graphics g = Graphics.FromImage(bmpimg))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
? ? ? ? ? ? ? ? ? ? g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
? ? ? ? ? ? ? ? ? ? g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
? ? ? ? ? ? ? ? ? ? g.DrawImage(result, 0, 0, width, height);
? ? ? ? ? ? ? ? ? ? //白底將二維碼插入圖片
? ? ? ? ? ? ? ? ? ? g.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
? ? ? ? ? ? ? ? ? ? g.DrawImage(logo, middleL, middleT, middleW, middleH);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? bmpimg.Save(@"E:\測試\" + imgName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? this.textBox1.Text = Convert.ToString(ex);
? ? ? ? ? ? }
? ? ? ? ? ? return bmpimg;
? ? ? ? }
????????/// <summary>
? ? ? ? /// 刪除默認對應的空白
? ? ? ? /// </summary>
? ? ? ? /// <param name="matrix"></param>
? ? ? ? /// <returns></returns>
? ? ? ? private static BitMatrix deleteWhite(BitMatrix matrix)
? ? ? ? {
? ? ? ? ? ? int[] rec = matrix.getEnclosingRectangle();
? ? ? ? ? ? int resWidth = rec[2] + 1;
? ? ? ? ? ? int resHeight = rec[3] + 1;
? ? ? ? ? ? BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
? ? ? ? ? ? resMatrix.clear();
? ? ? ? ? ? for (int i = 0; i < resWidth; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? for (int j = 0; j < resHeight; j++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (matrix[i + rec[0], j + rec[1]])
? ? ? ? ? ? ? ? ? ? ? ? resMatrix[i, j] = true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? return resMatrix;
? ? ? ? }
如果想要打印條形碼在下圖所示處選擇想要打印條碼的格式
好了,
生成條形碼和二維碼的方式多種赂苗,條碼的種類也有很多種愉耙,每一種都有其對應的應用領域,希望此文能夠幫到你