ASP.NET產生二維碼的例子

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   color   os   java   使用   

分享一例c#產生二維碼的代碼,直接引用ThoughtWorks.QRCode.dll 類產生二維碼,有需要的朋友參考下。

方法1,直接引用ThoughtWorks.QRCode.dll 類,產生二維碼。
 
程式碼範例:

ThoughtWorks.QRCode.Codec.QRCodeEncoder encoder = new QRCodeEncoder();encoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//編碼方法(注意:BYTE能支援中文,ALPHA_NUMERIC掃描出來的都是數字)encoder.QRCodeScale = 4;//大小encoder.QRCodeVersion = 0;//版本(注意:設定為0主要是防止編碼的字串太長時發生錯誤)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|所有檔案(*.*) |*.*";//設定預設檔案類型顯示順序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條碼。目標是能夠對QR編碼、Data Matrix、UPC的1D條碼進行解碼。於此同時,它同樣提供 cpp,ActionScript,android,iPhone,rim,j2me,j2se,jruby,C#等方式的類庫。zxing類庫的作用主要是解碼,是目前開源類庫中解碼能力比較強的(商業的另說,不過對於動輒成千上萬的類庫授權費用,的確很值)。
到Googlecode下載相應的代碼
1.下載zxing最新的包
到zxing的首頁: 
http://code.google.com/p/zxing/
找到其中的CSharp檔案夾,在vs中開啟並編譯,將obj下debug中的zxing.dll複製並粘帖到你的項目中的bin檔案目錄下,
右擊添加項目引用。將zxing.dll引用到項目中,就可以在需要的地方使用了。
原始碼中有兩處UTF-8的問題,會導致中文出現亂碼(編譯.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";
應將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;    }

 

ASP.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.