日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

zxing二维码的生成与解码(C#)

發(fā)布時間:2023/12/20 C# 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 zxing二维码的生成与解码(C#) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

ZXing是一個開源Java類庫用于解析多種格式的1D/2D條形碼。目標是能夠?qū)R編碼、Data Matrix、UPC的1D條形碼進行解碼。 其提供了多種平臺下的客戶端包括:J2ME、J2SE和Android。

首先,在其官網(wǎng)http://code.google.com/p/zxing/上去下載源碼

編譯在其csharp目錄下打開zxing.csproj文件,新建一個工程。在編譯之前修改兩個錯誤:

?

源代碼中有兩處UTF-8的問題,會導(dǎo)致亂碼,

其一:com.google.zxing.qrcode.encoder.encoder類中的

internal const System.String DEFAULT_BYTE_MODE_ENCODING = "ISO-8859-1";

此處,將ISO-8859-1改為UTF-8

其二:com.google.zxing.qrcode.decoder.DecodedBitStreamParser類的成員

private const System.String UTF8 = "UTF8";

應(yīng)將UTF8改為UTF-8

?

修改完之后,點重新生成編譯出新的dll文件。

將zxing.dll考入到你自己所需要的工程中,并添加引用。

這樣自己就可以試用了,主要是二維碼的生成和識別兩個用處:

?

二維碼的生成:

using com.google.zxing.qrcode; using com.google.zxing; using com.google.zxing.common; using ByteMatrix = com.google.zxing.common.ByteMatrix; using EAN13Writer = com.google.zxing.oned.EAN13Writer; using EAN8Writer = com.google.zxing.oned.EAN8Writer; using MultiFormatWriter = com.google.zxing.MultiFormatWriter; private void button1_Click(object sender, EventArgs e) {string content = textBox1.Text; ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 200, 200); Bitmap bitmap = toBitmap(byteMatrix); pictureBox1.Image = bitmap; //writeToFile(byteMatrix, System.Drawing.Imaging.ImageFormat.Png, sFD.FileName); //SaveFileDialog sFD = new SaveFileDialog(); //sFD.DefaultExt = "*.png|*.png"; //sFD.AddExtension = true; //try //{ // if (sFD.ShowDialog() == DialogResult.OK) // { // } //} //catch (Exception ex) //{ // MessageBox.Show(ex.Message); //} } public static void writeToFile(ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, string file) { Bitmap bmap = toBitmap(matrix); bmap.Save(file, format); } public static Bitmap toBitmap(ByteMatrix matrix) { int width = matrix.Width; int height = matrix.Height; Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF")); } } return bmap; }

二維碼的讀取識別:

private void button1_Click(object sender, EventArgs e) { if (this.openFileDialog1.ShowDialog() != DialogResult.OK) { return; } Image img = Image.FromFile(this.openFileDialog1.FileName); Bitmap bmap; try { bmap = new Bitmap(img); } catch (System.IO.IOException ioe) { MessageBox.Show(ioe.ToString()); return; } if (bmap == null) { MessageBox.Show("Could not decode image"); return; } LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height); com.google.zxing.BinaryBitmap bitmap = new com.google.zxing.BinaryBitmap(new COMMON.HybridBinarizer(source)); Result result; try { result = new MultiFormatReader().decode(bitmap); } catch(ReaderException re) { MessageBox.Show(re.ToString()); return; } MessageBox.Show(result.Text); }

?

轉(zhuǎn)載于:https://www.cnblogs.com/hnsongbiao/p/9145285.html

總結(jié)

以上是生活随笔為你收集整理的zxing二维码的生成与解码(C#)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。