不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
C#调用ZXing生成二维码简单示例
编辑:dnawo 日期:2012-10-12
复制内容到剪贴板
程序代码

<%@ WebHandler Language="C#" Class="Default" %>
using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using com.google.zxing;
using com.google.zxing.common;
using com.google.zxing.qrcode.decoder;
public class Default : IHttpHandler {
public void ProcessRequest (HttpContext context) {
CreateQRCode(context);
}
/// <summary>
/// 生成二维码
/// </summary>
/// <param name="context"></param>
private void CreateQRCode(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
int width = 300; //宽度
int height = 300; //高度
string content = "http://www.mzwu.com/"; //内容
Color fgColor = ColorTranslator.FromHtml("0xFF000000"); //前景色
Color bgColor = ColorTranslator.FromHtml("0xFFFFFFFF"); //背景色
Hashtable hints = new Hashtable();
hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//容错能力
hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//字符集
ByteMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
using (Bitmap bmap = new Bitmap(width, height, 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 ? fgColor : bgColor);
}
}
bmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
/// <summary>
/// 解析二维码
/// </summary>
/// <param name="img"></param>
/// <returns></returns>
private string ParseQRCode(string img)
{
using (Bitmap bmap = new Bitmap(Image.FromFile(img)))
{
Hashtable hints = new Hashtable();
hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//字符集
LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = new MultiFormatReader().decode(bitmap, hints);
return result.Text;
}
}
public bool IsReusable {
get {
return false;
}
}
}
using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using com.google.zxing;
using com.google.zxing.common;
using com.google.zxing.qrcode.decoder;
public class Default : IHttpHandler {
public void ProcessRequest (HttpContext context) {
CreateQRCode(context);
}
/// <summary>
/// 生成二维码
/// </summary>
/// <param name="context"></param>
private void CreateQRCode(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
int width = 300; //宽度
int height = 300; //高度
string content = "http://www.mzwu.com/"; //内容
Color fgColor = ColorTranslator.FromHtml("0xFF000000"); //前景色
Color bgColor = ColorTranslator.FromHtml("0xFFFFFFFF"); //背景色
Hashtable hints = new Hashtable();
hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//容错能力
hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//字符集
ByteMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
using (Bitmap bmap = new Bitmap(width, height, 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 ? fgColor : bgColor);
}
}
bmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
/// <summary>
/// 解析二维码
/// </summary>
/// <param name="img"></param>
/// <returns></returns>
private string ParseQRCode(string img)
{
using (Bitmap bmap = new Bitmap(Image.FromFile(img)))
{
Hashtable hints = new Hashtable();
hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//字符集
LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = new MultiFormatReader().decode(bitmap, hints);
return result.Text;
}
}
public bool IsReusable {
get {
return false;
}
}
}

参考资料
[1].ZXing:http://code.google.com/p/zxing/
[2].[C#]透過 zxing 產生QR Code:http://www.dotblogs.com.tw/jaigi/archive/2012/03/26/71049.aspx
评论: 0 | 引用: 0 | 查看次数: 8622
发表评论
请登录后再发表评论!