当前位置: 移动技术网 > IT编程>开发语言>.net > 简单了解条形码,二维码的生成

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

2019年03月21日  | 移动技术网IT编程  | 我要评论

c形包围,橱窗展示,三环新城业主论坛

    因为条形码有不同的编码格式,使用时要注意编码格式。首先在 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; 
  } 
} 

借鉴于挣脱生命的束缚:
详细的讲解:

 

 

  二维码

适用于winform ,需要引用 zxing.dll文件  下载地址:

注意要引用的文件,空间
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;
        }

文章参考:,大家有时间可以去看看!

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网