简单了解条形码,二维码的生成

时间:2022-11-16 23:12:00

    因为条形码有不同的编码格式,使用时要注意编码格式。首先在 NuGet包管理器中引用下载BarcodeLib.dll文件。BarcodeLib.dll一维条码库,支持以下条码格式:UPC-A,UPC-E,UPC 2 Digit Ext,UPC 5 Digit Ext.,EAN-13,JAN-13,EAN-8,ITF-14,Codabar,PostNet,Bookland/ISBN,Code 11,Code 39,Code 39 Extended,Code 93,LOGMARS,MSI,Interleaved 2 of 5,Standard 2 of 5,Code 128,Code 128-A,Code 128-B,Code 128-C,Telepen ;

要执行的方法下:

System.Drawing.Image image; 
int width = 148, height = 55; 
string fileSavePath = AppDomain.CurrentDomain.BaseDirectory + "BarcodePattern.jpg"; 
if (File.Exists(fileSavePath)) 
  File.Delete(fileSavePath); 
GetBarcode(height, width, BarcodeLib.TYPE.CODE128//编码格式
, "要编码的数据", out image, fileSavePath); 
pictureBox1.Image = Image.FromFile("BarcodePattern.jpg"); 

注意要改的参数   

public static void GetBarcode(int height, int width, BarcodeLib.TYPE type, string code, out System.Drawing.Image image, string fileSaveUrl) 
{ 
  try 
  { 
    image = null; 
    BarcodeLib.Barcode b = new BarcodeLib.Barcode(); 
    b.BackColor = System.Drawing.Color.White;//图片背景颜色 
    b.ForeColor = System.Drawing.Color.Black;//条码颜色 
    b.IncludeLabel = true; 
    b.Alignment = BarcodeLib.AlignmentPositions.LEFT; 
    b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER; 
    b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;//图片格式 
    System.Drawing.Font font = new System.Drawing.Font("verdana", 10f);//字体设置 
    b.LabelFont = font; 
    b.Height = height;//图片高度设置(px单位) 
    b.Width = width;//图片宽度设置(px单位) 
    image = b.Encode(type, code);//生成图片 
    image.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
  } 
  catch (Exception ex) 
  { 
    image = null; 
  } 
} 
简单了解条形码,二维码的生成

借鉴于挣脱生命的束缚:http://www.cnblogs.com/qqhfeng/p/4240597.html
详细的讲解:http://www.barcodelib.com/net_barcode/main.html

 

 

  二维码

适用于WinForm ,需要引用 zxing.dll文件  下载地址:http://pan.baidu.com/s/1ntNr79v

注意要引用的文件,空间
using com.google.zxing;
using COMMON = com.google.zxing.common;

代码如下:

private void BtnBm_Click(object sender, EventArgs e)  //生成二维码
        {
            if (string.IsNullOrEmpty(this.textBox1.Text.Trim()))
            {
                MessageBox.Show("请输入需要转换的信息!");
            }
            else
            {
                string content = this.textBox1.Text;//待编码数据
                try
                {
                    int QSize = Int32.Parse(txtSize.Text);//二维码大小
                    string s = hScrollBar1.Value.ToString("X");//二维码透明度
                    string q = hScrollBar2.Value.ToString("X");//背景透明度
                    string Scolor = "0x" + s + txtYSe.Text;//二维码颜色
                    string Qcolor = "0x" + q + txtBys.Text;//背景颜色
                    COMMON.ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QSize, QSize);
                    Bitmap bt = toBitmap(byteMatrix, Scolor, Qcolor);
                    pictureBox1.Image = bt;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
        public static Bitmap toBitmap(COMMON.ByteMatrix matrix, string scolor, string qcolor)
        {
            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(scolor) : ColorTranslator.FromHtml(qcolor));
                }
            }
            return bmap;
        }private void BtnSave_Click(object sender, EventArgs e) //保存
        {
            Image img = pictureBox1.Image;
            if (img != null)
            {
                SaveFileDialog sFD = new SaveFileDialog();
                sFD.Filter = "*.png|*.png";
                if (sFD.ShowDialog() == DialogResult.OK)
                {
                    Bitmap bmap = new Bitmap(img, img.Width, img.Height);
                    bmap.Save(sFD.FileName);
                    MessageBox.Show("保存成功!");
                }
            }
            else
            {
                MessageBox.Show("您还没有生成二维码!");
            }
        }

        private void BtnJm_Click(object sender, EventArgs e) //读码解码
        {

            if (this.openFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            Bitmap bmap;
            try
            {
                Image img = Image.FromFile(this.openFileDialog1.FileName);
                bmap = new Bitmap(img);
                if (bmap == null)
                {
                    MessageBox.Show("解码错误,请确保二维码图片已打开!");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("解码错误,请确保图片格式正确!");
                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
            {
                string str = "解码失败,失败原因可能是:" + "\n";
                str += "1.您打开的图片非二维码图片!" + "\n";
                str += "2.您打开的二维码图片背景色太深!" + "\n";
                str += "3.您打开的二维码图片二维码和背景色太接近!" + "\n";
                MessageBox.Show(str);
                return;
            }
            textBox1.Text = result.Text;
        }

文章参考:https://www.cnblogs.com/xuhang/p/3831776.html,大家有时间可以去看看!