1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
[DllImport("gdi32")] private static extern int DeleteObject(IntPtr o);
public static Bitmap GetQRCode(string strContent, int iWidth, int iHeigth) { try { 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); hint.Add(EncodeHintType.MARGIN, 1); BitMatrix bitMatrix = writer.encode(strContent, BarcodeFormat.QR_CODE, iWidth, iHeigth, hint); BarcodeWriter barcodeWriter = new BarcodeWriter(); Bitmap bitmapQRCode = barcodeWriter.Write(bitMatrix); int[] rectangle = bitMatrix.getEnclosingRectangle(); Bitmap bitmapQRCodeBMP = new Bitmap(bitmapQRCode.Width, bitmapQRCode.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bitmapQRCodeBMP)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(bitmapQRCode, 0, 0); } return bitmapQRCodeBMP; } catch (Exception ex) { TXTHelper.Logs(ex.ToString()); return null; } }
public static Bitmap GetQRCode_logo(string strContent, int iWidth, int iHeigth, string strLogoPath) { try { 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); hint.Add(EncodeHintType.MARGIN, 1); BitMatrix bitMatrix = writer.encode(strContent, BarcodeFormat.QR_CODE, iWidth, iHeigth, hint); BarcodeWriter barcodeWriter = new BarcodeWriter(); Bitmap bitmapQRCode = barcodeWriter.Write(bitMatrix); int[] rectangle = bitMatrix.getEnclosingRectangle(); Bitmap bitmapQRCodeBMP = new Bitmap(bitmapQRCode.Width, bitmapQRCode.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bitmapQRCodeBMP)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(bitmapQRCode, 0, 0); } Bitmap bitmapLogo = new Bitmap(strLogoPath); int middleW = Math.Min((int)(rectangle[2] / 3.5), bitmapLogo.Width); int middleH = Math.Min((int)(rectangle[3] / 3.5), bitmapLogo.Height); int middleL = (bitmapQRCode.Width - middleW) / 2; int middleT = (bitmapQRCode.Height - middleH) / 2; Graphics myGraphic = Graphics.FromImage(bitmapQRCodeBMP); myGraphic.DrawImage(bitmapLogo, middleL, middleT, middleW, middleH); return bitmapQRCodeBMP; } catch (Exception ex) { TXTHelper.Logs(ex.ToString()); return null; } }
public static ImageSource GetImageSourceFromBitmap(Bitmap bitmapQRCode) { try { IntPtr ipQRCode = bitmapQRCode.GetHbitmap(); BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ipQRCode, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); DeleteObject(ipQRCode); return bitmapSource; } catch (Exception ex) { TXTHelper.Logs(ex.ToString()); return null; } }
public static string BarcodeReader(Bitmap bitmapQRCode) { try { BarcodeReader barcodeReader = new BarcodeReader(); barcodeReader.Options.CharacterSet = "UTF-8"; Result resultQRCode = barcodeReader.Decode(bitmapQRCode); if (resultQRCode == null) { return string.Empty; } else { return resultQRCode.Text; } } catch (Exception ex) { TXTHelper.Logs(ex.ToString()); return null; } }
public static string BarcodeReader(string strQRCodePath) { try { BarcodeReader barcodeReader = new BarcodeReader(); barcodeReader.Options.CharacterSet = "UTF-8"; Bitmap bitmapQRCode = new Bitmap(strQRCodePath); Result resultQRCode = barcodeReader.Decode(bitmapQRCode); if (resultQRCode == null) { return string.Empty; } else { return resultQRCode.Text; } } catch (Exception ex) { TXTHelper.Logs(ex.ToString()); return null; } }
public static bool SaveBitmap(string strSavePath, Bitmap bitmapQRCode) { try { ImageFormat imageFormat; switch (System.IO.Path.GetExtension(strSavePath)) { case ".bmp": imageFormat = ImageFormat.Bmp; break; case ".emf": imageFormat = ImageFormat.Emf; break; case ".exif": imageFormat = ImageFormat.Exif; break; case ".gif": imageFormat = ImageFormat.Gif; break; case ".icon": imageFormat = ImageFormat.Icon; break; case ".jpeg": imageFormat = ImageFormat.Jpeg; break; case ".jpg": imageFormat = ImageFormat.Jpeg; break; case ".memorybmp": imageFormat = ImageFormat.MemoryBmp; break; case ".png": imageFormat = ImageFormat.Png; break; case ".tiff": imageFormat = ImageFormat.Tiff; break; case ".wmf": imageFormat = ImageFormat.Wmf; break; default: imageFormat = ImageFormat.Png; break; } bitmapQRCode.Save(strSavePath, imageFormat); return true; } catch (Exception ex) { TXTHelper.Logs(ex.ToString()); return false; } }
|