.NET使用ZXing.NET產生中間帶圖片的二維碼

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   使用   

很久之前就有寫這樣的代碼了,只是一直沒記錄下來,偶然想寫成部落格。

把之前的代碼封裝成函數,以方便理解以及調用。

基於開源的 ZXing.NET 組件,代碼如下:

先添加對ZXing.NET的引用,然後添加相應的命名空間:

using System;using System.Collections.Generic;using System.Drawing;using ZXing;using ZXing.Common;using ZXing.QrCode;using ZXing.QrCode.Internal;

 方法一是產生二維碼圖片:

/// <summary>/// 產生二維碼圖片/// </summary>/// <param name="contents">要產生二維碼包含的資訊</param>/// <param name="width">產生的二維碼寬度(預設300像素)</param>/// <param name="height">產生的二維碼高度(預設300像素)</param>/// <returns>二維碼圖片</returns>public static Bitmap GeneratorQrImage(string contents, int width = 300, int height = 300){    if (string.IsNullOrEmpty(contents))    {        return null;    }    //本文地址:http://www.cnblogs.com/Interkey/p/qrcode.html    EncodingOptions options = null;    BarcodeWriter writer = null;    options = new QrCodeEncodingOptions    {        DisableECI = true,        CharacterSet = "UTF-8",        Width = width,        Height = height,        ErrorCorrection = ErrorCorrectionLevel.H,    };    writer = new BarcodeWriter();    writer.Format = BarcodeFormat.QR_CODE;    writer.Options = options;    Bitmap bitmap = writer.Write(contents);    return bitmap;}

方法二是產生中間帶圖片的二維碼圖片:

/// <summary>/// 產生中間帶有圖片的二維碼圖片/// </summary>/// <param name="contents">要產生二維碼包含的資訊</param>/// <param name="middleImg">要產生到二維碼中間的圖片</param>/// <param name="width">產生的二維碼寬度(預設300像素)</param>/// <param name="height">產生的二維碼高度(預設300像素)</param>/// <returns>中間帶有圖片的二維碼</returns>public static Bitmap GeneratorQrImage(string contents, Image middleImg, int width = 300, int height = 300){    if (string.IsNullOrEmpty(contents))    {        return null;    }    if (middleImg == null)    {        //return null;        return GeneratorQrImage(contents);    }    ////本文地址:http://www.cnblogs.com/Interkey/p/qrcode.html    //構造二維碼寫碼器    MultiFormatWriter mutiWriter = new MultiFormatWriter();    Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();    hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");    hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);    //產生二維碼    BitMatrix bm = mutiWriter.encode(contents, BarcodeFormat.QR_CODE, width, height, hint);    BarcodeWriter barcodeWriter = new BarcodeWriter();    Bitmap bitmap = barcodeWriter.Write(bm);                //擷取二維碼實際尺寸(去掉二維碼兩邊空白後的實際尺寸)    int[] rectangle = bm.getEnclosingRectangle();    //計算插入圖片的大小和位置    int middleImgW = Math.Min((int)(rectangle[2] / 3.5), middleImg.Width);    int middleImgH = Math.Min((int)(rectangle[3] / 3.5), middleImg.Height);    int middleImgL = (bitmap.Width - middleImgW) / 2;    int middleImgT = (bitmap.Height - middleImgH) / 2;    //將img轉換成bmp格式,否則後面無法建立 Graphics對象    Bitmap bmpimg = new Bitmap(bitmap.Width, bitmap.Height, System.Drawing.Imaging.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(bitmap, 0, 0);    }    //在二維碼中插入圖片    Graphics myGraphic = Graphics.FromImage(bmpimg);    //白底    myGraphic.FillRectangle(Brushes.White, middleImgL, middleImgT, middleImgW, middleImgH);    myGraphic.DrawImage(middleImg, middleImgL, middleImgT, middleImgW, middleImgH);    return bmpimg;}

效果如下:

中間有圖片與沒有圖片的產生的不一樣,原因不清楚,初步以為中間有圖片的是因為容錯率的原故。

對應的解析代碼也很簡單:

// create a barcode reader instanceIBarcodeReader reader = new BarcodeReader();// load a bitmapBitmap barcodeBitmap = (Bitmap)Image.FromFile("C:\\sample-barcode-image.png");// detect and decode the barcode inside the bitmapResult result = reader.Decode(barcodeBitmap);// do something with the resultif (result != null){    this.txtDecoderType.Text = result.BarcodeFormat.ToString();    this.txtDecoderContent.Text = result.Text;}

以上是產生和解析二維碼的代碼,至於產生一維碼和對應的解析也很簡單。

 

其他常用可產生二維碼的組件有:QrCode.Net(開源)、ZXing(開源)、ThoughtWorks.QRCode等。

瞭解了二維碼的資料存放區後,可以基於這些技術產生富有創意的二維碼。

 

參考:

  1. QR二維碼產生器源碼(中間可插入小圖片)
  2. 用C#實現的條碼和二維碼編碼解碼器
  3. 如何在ASP.NET和WPF中顯示二維碼
  4. C#利用QrCode.Net產生二維碼(Qr碼)
  5. 二維碼的產生細節和原理
  6. 帶你探索條碼世界的奧秘
  7. Android二維碼識別 開源項目ZXing的編譯

.NET使用ZXing.NET產生中間帶圖片的二維碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.