public override Bitmap Render(BitMatrix matrix, BarcodeFormat format, string content, EncodingOptions options) { int width = matrix.Width; int height = matrix.Height; var backgroundBrush = new LinearGradientBrush( new Rectangle(0, 0, width, height), BackgroundGradientColor, Background, LinearGradientMode.Vertical); var foregroundBrush = new LinearGradientBrush( new Rectangle(0, 0, width, height), ForegroundGradientColor, Foreground, LinearGradientMode.ForwardDiagonal); var bmp = new Bitmap(width, height); var gg = Graphics.FromImage(bmp); gg.Clear(Background); for (int x = 0; x < width - 1; x++) { for (int y = 0; y < height - 1; y++) { if (matrix[x, y]) { gg.FillRectangle(foregroundBrush, x, y, 1, 1); } else { gg.FillRectangle(backgroundBrush, x, y, 1, 1); } } } return bmp; }
private void Button_Click(object sender, RoutedEventArgs e) { int a = tbctrl.SelectedIndex; if (a == 0) { if (txt != null & txt != "") { try { var bw = new ZXing.BarcodeWriter(); var encOptions = new ZXing.Common.EncodingOptions() { Width = Convert.ToInt32(hw), Height = Convert.ToInt32(hw), Margin = 0 }; bw.Options = encOptions; bw.Format = ZXing.BarcodeFormat.QR_CODE; //string text = "BEGIN:VCARD VERSION:3.0 N:Derlysh;Ilya;V TITLE:Chief of information security TEL;TYPE=work:+4956835753 TEL;TYPE=cell:+79039705658 EMAIL;TYPE=INTERNET:[email protected] END:VCARD"; var result = new Bitmap(bw.Write(txt)); System.Windows.Controls.Image i = new System.Windows.Controls.Image(); result.SetResolution(Convert.ToInt32(dpi), Convert.ToInt32(dpi)); result.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + nm + ".jpg"); MessageBox.Show("Выполнено!"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } else { MessageBox.Show("Нет данных!"); } } else { try { var bw = new ZXing.BarcodeWriter(); var encOptions = new ZXing.Common.EncodingOptions() { Width = Convert.ToInt32(hw), Height = Convert.ToInt32(hw), Margin = 0 }; bw.Options = encOptions; bw.Format = ZXing.BarcodeFormat.QR_CODE; //string text = "BEGIN:VCARD VERSION:3.0 N:Derlysh;Ilya;V TITLE:Chief of information security TEL;TYPE=work:+4956835753 TEL;TYPE=cell:+79039705658 EMAIL;TYPE=INTERNET:[email protected] END:VCARD"; bw.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); bw.Options.Hints.Add(EncodeHintType.DISABLE_ECI, true); var result = new Bitmap(bw.Write(card.generate())); System.Windows.Controls.Image i = new System.Windows.Controls.Image(); result.SetResolution(Convert.ToInt32(dpi), Convert.ToInt32(dpi)); result.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + nm + ".jpg"); MessageBox.Show("Выполнено!"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } }
private Texture2D GenerateBarcode(string data, BarcodeFormat format, int width, int height) { // Generate the BitMatrix var encOptions = new ZXing.Common.EncodingOptions { }; encOptions.Hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); BitMatrix bitMatrix = new MultiFormatWriter() .encode(data, format, width, height, encOptions.Hints); // Generate the pixel array Color[] pixels = new Color[bitMatrix.Width * bitMatrix.Height]; int pos = 0; /*for (int y = 0; y < bitMatrix.Height; y++) * { * for (int x = 0; x < bitMatrix.Width; x++) * { * pixels[pos++] = bitMatrix[x, y] ? Color.black : Color.white; * } * }*/ for (int y = bitMatrix.Height - 1; y >= 0; y--) { for (int x = 0; x < bitMatrix.Width; x++) { pixels[pos++] = bitMatrix[x, y] ? Color.black : Color.white; } } // Setup the texture Texture2D tex = new Texture2D(bitMatrix.Width, bitMatrix.Height); tex.SetPixels(pixels); tex.Apply(); return(tex); }
private Bitmap GetQrCode(string input) { Bitmap output = null; try { var bw = new ZXing.BarcodeWriter(); ZXing.Common.EncodingOptions encOptions = new ZXing.Common.EncodingOptions { Width = int.Parse(txtWidth.Text), Height = int.Parse(txtHeight.Text), Margin = int.Parse(txtMargin.Text), PureBarcode = false }; encOptions.Hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); bw.Renderer = new BitmapRenderer(); bw.Options = encOptions; bw.Format = ZXing.BarcodeFormat.QR_CODE; Bitmap bmp = bw.Write(input); if (cbCustomFooter.Checked && Footer != null) { bmp = AppendImageFooter(bmp, Footer); } if (CheckQR(bmp, input)) { output = bmp; } else if (cbCustomFooter.Checked) { MessageBox.Show("Try again with a lower opacity logo!", "Error"); } } catch { } return(output); }
/// <summary> /// Renders the specified matrix. /// </summary> /// <param name="matrix">The matrix.</param> /// <param name="format">The format.</param> /// <param name="content">The content.</param> /// <param name="options">The options.</param> /// <returns></returns> public PixelData Render(BitMatrix matrix, BarcodeFormat format, string content, EncodingOptions options) { int width = matrix.Width; int heigth = matrix.Height; bool outputContent = (options == null || !options.PureBarcode) && !String.IsNullOrEmpty(content) && (format == BarcodeFormat.CODE_39 || format == BarcodeFormat.CODE_128 || format == BarcodeFormat.EAN_13 || format == BarcodeFormat.EAN_8 || format == BarcodeFormat.CODABAR || format == BarcodeFormat.ITF || format == BarcodeFormat.UPC_A || format == BarcodeFormat.MSI || format == BarcodeFormat.PLESSEY); int emptyArea = outputContent ? 16 : 0; int pixelsize = 1; if (options != null) { if (options.Width > width) { width = options.Width; } if (options.Height > heigth) { heigth = options.Height; } // calculating the scaling factor pixelsize = width / matrix.Width; if (pixelsize > heigth / matrix.Height) { pixelsize = heigth / matrix.Height; } } var pixels = new byte[width * heigth * 4]; var index = 0; for (int y = 0; y < matrix.Height - emptyArea; y++) { for (var pixelsizeHeight = 0; pixelsizeHeight < pixelsize; pixelsizeHeight++) { for (var x = 0; x < matrix.Width; x++) { var color = matrix[x, y] ? Foreground : Background; for (var pixelsizeWidth = 0; pixelsizeWidth < pixelsize; pixelsizeWidth++) { pixels[index++] = color.B; pixels[index++] = color.G; pixels[index++] = color.R; pixels[index++] = color.A; } } for (var x = pixelsize * matrix.Width; x < width; x++) { pixels[index++] = Background.B; pixels[index++] = Background.G; pixels[index++] = Background.R; pixels[index++] = Background.A; } } } for (int y = matrix.Height * pixelsize - emptyArea; y < heigth; y++) { for (var x = 0; x < width; x++) { pixels[index++] = Background.B; pixels[index++] = Background.G; pixels[index++] = Background.R; pixels[index++] = Background.A; } } return new PixelData(width, heigth, pixels); }
private void btnEncodeOptions_Click(object sender, EventArgs e) { if (cmbEncoderType.SelectedItem == null) { MessageBox.Show(this, "Please select a barcode format first.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { EncodingOptions options; switch ((BarcodeFormat)cmbEncoderType.SelectedItem) { case BarcodeFormat.QR_CODE: options = new ZXing.QrCode.QrCodeEncodingOptions { Height = picEncodedBarCode.Height, Width = picEncodedBarCode.Width }; break; case BarcodeFormat.PDF_417: options = new ZXing.PDF417.PDF417EncodingOptions { Height = picEncodedBarCode.Height, Width = picEncodedBarCode.Width }; break; case BarcodeFormat.DATA_MATRIX: options = new ZXing.Datamatrix.DatamatrixEncodingOptions { Height = picEncodedBarCode.Height, Width = picEncodedBarCode.Width, SymbolShape = ZXing.Datamatrix.Encoder.SymbolShapeHint.FORCE_SQUARE, MinSize = new Dimension(picEncodedBarCode.Width, picEncodedBarCode.Height) }; break; default: options = new EncodingOptions { Height = picEncodedBarCode.Height, Width = picEncodedBarCode.Width }; break; } var dlg = new EncodingOptionsForm { Options = options }; if (dlg.ShowDialog(this) == DialogResult.OK) { EncodingOptions = dlg.Options; } } catch (Exception exc) { MessageBox.Show(this, exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
// parameters: // strType 39 / 空 / static Stream BuildQrCodeImage(string path) { Hashtable param_table = ParseParameters(path, ',', '=', "url"); string strType = (string)param_table["type"]; string strCode = (string)param_table["code"]; string strWidth = (string)param_table["width"]; string strHeight = (string)param_table["height"]; int nWidth = 200; int nHeight = 200; if (string.IsNullOrEmpty(strWidth) == false) Int32.TryParse(strWidth, out nWidth); if (string.IsNullOrEmpty(strHeight) == false) Int32.TryParse(strHeight, out nHeight); string strCharset = "ISO-8859-1"; bool bDisableECI = false; BarcodeFormat format = BarcodeFormat.QR_CODE; if (strType == "39" || strType == "code_39") { format = BarcodeFormat.CODE_39; strCode = strCode.ToUpper(); // 小写字符会无法编码 } else if (strType == "ean_13") { format = BarcodeFormat.EAN_13; strCode = strCode.ToUpper(); } EncodingOptions options = new QrCodeEncodingOptions { Height = nWidth, // 400, Width = nHeight, // 400, DisableECI = bDisableECI, ErrorCorrection = ErrorCorrectionLevel.L, CharacterSet = strCharset // "UTF-8" }; if (strType == "39" || strType == "code_39" || strType == "ean_13") options = new EncodingOptions { Width = nWidth, // 500, Height = nHeight, // 100, Margin = 10 }; var writer = new BarcodeWriter { // Format = BarcodeFormat.QR_CODE, Format = format, // Options = new EncodingOptions Options = options }; try { MemoryStream result = new MemoryStream(4096); using (var bitmap = writer.Write(strCode)) { bitmap.Save(result, System.Drawing.Imaging.ImageFormat.Png); } result.Seek(0, SeekOrigin.Begin); return result; } catch (Exception ex) { Stream result = BuildTextImage("异常: " + ex.Message, Color.FromArgb(255, Color.DarkRed)); result.Seek(0, SeekOrigin.Begin); return result; } }
/// <summary> /// Set font size to keep all text inside image width/height. /// </summary> public void makeQRCode(ErrorCode errorlevel) { ZXing.QrCode.Internal.ErrorCorrectionLevel EClevel = ZXing.QrCode.Internal.ErrorCorrectionLevel.L; _encodeopt = new EncodingOptions { Height = qrSize.Height, Width = qrSize.Width, Margin = 1, PureBarcode = true }; switch (errorlevel) { case ErrorCode.low: { EClevel = ZXing.QrCode.Internal.ErrorCorrectionLevel.L; break; } case ErrorCode.med: { EClevel = ZXing.QrCode.Internal.ErrorCorrectionLevel.M; break; } case ErrorCode.quart: { EClevel = ZXing.QrCode.Internal.ErrorCorrectionLevel.Q; break; } case ErrorCode.high: { EClevel = ZXing.QrCode.Internal.ErrorCorrectionLevel.H; break; } default: break; } _encodeopt.Hints.Add(EncodeHintType.ERROR_CORRECTION, EClevel); _qrcodegen.Options = _encodeopt; _qrcodegen.Format = BarcodeFormat.QR_CODE; if (String.IsNullOrEmpty(qrData) != true) { outfile = _qrcodegen.Write(qrData); } }
private Bitmap GetQrCode( string text, Image overlay ) { var width = 512; var height = 512; var margin = 0; int MAX_TEXT = 750; if (String.IsNullOrEmpty(text)) { Bitmap blankBitmap = new Bitmap(width, height); return (blankBitmap); } var qrText = text; if (qrText.Length > MAX_TEXT) { qrText = qrText.Substring(0, MAX_TEXT); } var bw = new ZXing.BarcodeWriter(); var encOptions = new ZXing.Common.EncodingOptions { Width = width, Height = height, Margin = margin, PureBarcode = true }; encOptions.Hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q); encOptions.Hints.Add(EncodeHintType.DISABLE_ECI, true); encOptions.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); encOptions.Hints.Add(EncodeHintType.PDF417_COMPACT, true); encOptions.Hints.Add(EncodeHintType.PDF417_COMPACTION, ZXing.PDF417.Internal.Compaction.AUTO); bw.Renderer = new BitmapRenderer(); bw.Options = encOptions; bw.Format = BarcodeFormat.QR_CODE; try { BitMatrix bm = bw.Encode( text ); int[] rectangle = bm.getEnclosingRectangle(); var bmW = rectangle[2]; var bmH = rectangle[3]; //bw.Options.Width = (int) ( bmW * 1.1 ); //bw.Options.Height = (int) ( bmH * 1.1 ); Bitmap barcodeBitmap = bw.Write(qrText); int deltaHeigth = barcodeBitmap.Height - overlay.Height; int deltaWidth = barcodeBitmap.Width - overlay.Width; using (Graphics g = Graphics.FromImage(barcodeBitmap)) { g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.DrawImage(overlay, new Point(deltaWidth / 2, deltaHeigth / 2)); } return (barcodeBitmap); } catch (WriterException) { return (new Bitmap(width, height)); } }
public ActionResult CreateBarcode(String code) { options = new EncodingOptions { //DisableECI = true, //CharacterSet = "UTF-8", Width = 373, Height = 263 }; writer = new BarcodeWriter(); writer.Format = BarcodeFormat.ITF; writer.Options = options; Bitmap bitmap = writer.Write(code); return File(BitmapToBytes(bitmap), "application/x-MS-bmp", "barcode.bmp"); }