文档章节

PCB Genesis加二维码 实现方式

o
 osc_4nmshwhm
发布于 2018/08/06 20:37
字数 733
阅读 0
收藏 0

使用incam可以很轻松的增加2维码,这里通过另外一种方式玩转二维码的加法,

使用谷歌zxing.dll类库实现,将文字信息转为bitmap点阵后,在Genesis绘制点即可。

 

一.incam增加二维码效果

 

二.通过代码增加方PAD实现效果:

三.代码实现:

    Genesis增加二维码代码

private void addQR()
        {
            CodeHelper qr = new CodeHelper();
            Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
            hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//设置内容编码  
            hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);//错码率  L水平 7 %  M水平 15 %   Q水平 25 %  H水平 30 % 
            hint.Add(EncodeHintType.QR_VERSION, 11);//大小  (V-1)*4 + 21(V是版本号)
            hint.Add(EncodeHintType.MARGIN, 0);//边距
            ZXing.Common.BitMatrix codeArray = qr.enMatrix("at00101ca0", 61, 61, BarcodeFormat.QR_CODE, hint); //at00101ca0为增加二维码内容
            add add_ = new add();
            gPP gp = new gPP();
            gp.width = 500;//PAD大小
            gp.symbols = "s";//PAD形状为方PAD
            for (int x = 0; x < codeArray.Width; x++)
            {
                for (int y = 0; y < codeArray.Height; y++)
                {
                    if (codeArray[x, y])
                    {
                        gp.p.x = x * 0.5;
                        gp.p.y = y * 0.5;
                        add_.pad(gp); //增加方PAD PAD
                    }
                }
            }
        }

 

   zxing.dll重新封装后的二维码类

public class CodeHelper
    {

        public string CHARSET  = "UTF-8";

        public static int SaveTo(Bitmap bitmap, string filepath, string filename)
        {
            try
            {
                bitmap.Save(filepath + filename); return 0;
            }
            catch (System.Exception ex)
            {
                return -1;
            }
        }
        /// <summary>
        ///字符转二维码
        /// </summary>
        /// <param name="content"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="barcodeFormat"></param>
        /// <param name="hints"></param>
        /// <returns></returns>
        public Bitmap encode(string content, int width, int height, BarcodeFormat barcodeFormat, IDictionary<EncodeHintType, object> hints)
        {
            BitMatrix byteMatrix = new MultiFormatWriter().encode(content, barcodeFormat, width, height, hints);
            Bitmap bitmap = toBitmap(byteMatrix);
            return bitmap;
        }

        /// <summary>
        ///字符转二维码
        /// </summary>
        /// <param name="content"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="barcodeFormat"></param>
        /// <param name="hints"></param>
        /// <returns></returns>
        public string  enString(string content, int width, int height, BarcodeFormat barcodeFormat, IDictionary<EncodeHintType, object> hints)
        {
            BitMatrix byteMatrix = new MultiFormatWriter().encode(content, barcodeFormat, width, height, hints);
            return toString(byteMatrix); 
        }
        /// <summary>
        ///字符转二维码
        /// </summary>
        /// <param name="content"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="barcodeFormat"></param>
        /// <param name="hints"></param>
        /// <returns></returns>
        public BitMatrix enMatrix(string content, int width, int height, BarcodeFormat barcodeFormat, IDictionary<EncodeHintType, object> hints)
        {
            BitMatrix byteMatrix = new MultiFormatWriter().encode(content, barcodeFormat, width, height, hints);
            return byteMatrix;
        }
        /// <summary>
        /// 字符转二维码
        /// </summary>
        /// <param name="content"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="barcodeFormat"></param>
        /// <returns></returns>
        public Bitmap encode(string content, int width, int height, BarcodeFormat barcodeFormat)
        {
            BitMatrix byteMatrix = new MultiFormatWriter().encode(content, barcodeFormat, width, height);

            Bitmap bitmap = toBitmap(byteMatrix);
            return bitmap;
        }
        /// <summary>
        /// BitMatrix转Bitmap
        /// </summary>
        /// <param name="matrix"></param>
        /// <returns></returns>
        private static String  toString(BitMatrix matrix)
        {
            StringBuilder bitString = new StringBuilder();

            for (int x = 0; x < matrix.Width; x++)
            {
                bitString.Append("\r\n");
                for (int y = 0; y < matrix.Height; y++)
                {
                    //bitmap.SetPixel(x, y, matrix[x, y] ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));
                    bitString.Append(matrix[x, y] ? "" : "  ");
                }
            }
            return bitString.ToString ();
        }
        /// <summary>
        /// BitMatrix转Bitmap
        /// </summary>
        /// <param name="matrix"></param>
        /// <returns></returns>
        private static Bitmap toBitmap(BitMatrix matrix)
        {
            int width = matrix.Width;
            int height = matrix.Height;
            Bitmap bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    bitmap.SetPixel(x, y, matrix[x, y] ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));
                }
            }
            return bitmap;
        }



        /// <summary>
        /// 字符转二维码并保存
        /// </summary>
        /// <param name="content"></param>
        /// <param name="path"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="barcodeFormat"></param>
        /// <returns></returns>
        public int encodeAndSave(string content, string path, int width, int height, BarcodeFormat barcodeFormat)
        {
            return SaveTo(encode(content, width, height, barcodeFormat), path, "");
        }
        /// <summary>
        /// bmp图片路径转换??
        /// </summary>
        /// <param name="bmppath"></param>
        /// <param name="hints"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static int decode(string bmppath, IDictionary<DecodeHintType, object> hints, out Result result)
        {
            Bitmap bmap = new Bitmap(bmppath);
            BitmapLuminanceSource source = new BitmapLuminanceSource(bmap);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
            result = new MultiFormatReader().decode(bitmap, hints);
            return 0;
        }
    }

 

o
粉丝 0
博文 500
码字总数 0
作品 0
私信 提问
加载中
请先登录后再评论。

暂无文章

Java 获取资源文件路径

1 问题描述 通过源码运行时,一般使用如下方式读取资源文件: String str = "1.jpg"; 资源文件与源码文件放在同一目录下,或者拥有同一父级目录: String str = "a/b/1.jpg"; 这样直接编译...

氷泠
31分钟前
6
0
Linux程序移植到Android上

序言: 由于本人还是比较偏重于先说明原理在说明实际操作步骤,要知其然更要知其所以然,如下图所示: 传统的linux系统中的程序基本都依赖于glibc(至于什么是glibc可以百度去),而右边AOS...

shzwork
43分钟前
22
0
git 为项目设置用户名/邮箱/密码

1.找到项目所在目录下的 .git,进入.git文件夹,然后执行如下命令分别设置用户名和邮箱 git config user.name "Affandi" git config user.email "123333333@qq.com" 然后执行命令查看con......

有时很滑稽
今天
0
0
如何从int转换为String? - How do I convert from int to String?

问题: I'm working on a project where all conversions from int to String are done like this: 我正在一个项目中,所有从int到String转换都是这样完成的: int i = 5;String strI = "" ......

javail
今天
19
0
Vue+Spring Data JPA+MySQL 增查改删

视频讲解: https://www.bilibili.com/video/BV16i4y1G7i2/ 工程概述: 前后端分离,进行简单增查改删(CRUD) 前端使用VUE 后端使用Spring Data JPA 数据库使用MySQL #EmployeeController.jav...

潘文海
今天
13
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部