PCB Genesis加二維碼 實現方式


使用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;
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM