`
hzy3774
  • 浏览: 993889 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论

C#利用zxing.net操作二维码和条形码

 
阅读更多

下载地址:http://zxingnet.codeplex.com/

zxing.net是.net平台下编解条形码和二维码的工具,使用非常方便。

首先下载二进制dll文件,引入工程;

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZXing.QrCode;
using ZXing;
using ZXing.Common;
using ZXing.Rendering;

namespace zxingTest
{
    public partial class Form1 : Form
    {
        EncodingOptions options = null;
        BarcodeWriter writer = null;

        public Form1()
        {
            InitializeComponent();
            options = new QrCodeEncodingOptions
            {
                DisableECI = true,
                CharacterSet = "UTF-8",
                Width = pictureBoxQr.Width,
                Height = pictureBoxQr.Height
            };
            writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options = options;
        }

        private void buttonQr_Click(object sender, EventArgs e)
        {
            if (textBoxText.Text == string.Empty)
            {
                MessageBox.Show("输入内容不能为空!");
                return;
            }
            Bitmap bitmap = writer.Write(textBoxText.Text);
            pictureBoxQr.Image = bitmap;
        }
    }
}

 效果:



 将字符编码时可以指定字符格式;默认为ISO-8859-1英文字符集,但一般移动设备常用UTF-8字符集编码,

可以通过QrCodeEncodingOptions设置编码方式。

如果要生成其他zxing支持的条形码,只要修改BarcodeWriter.Format就可以了。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZXing.QrCode;
using ZXing;
using ZXing.Common;
using ZXing.Rendering;

namespace zxingTest
{
    public partial class Form1 : Form
    {
        EncodingOptions options = null;
        BarcodeWriter writer = null;

        public Form1()
        {
            InitializeComponent();
            options = new EncodingOptions
            {
                //DisableECI = true,
                //CharacterSet = "UTF-8",
                Width = pictureBoxQr.Width,
                Height = pictureBoxQr.Height
            };
            writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.ITF;
            writer.Options = options;
        }

        private void buttonQr_Click(object sender, EventArgs e)
        {
            if (textBoxText.Text == string.Empty)
            {
                MessageBox.Show("输入内容不能为空!");
                return;
            }
            Bitmap bitmap = writer.Write(textBoxText.Text);
            pictureBoxQr.Image = bitmap;
        }
    }
}

 

效果:


 输入字符串需要符合编码的格式,不然会报错。
解码方式:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZXing.QrCode;
using ZXing;
using ZXing.Common;
using ZXing.Rendering;
  
namespace zxingTest
{
    public partial class Form1 : Form
    {
        BarcodeReader reader = null;
  
        public Form1()
        {
            InitializeComponent();
            reader = new BarcodeReader();
        }
  
        private void Form1_DragEnter(object sender, DragEventArgs e)//当拖放进入窗体
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;    //显示拷贝效应
            else
                e.Effect = DragDropEffects.None;  
        }
  
        private void Form1_DragDrop(object sender, DragEventArgs e) //当拖放放在窗体上
        {
            string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop, false); //获取文件名
            if (fileNames.Length > 0)
            {
                pictureBoxPic.Load(fileNames[0]);   //显示图片
                Result result = reader.Decode((Bitmap)pictureBoxPic.Image); //通过reader解码
                textBoxText.Text = result.Text; //显示解析结果
            }
        }
    }
}
 

 
  • 大小: 30.9 KB
  • 大小: 29.7 KB
  • 大小: 25.7 KB
分享到:
评论
11 楼 wck173168 2016-04-11  
请问如何调用摄像头,谢谢
10 楼 calosteward 2015-06-17  
谢了
经常看到的都是Zxing barcode 和java相关的教程。很少看到.net相关的,非常感谢你的分享。
________________
.net barcode generation
9 楼 hzy3774 2015-01-09  
abcba123 写道
请问是要把zxing。dll加入reference?还是其他。dll?

zxing.dll即可
8 楼 abcba123 2015-01-09  
请问是要把zxing。dll加入reference?还是其他。dll?
7 楼 itzenghaixiong 2014-05-07  
生成的二维码外面会有白色的边框,怎么把这些白色边框去掉
6 楼 恩铭-杨 2014-03-17  
我是看不懂滴,只会写AU3的东东!但是AU3没有脱机的二维码编译。55555555
5 楼 恩铭-杨 2014-03-17  
我睡不着觉的时候,找来的!
4 楼 恩铭-杨 2014-03-17  
using com.google.zxing;
using com.google.zxing.qrcode;
using com.google.zxing.qrcode.decoder;
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;

public class b : Form
{
    private IContainer a;
    private Button b;
    private TextBox c;
    private PictureBox d;
    private Button e;
    private SaveFileDialog f;
    private TextBox g;
    private TextBox h;
    private Label i;
    private Label j;
    private Label k;

    public b()
    {
        this.a();
    }

    private void a()
    {
        this.b = new Button();
        this.c = new TextBox();
        this.d = new PictureBox();
        this.e = new Button();
        this.f = new SaveFileDialog();
        this.g = new TextBox();
        this.h = new TextBox();
        this.i = new Label();
        this.j = new Label();
        this.k = new Label();
        ((ISupportInitialize) this.d).BeginInit();
        base.SuspendLayout();
        this.b.Location = new Point(0x1a0, 0x42);
        this.b.Name = "button1";
        this.b.Size = new Size(0x77, 0x2a);
        this.b.TabIndex = 0;
        this.b.Text = "生成二维码(&C)";
        this.b.UseVisualStyleBackColor = true;
        this.b.Click += new EventHandler(this.b);
        this.c.Location = new Point(120, 30);
        this.c.MaxLength = 0x400;
        this.c.Name = "textBox1";
        this.c.Size = new Size(0x12d, 0x15);
        this.c.TabIndex = 1;
        this.c.Text = "http://blog.csdn.net/kimmking";
        this.d.BackColor = Color.White;
        this.d.Location = new Point(30, 0x77);
        this.d.Name = "pictureBox1";
        this.d.Size = new Size(0x200, 0x200);
        this.d.TabIndex = 2;
        this.d.TabStop = false;
        this.e.Location = new Point(450, 0x16);
        this.e.Name = "button2";
        this.e.Size = new Size(0x55, 0x22);
        this.e.TabIndex = 3;
        this.e.Text = "保存(&S)";
        this.e.UseVisualStyleBackColor = true;
        this.e.Click += new EventHandler(this.a);
        this.f.Filter = "bmp文件|*.bmp|jpg文件|*.jpg|所有文件|*.*";
        this.f.Title = "保存二维码图片";
        this.f.FileOk += new CancelEventHandler(this.a);
        this.g.Location = new Point(0x7b, 0x4b);
        this.g.MaxLength = 20;
        this.g.Name = "textBox2";
        this.g.Size = new Size(0x62, 0x15);
        this.g.TabIndex = 4;
        this.g.Text = "我的博客";
        this.h.Location = new Point(0x121, 0x4b);
        this.h.MaxLength = 8;
        this.h.Name = "textBox3";
        this.h.Size = new Size(0x62, 0x15);
        this.h.TabIndex = 5;
        this.h.Text = "KK";
        this.i.AutoSize = true;
        this.i.Location = new Point(0x2b, 0x21);
        this.i.Name = "label1";
        this.i.Size = new Size(0x4d, 12);
        this.i.TabIndex = 6;
        this.i.Text = "网址或内容:";
        this.j.AutoSize = true;
        this.j.Location = new Point(0x2b, 0x4f);
        this.j.Name = "label2";
        this.j.Size = new Size(0x29, 12);
        this.j.TabIndex = 7;
        this.j.Text = "标题:";
        this.k.AutoSize = true;
        this.k.Location = new Point(0xf2, 0x4f);
        this.k.Name = "label3";
        this.k.Size = new Size(0x29, 12);
        this.k.TabIndex = 8;
        this.k.Text = "标题:";
        base.AutoScaleDimensions = new SizeF(6f, 12f);
        base.AutoScaleMode = AutoScaleMode.Font;
        base.ClientSize = new Size(0x23c, 0x291);
        base.Controls.Add(this.k);
        base.Controls.Add(this.j);
        base.Controls.Add(this.i);
        base.Controls.Add(this.h);
        base.Controls.Add(this.g);
        base.Controls.Add(this.e);
        base.Controls.Add(this.d);
        base.Controls.Add(this.c);
        base.Controls.Add(this.b);
        base.Name = "Form1";
        this.Text = "彩色二维码生成器";
        ((ISupportInitialize) this.d).EndInit();
        base.ResumeLayout(false);
        base.PerformLayout();
    }

    private static Bitmap a(int A_0, int A_1)
    {
        Bitmap image = new Bitmap(A_0, A_1, PixelFormat.Format32bppArgb);
        Rectangle rect = new Rectangle(0, 0, A_0, A_1);
        LinearGradientBrush brush = new LinearGradientBrush(rect, Color.FromArgb(230, 0x23, 0xa9, 160), Color.FromArgb(0xff, 8, 60, 0x63), LinearGradientMode.Vertical);
        Graphics graphics = Graphics.FromImage(image);
        graphics.FillRectangle(brush, rect);
        graphics.Dispose();
        return image;
    }

    private void a(object A_0, CancelEventArgs A_1)
    {
    }

    private void a(object A_0, EventArgs A_1)
    {
        if (this.d.Image == null)
        {
            MessageBox.Show("请先生成二维码!!!", "图片保存失败");
        }
        else if (this.f.ShowDialog() == DialogResult.OK)
        {
            string filename = this.f.FileName.ToString();
            this.d.Image.Save(filename);
            MessageBox.Show("成功保存图片到" + filename + "!", "图片保存成功");
        }
    }

    private void b(object A_0, EventArgs A_1)
    {
        string text = this.c.Text;
        if (text.Length <
        {
            text = "http://blog.csdn.net/kimmking";
        }
        QRCodeWriter writer = new QRCodeWriter();
        Hashtable hints = new Hashtable();
        hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.Add(EncodeHintType.VERSION_START, 5);
        Bitmap image = writer.encode(text, BarcodeFormat.QR_CODE, 0x200, 0x200, hints).ToBitmap();
        Bitmap bitmap2 = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb);
        Graphics graphics = Graphics.FromImage(bitmap2);
        graphics.SmoothingMode = SmoothingMode.AntiAlias;
        graphics.DrawImage(image, 0, 0);
        image.Dispose();
        SizeF ef = new SizeF();
        Bitmap bitmap3 = a(bitmap2.Width, bitmap2.Height);
        Color color = Color.FromArgb(200, 0xe0, 0x72, 1);
        int num = 140;
        try
        {
            num -= (Encoding.UTF8.GetBytes(text).Length - 20) / 2;
        }
        catch (Exception)
        {
        }
        int num2 = num;
        int num3 = num2;
        for (int i = 0; i < bitmap2.Width; i++)
        {
            for (int j = 0; j < bitmap2.Height; j++)
            {
                Color color3;
                Color pixel = bitmap2.GetPixel(i, j);
                if ((i < num2) && (j < num3))
                {
                    color3 = ((pixel.A == 0xff) && (pixel.B == 0)) ? color : pixel;
                }
                else
                {
                    color3 = ((pixel.A == 0xff) && (pixel.B == 0)) ? bitmap3.GetPixel(i, j) : pixel;
                }
                bitmap2.SetPixel(i, j, color3);
            }
        }
        bitmap3.Dispose();
        string str2 = this.g.Text;
        if (str2.Length < 1)
        {
            str2 = "我的博客";
        }
        float emSize = 32f;
        emSize -= (str2.Length - 4) * 1.8f;
        Font font = new Font("微软雅黑", emSize, FontStyle.Bold);
        ef = graphics.MeasureString(str2, font);
        float num7 = (bitmap2.Width - ef.Width) / 2f;
        Brush brush = new SolidBrush(Color.FromArgb(0xff, 0x3a, 0xb2, 0xc2));
        Brush brush2 = new SolidBrush(Color.White);
        int y = 50;
        graphics.FillRectangle(brush2, new Rectangle((int) num7, y, (int) ef.Width, (int) ef.Height));
        graphics.DrawString(str2, font, brush, (float) ((int) num7), (float) y);
        Brush brush3 = new SolidBrush(Color.FromArgb(0xff, 0x3a, 0xb2, 0xc2));
        int width = 140;
        graphics.FillEllipse(brush2, (bitmap2.Width - width) / 2, (bitmap2.Height - width) / 2, width, width);
        int num10 = 0x80;
        graphics.FillEllipse(brush3, (bitmap2.Width - num10) / 2, (bitmap2.Height - num10) / 2, num10, num10);
        int num11 = 110;
        graphics.FillEllipse(brush2, (bitmap2.Width - num11) / 2, (bitmap2.Height - num11) / 2, num11, num11);
        str2 = this.h.Text;
        if (str2.Length < 1)
        {
            str2 = "KK";
        }
        float num12 = 32f;
        num12 -= (str2.Length - 3) * 3.5f;
        Font font2 = new Font("Meiryo", num12, FontStyle.Bold);
        ef = graphics.MeasureString(str2, font2);
        float x = ((bitmap2.Width - ef.Width) / 2f) + 2f;
        float num14 = ((bitmap2.Height - ef.Height) / 2f) + 8f;
        graphics.DrawString(str2, font2, brush3, x, num14);
        graphics.Dispose();
        if (this.d.Image != null)
        {
            this.d.Image.Dispose();
        }
        this.d.Height = bitmap2.Height;
        this.d.Width = bitmap2.Width;
        this.d.Image = bitmap2;
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing && (this.a != null))
        {
            this.a.Dispose();
        }
        base.Dispose(disposing);
    }
}

3 楼 nightwolfzk 2013-12-17  
2楼的工作QQ号2043443204
2 楼 nightwolfzk 2013-12-17  
你好,我的需求是需要设置颜色,网上找的好多貌似都和google最新的DLL不对应,类和方法都没有。求赐教
1 楼 zhucx1150779 2013-12-09  
你好,我的需求是要加条形码,一维的,为什么程序会报错,有时间能帮我解答一下么 QQ 244145518 非常感谢

相关推荐

    C#使用ZXing.Net生成二维码和条形码,包含识别条码内容

    C#使用ZXing.Net生成二维码和条形码,可以输入的内容生成条码,可以设置条码的宽度和高度,以及各种条码类型,还可以通过拖拽直接识别条码内容 具体操作见:...

    C#利用zxing.net生成二维码和条形码并实现打印的功能

    利用zxing.dll生成和解码二维码。zxing.dll的简单应用,大家可以用反编译工具查看源码,非常方便就只有一个dll文件直接饮用就可以使用!

    C#利用Zxing.net生成条形码和二维码并实现打印,解码

    在本文中,我们将深入探讨如何使用C#编程语言结合Zxing.Net库来生成条形码和二维码,并且实现它们的打印和解码功能。Zxing.Net是一个强大的开源库,它为.NET开发者提供了读取和生成多种条形码和二维码的能力。在...

    C#利用Zxing.net生成条形码和二维码并实现打印

    Zxing

    C# ZXing.Net 生成二维码支持中文Demo

    它的.NET版本ZXing.Net由Michael Monschau维护,提供了对多种条码和二维码格式的支持,包括中文字符的二维码生成。 要使用ZXing.Net在C#中生成包含中文的二维码,首先需要在项目中引入ZXing.Net库。这可以通过NuGet...

    ZXing.NET C#使用ZXing识别二维码,条形码

    ZXing.NET,全称为“Zebra Crossing for .NET”,是一个基于C#的开源库,用于读取和写入多种一维和二维条码,包括二维码(QR Code)和常见的条形码格式,如EAN、UPC、Code 128等。这个库是ZXing(Java版本)的.NET...

    利用ZXing .Net库二维码操作的C#例程

    在本文中,我们将深入探讨如何在C#环境中利用ZXing.Net库进行二维码操作。ZXing,也称为Zebra Crossing,是一个强大的开源项目,主要用于一维和二维条码的图像处理。该库最初由Java编写,但已经发展出多个语言的端口...

    C#使用ZXing识别二维码,条形码

    在C#中使用ZXing.NET进行二维码和条形码识别,主要涉及以下几个步骤: 1. **引入ZXing.NET库** 首先,你需要在你的项目中引入ZXing.NET库。这可以通过NuGet包管理器完成,搜索"ZXing.Net"并安装到你的项目中。 2....

    003.VS2017用C#使用ZXing.Net生成二维码和条形码,包含识别条码内容.rar

    在本项目中,我们主要探讨如何使用C#编程语言结合ZXing.Net库,在Visual Studio 2017中实现二维码和条形码的生成与识别功能。ZXing.Net,也称为 BarcodeReader 和 BarcodeWriter,是一个跨平台的开源库,它为多种...

    zxing.net 最新版生成二维码,读取二维码,读取多个二维码。

    ZXing.Net,全称为ZXing .NET,是一个用于.NET Framework、.NET Core和Unity的开源条形码和二维码读取与生成库。该库是ZXing(Zebra Crossing)项目的.NET实现,ZXing是一个跨平台的Java库,专门用于处理各种条形码...

    C# 利用ZXing.Net来生成条形码和二维码

    本文是利用ZXing.Net在WinForm中生成条形码,二维码的小例子,仅供学习分享使用,如有不足之处,还请指正。 什么是ZXing.Net? ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到...

    ZXing.Net.0.14.0.0下载

    ZXing.Net是一个C#实现的二维码和条形码读取与生成库,版本为0.14.0.0。这个库是ZXing(Zebra Crossing)的.NET移植版,ZXing是一个开源Java库,用于多种一维和二维条码的读取。ZXing.Net允许.NET开发者在他们的应用...

    ZXing.net类库,亲测有用

    ZXing.Net类库,全称为ZXing.Net Barcode Reader,是一个基于.NET Framework的开源项目,用于读取和生成一维条形码和二维条形码,包括二维码(QR Code)。这个库由Michael Agner开发,其设计目标是为.NET开发者提供...

    C#利用Zxing.dll识别图片条形码

    ZXing,全称“Zebra Crossing”,是一个开源的、多平台的条形码和二维码读取库,支持多种格式,如QR码、Code 128、EAN-13等。 首先,我们来看标题提到的"C#利用Zxing.dll识别图片条形码"。这涉及到以下关键知识点:...

    一款基于ZXing.net的条码打印软件,输入对应的值,打印出物料条码标签

    ZXing.net是这个库的.NET版本,为C#和.NET开发者提供了便利,使得在.NET环境中处理条码和二维码变得简单。它支持的编码类型包括但不限于:QR码、Aztec码、PDF417、Data Matrix、UPC-A、EAN-13等。 该软件的亮点在于...

    zxing.net 最新源码 ZXing.Net-master

    ZXing.Net,全称为“Zebra Crossing for .NET”,是一个基于C#的开源条形码和二维码扫描库,适用于.NET Framework、.NET Core以及UWP等平台。该项目源自于ZXing(Zebra Crossing),最初是Java平台上的一个条码识别...

    C#.NET生成普通二维码方法及生成带图片二维码方法源码Demo

    ZXing是一个流行的开源库,支持多种条形码和二维码格式。以下是一个简单的示例,展示如何使用ZXing.Net生成一个包含文本的二维码: 1. 引入库: 首先,需要在项目中引入ZXing.Net库。这可以通过NuGet包管理器完成...

Global site tag (gtag.js) - Google Analytics