首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> 關於C#

C# 版 QR Code 二維碼產生器:支援編碼/解碼、列印、IrisSkin 換膚

     編輯:關於C#

基於 Google 開源 ZXing.NET 函式庫打造的 WinForms 二維碼工具,整合編碼、解碼、儲存、列印、換膚功能。

支援格式與版本

  • ZXing.NET v2.2(官方下載)
  • 編碼格式:QR Code、Code 128/39/93、EAN-8/13、UPC-A/E、Data Matrix、PDF417、Aztec、ITF、Codabar、RSS-14 等
  • 跨平台:Android、iOS、C++、C#、Java、Ruby 等多語言移植版

軟體功能一覽

  1. 即時文字輸入 → 同步產生 QR Code
  2. 支援圖片儲存、列印
  3. 開啟圖片檔 → 解碼還原文字
  4. IrisSkin4 引擎支援換膚(內建 SportsGreen.ssk)
  5. ToolTip 滑鼠懸停提示、按鈕圖片切換動畫
主介面編碼模式
圖 1:主視窗 - 文字輸入即時產生 QR Code
解碼模式介面
圖 2:切換至解碼面板,開啟圖片自動辨識
列印預覽
圖 3:列印功能示範
換膚效果
圖 4:IrisSkin 換膚效果

核心程式碼解析

初始化:換膚、ToolTip、按鈕圖片切換

public MainForm() {
    InitializeComponent();
    skinEngine1.SkinFile = Application.StartupPath + @"\skins\SportsGreen.ssk";
    p1.Visible = true; // 編碼面板

    var tip = new ToolTip {
        AutoPopDelay = 5000, InitialDelay = 1000, ReshowDelay = 500, ShowAlways = true
    };
    tip.SetToolTip(btnOpen, "開啟");
    tip.SetToolTip(btnSave, "儲存");
    tip.SetToolTip(btnPrint, "列印二維碼");
    tip.SetToolTip(btnDecode, "解碼");
    tip.SetToolTip(btnExit, "返回產生二維碼");

    // 滑鼠進入/離開切換按鈕圖片(略,見完整碼)
}

即時編碼:文字異動事件

private void txtContent_TextChanged(object sender, EventArgs e) {
    var content = txtContent.Text;
    if (string.IsNullOrEmpty(content)) {
        MessageBox.Show("請輸入內容", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }
    var matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 200, 200);
    var bitmap = ToBitmap(matrix);
    picQR.Image = bitmap;
}

public static Bitmap ToBitmap(ByteMatrix matrix) {
    var bmp = new Bitmap(matrix.Width, matrix.Height, PixelFormat.Format32bppArgb);
    for (int x = 0; x < matrix.Width; x++)
        for (int y = 0; y < matrix.Height; y++)
            bmp.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? Color.Black : Color.White);
    return bmp;
}

解碼:開啟圖片 → RGBLuminanceSource → HybridBinarizer → MultiFormatReader

private void btnOpen_Click(object sender, EventArgs e) {
    if (p1.Visible) { // 編碼模式:開啟文字檔
        if (openFileDialog1.ShowDialog() != DialogResult.OK) return;
        var path = openFileDialog1.FileName;
        if (path.EndsWith(".txt")) {
            txtContent.Text = File.ReadAllText(path, Encoding.Default);
        } else { MessageBox.Show("僅支援 .txt 檔案"); }
    } else { // 解碼模式:開啟圖片檔
        if (openFileDialog1.ShowDialog() != DialogResult.OK) return;
        var path = openFileDialog1.FileName;
        if (!path.EndsWith(".png")) { MessageBox.Show("僅支援 .png 圖片"); return; }
        var img = Image.FromFile(path);
        var bmp = new Bitmap(img);
        picOpenQR.Image = bmp;

        var source = new RGBLuminanceSource(bmp, bmp.Width, bmp.Height);
        var binBitmap = new BinaryBitmap(new HybridBinarizer(source));
        Result result;
        try { result = new MultiFormatReader().decode(binBitmap); }
        catch (ReaderException ex) { MessageBox.Show(ex.ToString()); return; }
        txtDecoded.Text = result.Text;
    }
}

儲存與列印

private void btnSave_Click(object sender, EventArgs e) {
    if (p1.Visible) { // 儲存 QR Code 圖片
        saveFileDialog1.Filter = "*.png|";
        if (saveFileDialog1.ShowDialog() == DialogResult.OK) {
            var path = saveFileDialog1.FileName;
            if (!path.EndsWith(".png")) path += ".png";
            picQR.Image.Save(path);
        }
    } else { // 儲存解碼文字
        saveFileDialog1.Filter = "*.txt|";
        if (saveFileDialog1.ShowDialog() == DialogResult.OK) {
            File.AppendAllText(saveFileDialog1.FileName + ".txt", txtDecoded.Text, Encoding.Default);
        }
    }
}

private void btnPrint_Click(object sender, EventArgs e) {
    var dlg = new PrintDialog { Document = printDocument };
    if (dlg.ShowDialog() == DialogResult.OK) {
        try { printDocument.Print(); }
        catch { printDocument.PrintController.OnEndPrint(printDocument, new PrintEventArgs()); }
    }
}

private void printDocument_PrintPage(object sender, PrintPageEventArgs e) {
    e.Graphics.DrawImage(p1.Visible ? picQR.Image : picOpenQR.Image, 20, 20);
}

計時器顯示目前時間

private void timer_Tick(object sender, EventArgs e) {
    lblTime.Text = DateTime.Now.ToString();
}

原始碼下載:GoogleQRCode.zip
完整程式(含皮膚):51CTO 下載

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved