private void button1_Click(object sender, EventArgs e) { System.Collections.Hashtable hints = new Hashtable(); hints.Add(EncodeHintType.CHARACTER_SET, "utf-8"); com.google.zxing.qrcode.QRCodeWriter qrCodeWriter = new com.google.zxing.qrcode.QRCodeWriter(); //图像数据转换,使用了矩阵转换 ByteMatrix byteMatrix = qrCodeWriter.encode(textBox1.Text, BarcodeFormat.QR_CODE, qr_width, qr_height, hints); //下面这里按照二维码的算法,逐个生成二维码的图片, //两个for循环是图片横列扫描的结果 //uint[] pixels = new uint[qr_width * qr_height]; Bitmap bitmap = new Bitmap(byteMatrix.Width, byteMatrix.Height, PixelFormat.Format64bppArgb); for (int y = 0; y < byteMatrix.Height; y++) { for (int x = 0; x < byteMatrix.Width; x++) { if (byteMatrix.Array[x][y] == -1) { bitmap.SetPixel(x, y, Color.White); } else { bitmap.SetPixel(x, y, Color.Black); } } } pictureBox1.Image = bitmap; }
public static void WriteQRCode(string Path, string URL, int Width, int Height) { /// <summary> /// Example usage /// BLL.QRCode.WriteQRCode("D:\\vessea.jpg", "http://www.vessea.com", 300, 300); /// </summary> com.google.zxing.qrcode.QRCodeWriter qrCode = new com.google.zxing.qrcode.QRCodeWriter(); com.google.zxing.common.ByteMatrix byteIMG = qrCode.encode(URL, com.google.zxing.BarcodeFormat.QR_CODE, Width, Height); sbyte[][] img = byteIMG.Array; Bitmap bmp = new Bitmap(Width, Height); Graphics g = Graphics.FromImage(bmp); g.Clear(Color.White); for (int i = 0; i <= img.Length - 1; i++) { for (int j = 0; j <= img[i].Length - 1; j++) { if (img[j][i] == 0) { g.FillRectangle(Brushes.Black, i, j, 1, 1); } else { g.FillRectangle(Brushes.White, i, j, 1, 1); } } } bmp.Save(Path, ImageFormat.Jpeg); }
private void button1_Click(object sender, EventArgs e) { System.Collections.Hashtable hints =new Hashtable(); hints.Add(EncodeHintType.CHARACTER_SET,"utf-8"); com.google.zxing.qrcode.QRCodeWriter qrCodeWriter = new com.google.zxing.qrcode.QRCodeWriter(); //图像数据转换,使用了矩阵转换 ByteMatrix byteMatrix = qrCodeWriter.encode(textBox1.Text, BarcodeFormat.QR_CODE,qr_width, qr_height, hints); //下面这里按照二维码的算法,逐个生成二维码的图片, //两个for循环是图片横列扫描的结果 //uint[] pixels = new uint[qr_width * qr_height]; Bitmap bitmap = new Bitmap(byteMatrix.Width, byteMatrix.Height, PixelFormat.Format64bppArgb); for (int y = 0; y < byteMatrix.Height; y++) { for (int x = 0; x < byteMatrix.Width; x++) { if (byteMatrix.Array[x][y]==-1) { bitmap.SetPixel(x, y, Color.White); } else { bitmap.SetPixel(x, y, Color.Black); } } } pictureBox1.Image = bitmap; }
private void btnReceive_Click(object sender, RoutedEventArgs e) { try { var R = Receive.ReceiveFunds(tbReceiveAdr.Text, tbCallbackUrl.Text); WriteText($"Destination: {R.DestinationAddress} \r\nInput Address: {R.InputAddress} \r\nCallback URL: {R.CallbackUrl} \r\nFee %: {R.FeePercent}"); tbReceiveInput.Text = R.InputAddress; //--=> Generate a QRCode. var x = new com.google.zxing.qrcode.QRCodeWriter().encode(R.InputAddress, com.google.zxing.BarcodeFormat.QR_CODE, 200, 200); imgQRrec.Source = BitmapToImageSource(x.ToBitmap()); } catch (Exception ex) { WriteText($"ERROR: {ex.Message}"); } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public com.google.zxing.common.BitMatrix encode(String contents, BarcodeFormat format, int width, int height, java.util.Map<EncodeHintType,?> hints) throws WriterException public BitMatrix encode(string contents, BarcodeFormat format, int width, int height, IDictionary<EncodeHintType, object> hints) { Writer writer; switch (format) { case com.google.zxing.BarcodeFormat.EAN_8: writer = new EAN8Writer(); break; case com.google.zxing.BarcodeFormat.EAN_13: writer = new EAN13Writer(); break; case com.google.zxing.BarcodeFormat.UPC_A: writer = new UPCAWriter(); break; case com.google.zxing.BarcodeFormat.QR_CODE: writer = new QRCodeWriter(); break; case com.google.zxing.BarcodeFormat.CODE_39: writer = new Code39Writer(); break; case com.google.zxing.BarcodeFormat.CODE_128: writer = new Code128Writer(); break; case com.google.zxing.BarcodeFormat.ITF: writer = new ITFWriter(); break; case com.google.zxing.BarcodeFormat.PDF_417: writer = new PDF417Writer(); break; case com.google.zxing.BarcodeFormat.CODABAR: writer = new CodaBarWriter(); break; default: throw new System.ArgumentException("No encoder available for format " + format); } return writer.encode(contents, format, width, height, hints); }
/// <summary> /// Does the actual drawing of the image. /// </summary> /// <param name="bm"></param> /// <param name="qrcode"></param> internal void DrawImage(ref System.Drawing.Bitmap bm, string qrcode) { com.google.zxing.qrcode.QRCodeWriter writer = new com.google.zxing.qrcode.QRCodeWriter(); com.google.zxing.common.ByteMatrix matrix; Graphics g = null; g = Graphics.FromImage(bm); float mag = PixelConversions.GetMagnification(g, bm.Width, bm.Height, OptimalHeight, OptimalWidth); int barHeight = PixelConversions.PixelXFromMm(g, OptimalHeight * mag); int barWidth = PixelConversions.PixelYFromMm(g, OptimalWidth * mag); matrix = writer.encode(qrcode, com.google.zxing.BarcodeFormat.QR_CODE, barWidth, barHeight, null); bm = new Bitmap(barWidth, barHeight); Color Color = Color.FromArgb(0, 0, 0); for (int y = 0; y < matrix.Height; ++y) { for (int x = 0; x < matrix.Width; ++x) { Color pixelColor = bm.GetPixel(x, y); //Find the colour of the dot if (matrix.get_Renamed(x, y) == -1) { bm.SetPixel(x, y, Color.White); } else { bm.SetPixel(x, y, Color.Black); } } } }
public void write(int paperFormatIndex, String fileName) { PageSize pageSize; paperFormatIndex++; switch (paperFormatIndex) { case (int)PageSize.A0: pageSize = PageSize.A0; break; case (int)PageSize.A1: pageSize = PageSize.A1; break; case (int)PageSize.A2: pageSize = PageSize.A2; break; case (int)PageSize.A3: pageSize = PageSize.A3; break; default: pageSize = PageSize.A4; break; } //Grayscale Map //Image grayscaledMap = MapWithQRCodeWriter.MakeGrayscale(map); // scale map Image scaledMap = this.scaleImageToFitFormat(pageSize, map); this.drawFrame(ref scaledMap); // get location int locationX = Convert.ToInt32(((this.codeSize / 2.0f) / scaledMap.Width) * map.Width); int locationY = Convert.ToInt32(((this.codeSize / 2.0f) / scaledMap.Height) * map.Height); PointLatLng location = MapOverlayForm.Instance.FromLocalToLatLng(locationX, locationY); // get scale int p1X = Convert.ToInt32((92.0f / scaledMap.Width) * map.Width); int p1Y = Convert.ToInt32((92.0f / scaledMap.Width) * map.Width); PointLatLng p1Location = MapOverlayForm.Instance.FromLocalToLatLng(p1X, p1Y); int p2X = Convert.ToInt32((356.0f / scaledMap.Width) * map.Width); int p2Y = Convert.ToInt32((92.0f / scaledMap.Width) * map.Width); PointLatLng p2Location = MapOverlayForm.Instance.FromLocalToLatLng(p2X, p2Y); double scale = MapOverlayForm.Instance.gMapControl.MapProvider.Projection.GetDistance(p1Location, p2Location); // create qr code com.google.zxing.qrcode.QRCodeWriter qrCode = new com.google.zxing.qrcode.QRCodeWriter(); String locationString = location.Lat.ToString() + ";" + location.Lng.ToString() + ";" + scale.ToString().Substring(0, 6); ByteMatrix byteIMG = qrCode.encode(locationString, com.google.zxing.BarcodeFormat.QR_CODE, this.codeSize, this.codeSize); // draw qr code on map image this.drawQRCodeOnImage(byteIMG, ref scaledMap); PdfDocument document = new PdfDocument(); PdfPage page = document.AddPage(); page.Size = pageSize; page.Orientation = PageOrientation.Landscape; XGraphics gfx = XGraphics.FromPdfPage(page); XImage image = XImage.FromGdiPlusImage(scaledMap); XUnit imgHeight = XUnit.FromInch(image.PixelHeight / image.VerticalResolution); XUnit imgWidth = XUnit.FromInch(image.PixelWidth / image.HorizontalResolution); double width = image.PixelWidth * 72 / image.HorizontalResolution; double height = image.PixelHeight * 72 / image.VerticalResolution; gfx.DrawImage(image, (page.Width.Point - imgWidth.Point) / 2.0, (page.Height.Point - imgHeight.Point) / 2.0, width, height); try { document.Save(fileName); } catch (IOException) { System.Windows.Forms.MessageBox.Show("Could not save the file. Maybe the file is used by another program. ", "IO Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); } document.Dispose(); }
public void ProcessRequest(HttpContext context) { string URL = HttpContext.Current.Request.Url.Host.ToString(); int Width = 200; int Height = 200; com.google.zxing.qrcode.QRCodeWriter qrCode = new com.google.zxing.qrcode.QRCodeWriter(); com.google.zxing.common.ByteMatrix byteIMG = qrCode.encode(URL, com.google.zxing.BarcodeFormat.QR_CODE, Width, Height); sbyte[][] img = byteIMG.Array; Bitmap bmp = new Bitmap(Width, Height); Graphics g = Graphics.FromImage(bmp); g.Clear(Color.White); for (int i = 0; i <= img.Length - 1; i++) { for (int j = 0; j <= img[i].Length - 1; j++) { if (img[j][i] == 0) { g.FillRectangle(Brushes.Black, i, j, 1, 1); } else { g.FillRectangle(Brushes.White, i, j, 1, 1); } } } /// <summary> /// Returns the QRCode in the browser /// </summary> HttpContext.Current.Response.ContentType = "image/jpg"; bmp.Save(HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg); }