C#QRコード生成と読み出し

3250 ワード

ZXingを使用します.dll、クラスライブラリをダウンロードし、参照を追加すればいい
ネーミングスペース:using ZXing;
QRコードの生成
    /// 
    ///        
    /// 
    ///           
    ///        
    ///        
    /// 
    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);
        }
        catch (Exception ex)
        {
            //    
        }
        return result;
    }

QRコードを読み込みます. /// /// /// /// /// private string DecodeQrCode(Bitmap barcodeBitmap) { BarcodeReader reader = new BarcodeReader(); reader.Options.CharacterSet = "UTF-8"; var result = reader.Decode(barcodeBitmap); return (result == null) ? null : result.Text; }