C#-此文是利用ZXing.Net的dll文件在WinForm中生成條形碼、二維碼測試案例,提供學習分享使用。

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; //是個枚舉類型,可以選擇打印條碼的內容

? ? ? ? ? ? ? ? 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">內容</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; //是個枚舉類型,可以選擇打印條碼的內容

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

? ? ? ? }


如果想要打印條形碼在下圖所示處選擇想要打印條碼的格式

好了,

生成條形碼和二維碼的方式多種,條碼的種類也有很多種,每一種都有其對應的應用領域,希望此文能夠幫到你

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容