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#) ,如需转载请自行联系原博主。

相关文章
|
Android开发
ZXing源码解析二:掌握解码步骤2
ZXing源码解析二:掌握解码步骤
|
uml Android开发
ZXing源码解析二:掌握解码步骤1
ZXing源码解析二:掌握解码步骤
28用zxing对二维码的解析
28用zxing对二维码的解析
130 0
27.用zxing生成二维码
27.用zxing生成二维码
217 0
|
Android开发
zxing 如何识别反转二维码
说起二维码扫描,估计很多人用的是 zxing 吧。 然而 zxing 虽然好用,但是却有一些坑。 这边分析一下自己实际项目遇到的一个坑。 什么坑呢? 下面举个栗子你就懂了。
2030 0
|
Java
zxing生成二维码和读取二维码
当然,首先要导入zxing的jar包。     生成二维码代码: package com.imooc.zxing; import java.io.File; import java.nio.file.
1512 0