zxing.net读取和保存二维码,设置中文字符集,读取中文二维码

ijunxiong 2016-05-13 11:52:04

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


//button1点击执行生成了当前路径下的二维码图片
//button2点击执行读取当生前路径下生成的二维码图片,并解析出字符串,
但是,button1生成的字符串是"http://aabb.cosj.c/中",却被读成了"http://aabb.cosj.c/?",因为中文无法识别,
找了很久没找到zxing.net的关于读取中文二维码的说明,请知道帮忙一下,多谢,注:不是java的哦,
...全文
691 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
virword 2020-03-02
  • 打赏
  • 举报
回复
sk 可以实现
  • 打赏
  • 举报
回复
你可以把 http://aabb.cosj.c/%e4%b8%ad 贴到你的浏览器上看看。或者你实际去用 http 调试器看看 http://aabb.cosj.c/中 真正给web服务器发送的 url 请求地址是怎样写的。 这里,url上根本不需要写中文!
  • 打赏
  • 举报
回复
这是假设二维码中十月份有中文的文本内容。但是你贴出来的代码有错误,所谓 http://aabb.cosj.c/中 这里根本不应该有中文,应该使用 UrlEncode方式处理为英文可打印字符。处理 url 应该遵循国际标准,不要使用中国的“地方法”。 仅根据你贴出的代码,你根本用不到中文。
  • 打赏
  • 举报
回复
zxing 原本有个 bug,源代码中的默认字符集设置是错误的,无法处理中文。网上下载 zxing dll 的时候你要看清楚是否是被人重新编译过的。或者你下载了源代码之后修改那一个“编码”字符串,才能用。
Struggle_hardllz 2016-05-13
  • 打赏
  • 举报
回复
可以读取的呀,我用之前也用了zxing这个DLL 这是我读取二维码的代码
  //设置读取二维码
            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);
            }
Struggle_hardllz 2016-05-13
  • 打赏
  • 举报
回复
引用 13 楼 ijunxiong 的回复:
@a123455o 代码已测试,可用,谢谢,另外测了 写的时候,如果字符集不设置,读取确实读不了,以及设置成gb2312,gbk等中文集都可以读出 中文字符来,再次谢过
不谢,做出来了就好 记得结贴哈
ijunxiong 2016-05-13
  • 打赏
  • 举报
回复
@a123455o 代码已测试,可用,谢谢,另外测了 写的时候,如果字符集不设置,读取确实读不了,以及设置成gb2312,gbk等中文集都可以读出 中文字符来,再次谢过
Struggle_hardllz 2016-05-13
  • 打赏
  • 举报
回复
引用 11 楼 ijunxiong 的回复:
@a123455o 刚看到,谢谢,我测下,,
dll我是从网上下载的,应该是可以的
ijunxiong 2016-05-13
  • 打赏
  • 举报
回复
@a123455o 还有,你是如何创建二维码的,可否发下代码,,谢谢 还有,,dll,,是官方下载的吗?
Struggle_hardllz 2016-05-13
  • 打赏
  • 举报
回复
引用 8 楼 ijunxiong 的回复:
@a123455o 但你这个不需要设置字符集吗。
不需要,这是我生成二维码加读取二维码的代码
 //生成二维码
        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);
            }
        }
ijunxiong 2016-05-13
  • 打赏
  • 举报
回复
@a123455o 刚看到,谢谢,我测下,,
ijunxiong 2016-05-13
  • 打赏
  • 举报
回复
@a123455o 但你这个不需要设置字符集吗。
Struggle_hardllz 2016-05-13
  • 打赏
  • 举报
回复
引用 6 楼 ijunxiong 的回复:
@a123455o 你说的可以读取,确定是中文吗,dll可以发给我吗,ijunxiong@126.com

是中问的呀
ijunxiong 2016-05-13
  • 打赏
  • 举报
回复
@a123455o 你说的可以读取,确定是中文吗,dll可以发给我吗,ijunxiong@126.com
ijunxiong 2016-05-13
  • 打赏
  • 举报
回复
@sp1234 哦,不是,我是举个例子,只是个字符串,不是url

111,111

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧