ASP.NET生成二維碼的方法總結(jié)
本文實(shí)例總結(jié)了ASP.NET生成二維碼的方法。分享給大家供大家參考,具體如下:
分享一例c#生成二維碼的代碼,直接引用ThoughtWorks.QRCode.dll 類生成二維碼,有需要的朋友參考下。
方法1.直接引用ThoughtWorks.QRCode.dll 類,生成二維碼。
代碼示例:
ThoughtWorks.QRCode.Codec.QRCodeEncoder encoder = new QRCodeEncoder();
encoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//編碼方法(注意:BYTE能支持中文,ALPHA_NUMERIC掃描出來的都是數(shù)字)
encoder.QRCodeScale = 4;//大小
encoder.QRCodeVersion = 0;//版本(注意:設(shè)置為0主要是防止編碼的字符串太長時發(fā)生錯誤)
encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
String qrdata = "二維碼信息";
System.Drawing.Bitmap bp = encoder.Encode(qrdata.ToString(), Encoding.GetEncoding("GB2312"));
Image image = bp;
Object oMissing = System.Reflection.Missing.Value;
pictureBox1.Image = bp;
保存二維碼圖片:
代碼示例:
SaveFileDialog sf = new SaveFileDialog();
sf.Title = "選擇保存文件位置";
sf.Filter = "保存圖片(*.jpg) |*.jpg|所有文件(*.*) |*.*";
//設(shè)置默認(rèn)文件類型顯示順序
sf.FilterIndex = 1;
//保存對話框是否記憶上次打開的目錄
sf.RestoreDirectory = true;
if (sf.ShowDialog() == DialogResult.OK)
{
Image im = this.pictureBox1.Image;
//獲得文件路徑
string localFilePath = sf.FileName.ToString();
if (sf.FileName != "")
{
string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);//獲取文件名,不帶路徑
// newFileName = fileNameExt+DateTime.Now.ToString("yyyyMMdd") ;//給文件名后加上時間
string FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf(".")); //獲取文件路徑,帶文件名,不帶后綴
string fn = sf.FileName;
pictureBox1.Image.Save(FilePath +"-"+ DateTime.Now.ToString("yyyyMMdd") + ".jpg");
}
}
//解析二維碼信息
// QRCodeDecoder decoder = new QRCodeDecoder();
// String decodedString = decoder.decode(new QRCodeBitmapImage(new Bitmap(pictureBox1.Image)));
//this.label3.Text = decodedString;
方法2.引用ZXing類庫。
ZXing是一個開源Java類庫用于解析多種格式的1D/2D條形碼。目標(biāo)是能夠?qū)R編碼、Data Matrix、UPC的1D條形碼進(jìn)行解碼。于此同時,它同樣提供 cpp,ActionScript,android,iPhone,rim,j2me,j2se,jruby,C#等方式的類庫。zxing類庫的作用主 要是解碼,是目前開源類庫中解碼能力比較強(qiáng)的(商業(yè)的另說,不過對于動輒成千上萬的類庫授權(quán)費(fèi)用,的確很值)。
到谷歌code下載相應(yīng)的代碼
1.下載zxing最新的包
到zxing的主頁: http://code.google.com/p/zxing/
找到其中的CSharp文件夾,在vs中打開并編譯,將obj下debug中的zxing.dll復(fù)制并粘帖到你的項目中的bin文件目錄下,
右擊添加項目引用。將zxing.dll引用到項目中,就可以在需要的地方使用了。
源代碼中有兩處UTF-8的問題,會導(dǎo)致中文出現(xiàn)亂碼(編譯.dll之前修改)
其一: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
代碼示例:
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;
方法:
string content = "二維碼信息";
ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300);
Bitmap bitmap = toBitmap(byteMatrix);
pictureBox1.Image = bitmap;
SaveFileDialog sFD = new SaveFileDialog();
sFD.Filter = "保存圖片(*.png) |*.png|所有文件(*.*) |*.*";
sFD.DefaultExt = "*.png|*.png";
sFD.AddExtension = true;
if (sFD.ShowDialog() == DialogResult.OK)
{
if (sFD.FileName != "")
{
writeToFile(byteMatrix, System.Drawing.Imaging.ImageFormat.Png, sFD.FileName);
}
}
解析二維碼:
代碼示例:
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 bitmap1 = new com.google.zxing.BinaryBitmap(new HybridBinarizer(source));
Result result;
try
{
result = new MultiFormatReader().decode(bitmap1);
}
catch (ReaderException re)
{
MessageBox.Show(re.ToString());
return;
}
MessageBox.Show(result.Text);
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;
}
PS:這里再為大家推薦一款功能非常強(qiáng)悍的二維碼在線生成工具,免費(fèi)供大家使用:
在線生成二維碼工具(加強(qiáng)版):
http://tools.jb51.net/transcoding/jb51qrcode
更多關(guān)于asp.net相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《asp.net字符串操作技巧匯總》、《asp.net操作XML技巧總結(jié)》、《asp.net文件操作技巧匯總》、《asp.net ajax技巧總結(jié)專題》及《asp.net緩存操作技巧總結(jié)》。
希望本文所述對大家asp.net程序設(shè)計有所幫助。
相關(guān)文章
ASP.NET(C#)中操作SQLite數(shù)據(jù)庫實(shí)例
最近項目中有使用到SQLite數(shù)據(jù)庫,于是查找資料,編寫了一個ASP.NET基于C#語言的SQLite數(shù)據(jù)庫操作實(shí)例.大家看代碼就可以看懂了,和以往使用ADO.NET操作SQL數(shù)據(jù)庫類似.2009-12-12
.net?core?刪除字符串最后一個字符的七大類N種實(shí)現(xiàn)方式(總結(jié)篇)
本文詳細(xì)介紹了七大類、N種不同的方法來刪除字符串的最后一個字符,涵蓋了從簡單的字符串方法到使用StringBuilder、數(shù)組操作、Linq以及正則表達(dá)式等多種技術(shù)手段,本文給大家介紹.net?core刪除字符串最后一個字符,感興趣的朋友一起看看吧2024-10-10
ASP.NET MVC實(shí)現(xiàn)多個按鈕提交的方法
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC實(shí)現(xiàn)多個按鈕提交的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
.NET中獲取Access新增記錄Id怪現(xiàn)象解決方法
寫了一個函數(shù)獲取Access表中指定用戶Id,要求當(dāng)傳入的用戶名不存在時,則在表中新增一條記錄并返回Id2012-03-03
.NET內(nèi)存泄漏分析Windbg項目實(shí)例
這篇文章介紹了.NET內(nèi)存泄漏分析Windbg項目實(shí)例,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
asp.net文件上傳功能(單文件,多文件,自定義生成縮略圖,水印)
上傳功能,是大家經(jīng)常用到了,可能每一個項目都可以會用到。網(wǎng)上到處都有上傳功能的代碼。比我寫的好的有很多。我這里也僅是分享我的代碼。2011-09-09
ASP.NET?MVC5網(wǎng)站開發(fā)用戶注冊(四)
上一次把基本框架搭建起來了,這次開始整Web部分,終于可以看到界面了小激動一下,web項目部分從用戶功能開始,基本有注冊,登錄、注銷、查找、查看、刪除等涉及Member區(qū)域和Manage區(qū)域,供大家參考,具體內(nèi)容如下2015-09-09

