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

ZXing是一个开源Java类库用于解析多种格式的1D/2D条形码。目标是能够对QR编码、Data Matrix、UPC的1D条形码进行解码。 其提供了多种平台下的客户端包括:J2ME、J2SE和Android。

首先,在其官网http://code.google.com/p/zxing/上去下载源码

编译在其csharp目录下打开zxing.csproj文件,新建一个工程。在编译之前修改两个错误:

源代码中有两处UTF-8的问题,会导致乱码,

其一: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


 

修改完之后,点重新生成编译出新的dll文件。

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

这样自己就可以试用了,主要是二维码的生成和识别两个用处:


 

二维码的生成:

[csharp] view plain copy

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;   
}  



 

二维码的读取识别:

[csharp] view plain copy

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);   
}  



 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值