Zxing生成二维码,和添加log

 public class QRCodeHelper
    {
        public static Bitmap CreatQRImage(String str)
        {
            // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
            Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
            hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
            hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            hint.Add(EncodeHintType.MARGIN, 0);

            BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 300, 300, hint);

            //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
            //int[] rectangle = matrix.getEnclosingRectangle();

            //int width = matrix.Width;
            //int height = matrix.Height;
            //// 二维矩阵转为一维像素数组,也就是一直横着排了
            //var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            //for (int y = 0; y < height; y++)
            //{
            //    for (int x = 0; x < width; x++)
            //    {
            //        if (matrix[x, y])
            //        {
            //            bitmap.SetPixel(x, y, Color.FromArgb(255, 255, 0, 0));
            //        }
            //        else
            //        {
            //            bitmap.SetPixel(x, y, Color.FromArgb(255, 255, 255, 0));
            //        }

            //    }
            //}

            Color color = Color.FromName("aaa");
            BarcodeWriter writer = new BarcodeWriter();
            writer.Renderer = new BitmapRenderer() { Background = Color.Orange, Foreground = Color.Green };
            Bitmap bitmap = writer.Write(matrix);
            return bitmap;
        }

        /// <summary>
        /// 增加水印
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="logo_type"></param>
        /// <returns></returns>
        public static Bitmap AddWaterMark(Bitmap image,Image watermark)
        {
            Bitmap b = new Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            Graphics g = Graphics.FromImage(b);
            g.Clear(Color.White);
            g.DrawImage(image, 0, 0, image.Width, image.Height);
            int w = image.Width / 4;
            int h = image.Width / 4;
            System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes();
            Point imgPoint = new Point((image.Width - w) / 2, (image.Height - h) / 2);

            g.DrawImage(watermark, new Rectangle(imgPoint.X, imgPoint.Y, w, h), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);

            imageAttributes.Dispose();
            return b;
        }
    }

 

posted @ 2017-08-25 11:54  命有天定  阅读(54)  评论(0)    收藏  举报