前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用zxing类生成一维码、二维码实例

使用zxing类生成一维码、二维码实例

原创
作者头像
用户8671053
修改2021-11-02 16:01:44
1.2K1
修改2021-11-02 16:01:44
举报
文章被收录于专栏:码农的生活码农的生活

条形码在生活中使用的已经相当广泛了,不管是去书店买书,还是去超市买商品,都会用到条码,而且每一个条码中的信息都不尽相同,每一类的商品都有统一的条 码,当然条码的类型也有不同,比如有标准的UPC条码,也有Code39,Code128,EAN8等等好多好多。这些根据不同的需要而被广泛使用。

      最近根据项目的需求,用了一段时间对一维码、二维码进行了小小的研究,在一篇Blog中看到使用了google的zxing生成二维码,好奇之下自己做了一个小Demo。

生成一维码

生成二维码

代码语言:javascript
复制
#region 引用程序集

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

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;
endregion
namespace qr

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            // 初始化组件

            InitializeComponent();

        }

    //  public static void writeToFile(ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, string file)  
    //{  
    //    Bitmap bmap = toBitmap(matrix);  
    //    bmap.Save(file, format);  
    //}  
    /// <summary>  
    /// 绘制一维码、二维码位图  
    /// </summary>  
    /// <param name="matrix"></param>  
    /// <returns></returns>  
    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_1(object sender, EventArgs e)  
    {  
        string content = textBox1.Text;  
        int codeWidth = Convert.ToInt16(txtWidth.Text.Trim().ToString());  
        int codeHeight = Convert.ToInt16(txtHeight.Text.Trim().ToString());  
        // 生成二维码  
        ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, codeWidth, codeHeight);  
        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);  
        //}  
    }  

    private void button2_Click(object sender, EventArgs e)  
    {  
        string content = textBox1.Text;  
        // 生成一维码  
        ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.EAN_8, 200, 100);  
        Bitmap bitmap = toBitmap(byteMatrix);  
        pictureBox1.Image = bitmap;  

        //Bitmap twobitmap = new Bitmap(bitmap.Width ,bitmap.Height);  
        //Graphics g = Graphics.FromImage(bitmap);  
        //g.DrawString(content, Font, Brushes.Black, pictureBox1.);  
        //pictureBox1.Image = bitmap;  

    }   
}  

}  </pre> 

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档