111,111
社区成员




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZXing;
using ZXing.Common;
namespace app1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
BarcodeWriter barcodeWriter = new BarcodeWriter();
barcodeWriter.Format = BarcodeFormat.QR_CODE;
EncodingOptions arg_a1;
arg_a1 = new EncodingOptions();
arg_a1.Height = 100;
arg_a1.Width = 100;
//如何设置字符集?
barcodeWriter.Options = arg_a1;
Bitmap m1 =
barcodeWriter.Write("http://aabb.cosj.c/中");
m1.Save(file1);
}
string file1 = AppDomain.CurrentDomain.BaseDirectory + "zxing-img01.bmp";
private void button2_Click(object sender, EventArgs e)
{
Bitmap m1 = (Bitmap)Bitmap.FromFile(file1);
BarcodeReader barcodeReader = new BarcodeReader();
DecodingOptions arg_1 = new DecodingOptions();
//arg_1.CharacterSet = "UTF-8";//提示已过时?
arg_1.CharacterSet = "gb2312";//设置了字符集也读不出来中文汉字
barcodeReader.Options = arg_1;
Result result = barcodeReader.Decode(m1);
textBox1.Text = result.Text;
}
}
}
//设置读取二维码
DecodingOptions decodeOption = new DecodingOptions();
decodeOption.PossibleFormats = new List<BarcodeFormat>(){
BarcodeFormat.QR_CODE,
};
if (pictureBox.Image == null)
{
MessageBox.Show("请载入图像资源!");
return;
}
//读取操作
BarcodeReader bar = new BarcodeReader();
bar.Options = decodeOption;
ZXing.Result rs = bar.Decode(pictureBox.Image as Bitmap);
if (rs == null)
{
txtMsg.Text = "读取失败";
MessageBox.Show("读取失败");
}
else
{
txtMsg.Text = rs.Text;
MessageBox.Show("读取成功,内容:" + rs.Text);
}
//生成二维码
private void btn_Click(object sender, RoutedEventArgs e)
{
// 设置QR二维码的规格
ZXing.QrCode.QrCodeEncodingOptions qrEncodeOption = new ZXing.QrCode.QrCodeEncodingOptions();
qrEncodeOption.CharacterSet = "UTF-8"; //设置编码格式,否则读取'中文'乱码
qrEncodeOption.Height = 200;
qrEncodeOption.Width = 200;
qrEncodeOption.Margin = 1; //设置周围空白边距
//生成条形码图片并保存
ZXing.BarcodeWriter wr = new BarcodeWriter();
wr.Format = BarcodeFormat.QR_CODE; //二维码
wr.Options = qrEncodeOption;
Bitmap img = wr.Write(txtMsg.Text);
string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "\\QR-" + this.txtMsg.Text + ".jpg";
img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
//读取保存的图片
title.Text = filePath;
//this.image.Source = ChangeBitmapToImageSource(img);
pictureBox.Image = img;
MessageBox.Show("保存成功:" + filePath);
}
//读取二维码
private void Dqewm_Click(object sender, RoutedEventArgs e)
{
//设置读取二维码
DecodingOptions decodeOption = new DecodingOptions();
decodeOption.PossibleFormats = new List<BarcodeFormat>(){
BarcodeFormat.QR_CODE,
};
if (pictureBox.Image == null)
{
MessageBox.Show("请载入图像资源!");
return;
}
//读取操作
BarcodeReader bar = new BarcodeReader();
bar.Options = decodeOption;
ZXing.Result rs = bar.Decode(pictureBox.Image as Bitmap);
if (rs == null)
{
txtMsg.Text = "读取失败";
MessageBox.Show("读取失败");
}
else
{
txtMsg.Text = rs.Text;
MessageBox.Show("读取成功,内容:" + rs.Text);
}
}