/// <summary> /// This method detects a code in a "pure" image -- that is, pure monochrome image /// which contains only an unrotated, unskewed, image of a code, with some white border /// around it. This is a specialized method that works exceptionally fast in this special /// case. /// /// @see com.google.zxing.pdf417.PDF417Reader#extractPureBits(BitMatrix) /// @see com.google.zxing.datamatrix.DataMatrixReader#extractPureBits(BitMatrix) /// @see com.google.zxing.qrcode.QRCodeReader#extractPureBits(BitMatrix) /// </summary> private static BitMatrix extractPureBits(BitMatrix image) { int[] enclosingRectangle = image.getEnclosingRectangle(); if (enclosingRectangle == null) { return null; } int left = enclosingRectangle[0]; int top = enclosingRectangle[1]; int width = enclosingRectangle[2]; int height = enclosingRectangle[3]; // Now just read off the bits BitMatrix bits = new BitMatrix(MATRIX_WIDTH, MATRIX_HEIGHT); for (int y = 0; y < MATRIX_HEIGHT; y++) { int iy = top + (y * height + height / 2) / MATRIX_HEIGHT; for (int x = 0; x < MATRIX_WIDTH; x++) { int ix = left + (x * width + width / 2 + (y & 0x01) * width / 2) / MATRIX_WIDTH; if (image[ix, iy]) { bits[x, y] = true; } } } return bits; }
/// <summary> /// 生成带Logo的二维码 /// </summary> /// <param name="text">文本内容</param> public static void Generate(string text, string LogoPth, string serverPth, string filename, ImgType type, /* ImageFormat imgFrt,*/ int?width = null, int?hight = null) { try { //Logo 图片 Bitmap logo = new Bitmap(LogoPth); //构造二维码写码器 MultiFormatWriter writer = new MultiFormatWriter(); Dictionary <EncodeHintType, object> hint = new Dictionary <EncodeHintType, object>(); hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); int w = 300; int h = 300; if (width != null) { w = (int)width; } if (hight != null) { h = (int)hight; } //生成二维码 ZXing.Common.BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, w, h, hint); BarcodeWriter barcodeWriter = new BarcodeWriter(); Bitmap map = barcodeWriter.Write(bm); //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸) int[] rectangle = bm.getEnclosingRectangle(); //计算插入图片的大小和位置 int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width); int middleH = Math.Min((int)(rectangle[3] / 3.5), logo.Height); int middleL = (map.Width - middleW) / 2; int middleT = (map.Height - middleH) / 2; //将img转换成bmp格式,否则后面无法创建Graphics对象 Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmpimg)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(map, 0, 0); } //将二维码插入图片 Graphics myGraphic = Graphics.FromImage(bmpimg); //白底 myGraphic.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH); myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH); string suffix = ImgSuffix.Getsuffix(type);; //后缀 if (!Directory.Exists(serverPth)) { Directory.CreateDirectory(serverPth); } //保存成图片 bmpimg.Save($"{serverPth}{filename}{suffix}"); } catch (Exception ex) { Console.WriteLine(ex.Message); } }