验证中...
Languages: C#
Categories: 编程语言基础
Latest update 2019-10-16 10:20
二维码生成与解析
Raw Copy
<asp:Label ID="Label1" runat="server" Text="输入信息"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<p>
<asp:Button ID="Button1" runat="server" Text="生成二维码" OnClick="Button1_Click" />
</p>
<asp:Image ID="Image1" runat="server" />
protected void Button1_Click(object sender, EventArgs e)
{
string str = this.TextBox1.Text;
Bitmap bt;
string enCodeString = str;
//生成设置编码实例
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
//设置二维码的规模,默认4
qrCodeEncoder.QRCodeScale = 4;
//设置二维码的版本,默认7
qrCodeEncoder.QRCodeVersion = 7;
//设置错误校验级别,默认中等
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
//生成二维码图片
bt = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8);
//二维码图片的名称
string filename = DateTime.Now.ToString("yyyyMMddHHmmss");
//保存二维码图片在photos路径下
string path = "";
path = Server.MapPath("~/photos/") + filename + ".jpg";
bt.Save(path);
//图片控件要显示的二维码图片路径
this.Image1.ImageUrl = "~/photos/" + filename + ".jpg";
DeCoder(path);
}
/// <summary>
/// 二维码解析
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public string DeCoder(string path)
{
string result = "";
//string strSaveDir = Request.MapPath("/QRcode/");
string strSaveDir = path;
//if (!Directory.Exists(strSaveDir))
//{
// Directory.CreateDirectory(strSaveDir);
//}
string strSavePath = Path.Combine(strSaveDir, "wolfy.png");
strSavePath = path;
if (System.IO.File.Exists(strSavePath))
{
QRCodeDecoder decoder = new QRCodeDecoder();
result = decoder.decode(new ThoughtWorks.QRCode.Codec.Data.QRCodeBitmapImage(new Bitmap(System.Drawing.Image.FromFile(strSavePath))), Encoding.UTF8);
}
return result;
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search