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

简介:

二维码的生成:

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

 

 

二维码的读取识别:

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

源代码中有两处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

本文转自博客园知识天地的博客,原文链接:zxing二维码的生成与解码(C#) ,如需转载请自行联系原博主。

相关文章
|
存储 关系型数据库 MySQL
OceanBase数据库 与 mysql 对比
OceanBase数据库 与 mysql 对比
5754 1
|
网络协议 测试技术 网络安全
|
机器学习/深度学习 自然语言处理 计算机视觉
【YOLOv8改进 - Backbone主干】VanillaNet:极简的神经网络,利用VanillaBlock降低YOLOV8参数
【YOLOv8改进 - Backbone主干】VanillaNet:极简的神经网络,利用VanillaBlock降低YOLOV8参数
|
SQL 人工智能 移动开发
Android 遍历界面所有的View
本文讲述如何遍历获取页面中所有的view,并输出对应的id,textview文本内容,imageview实际大小及设置的图片大小。 可用于检测android应用中的大图。
|
数据采集 Web App开发 前端开发
Python中好用的爬虫框架
**Scrapy** 是一个强大的Python爬虫框架,适合大规模数据采集,提供高度可定制的爬取流程、内置数据提取工具、自动请求调度、分布式爬取支持、中间件扩展及多种数据存储方式。 **Beautiful Soup** 和 **Requests** 结合使用,便于Python中简单网页的请求和HTML解析。Requests发送HTTP请求,Beautiful Soup解析内容,适合小型项目或数据提取。 **Requests-HTML** 是Requests的扩展,支持HTML解析和CSS选择器,自动处理链接,适合网页解析任务。
451 1
|
关系型数据库 API 数据库
SqlAlchemy 2.0 中文文档(二十三)(4)
SqlAlchemy 2.0 中文文档(二十三)
247 0
|
安全 Ubuntu Linux
Ubuntu解密:Root账户登录问题一网打尽
Ubuntu解密:Root账户登录问题一网打尽
437 1
|
安全 算法 网络协议
真实世界的密码学(二)(3)
真实世界的密码学(二)
509 4
|
算法 安全 Java
技术经验分享:JAVAAES加密与解密
技术经验分享:JAVAAES加密与解密
776 0
|
数据挖掘 Python
Scipy 高级教程——统计学
Scipy 高级教程——统计学【1月更文挑战第11篇】
346 4
Scipy 高级教程——统计学