示例#1
0
 public Bitmap createBarcode(String barcodeText, BarcodeFormat barcodeFormat, int width, int height)
 {
     if (!string.IsNullOrEmpty(barcodeText))
     {
         try
         {
             ByteMatrix bt = barcodeWriter.Encode(barcodeText, barcodeFormat, width, height);
             return(ConvertByteMatrixToImage(bt));
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
     }
     return(null);
 }
        private static Bitmap CreateCode(string url)
        {
            try
            {
                // create the QR code
                var hints = new Dictionary <EncodeHintType, object>
                {
                    { EncodeHintType.Margin, 6 }
                };
                var writer = new MultiFormatWriter();
                var matrix = writer.Encode(url, BarcodeFormat.QrCode, 256, 256, hints);

                // create a bitmap from the code
                int   h      = matrix.Height;
                int   w      = matrix.Width;
                int[] pixels = new int[h * w];
                for (int i = 0; i < h; i++)
                {
                    int offset = i * w;
                    for (int j = 0; j < w; j++)
                    {
                        pixels[offset + j] = matrix.Get(j, i) ? Color.Black : Color.White;
                    }
                }
                var qrCode = Bitmap.CreateBitmap(w, h, Bitmap.Config.Argb8888);
                qrCode.SetPixels(pixels, 0, w, 0, 0, w, h);

                return(qrCode);
            }
            catch (WriterException)
            {
                // ignored because exception would be thrown from ZXing library.
            }

            return(null);
        }
示例#3
0
        static void Main(string[] args)
        {
            var        b  = BarcodeFormat.QrCode;
            var        g  = Guid.NewGuid().ToString();
            string     t  = "data=" + g;
            ByteMatrix bt = barcodeWriter.Encode(t, b, 150, 150);
            Image      i  = ConvertByteMatrixToImage(bt);

            i.Save(".\\" + g + ".png", ImageFormat.Png);

            Bitmap bitmap = new Bitmap(i);

            Result                   rawResult;
            RGBLuminanceSource       r       = new RGBLuminanceSource(bitmap, 150, 150);
            GlobalHistogramBinarizer x       = new GlobalHistogramBinarizer(r);
            BinaryBitmap             bitmap2 = new BinaryBitmap(x);
            var hints = new System.Collections.Hashtable();
            //hints.Add(DecodeHintType.PossibleFormats, BarcodeFormat.QrCode);
            int count = 0;
            {
                try
                {
                    rawResult = barcodeReader.Decode(bitmap2, hints);
                    if (rawResult != null)
                    {
                        count++;
                        var y = rawResult.Text;
                        var z = rawResult.BarcodeFormat.ToString();
                    }
                }
                catch (ReaderException e)
                {
                    return;
                }
            }
        }