private void btn_File_Click(object sender, System.EventArgs e) { string sRes=""; OpenFileDialog ofn=new OpenFileDialog(); if(ofn.ShowDialog()==DialogResult.OK){ try { Reader barcodeReader= new com.google.zxing.MultiFormatReader(); System.Drawing.Bitmap srcbitmap = new System.Drawing.Bitmap(ofn.FileName.ToString()); // Make a copy of the image in the Bitmap variable //make a grey bitmap of it bmp_util util=new bmp_util(); srcbitmap = util.ConvertToGrayscale(srcbitmap); RGBLuminanceSource source = new RGBLuminanceSource(srcbitmap, srcbitmap.Width, srcbitmap.Height); com.google.zxing.BinaryBitmap bitmap= new com.google.zxing.BinaryBitmap(new HybridBinarizer(source)); com.google.zxing.Result result = barcodeReader.decode(bitmap); System.Console.WriteLine(result.Text); sRes=result.Text; } catch (com.google.zxing.ReaderException zex) { sRes=zex.Message; System.Console.WriteLine(zex.Message); } catch (Exception ex) { sRes=ex.Message; System.Console.WriteLine(ex.Message); } tb.Text=sRes; } }
private void btn_File_Click(object sender, System.EventArgs e) { string sRes = ""; OpenFileDialog ofn = new OpenFileDialog(); if (ofn.ShowDialog() == DialogResult.OK) { try { Reader barcodeReader = new com.google.zxing.MultiFormatReader(); System.Drawing.Bitmap srcbitmap = new System.Drawing.Bitmap(ofn.FileName.ToString()); // Make a copy of the image in the Bitmap variable //make a grey bitmap of it bmp_util util = new bmp_util(); srcbitmap = util.ConvertToGrayscale(srcbitmap); RGBLuminanceSource source = new RGBLuminanceSource(srcbitmap, srcbitmap.Width, srcbitmap.Height); com.google.zxing.BinaryBitmap bitmap = new com.google.zxing.BinaryBitmap(new HybridBinarizer(source)); com.google.zxing.Result result = barcodeReader.decode(bitmap); System.Console.WriteLine(result.Text); sRes = result.Text; } catch (com.google.zxing.ReaderException zex) { sRes = zex.Message; System.Console.WriteLine(zex.Message); } catch (Exception ex) { sRes = ex.Message; System.Console.WriteLine(ex.Message); } tb.Text = sRes; } }
public static String Decode(Image Image) { try { var hints = new Hashtable(); hints.Add(DecodeHintType.TRY_HARDER, true); ArrayList fmts = new ArrayList(); fmts.Add(BarcodeFormat.QR_CODE); hints.Add(DecodeHintType.POSSIBLE_FORMATS, fmts); var Bitmap = new Bitmap(Image); LuminanceSource source = new RGBLuminanceSource(Bitmap, Bitmap.Width, Bitmap.Height); var bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source)); Reader reader = new MultiFormatReader(); Result result = reader.decode(bitmap, hints); String DecodedString = result.Text; Console.WriteLine(DecodedString); return DecodedString; } catch (ReaderException ex) { return ""; } catch (InvalidOperationException ex) { return ""; } }
//解码操作 private void button3_Click(object sender, EventArgs e) { MultiFormatReader mutiReader = new com.google.zxing.MultiFormatReader(); Bitmap img = (Bitmap)Bitmap.FromFile(opFilePath); if (img == null) { return; } LuminanceSource ls = new RGBLuminanceSource(img, img.Width, img.Height); BinaryBitmap bb = new BinaryBitmap(new com.google.zxing.common.HybridBinarizer(ls)); Hashtable hints = new Hashtable(); hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); Result r = mutiReader.decode(bb, hints); txtMsg.Text = r.Text; }
private void BarCodeDetectTimer_Tick(object sender, EventArgs e) { try { Bitmap img = (Bitmap)pictureBox1.Image; Reader reader = new MultiFormatReader(); RGBLuminanceSource source1 = new RGBLuminanceSource(img, img.Width, img.Height); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source1)); Result result = reader.decode(bitmap); textBox1.Text = result.Text; dekodirano = textBox1.Text; BarCodeDetectTimer.Stop(); } catch (Exception) { statusStrip1.Text = "Greska pri dekodiranju"; } }
public string ReadBarcode(Bitmap BarcodeImage) { RGBLuminanceSource BarcodeSource = new RGBLuminanceSource(BarcodeImage, BarcodeImage.Width, BarcodeImage.Height); BinaryBitmap BarcodeImageBin = new BinaryBitmap(new GlobalHistogramBinarizer(BarcodeSource)); MultiFormatReader BarcodeReader = new MultiFormatReader(); try { Result BarcodeDecodeResult; BarcodeDecodeResult = BarcodeReader.decode(BarcodeImageBin); if (BarcodeDecodeResult.Text != null) return BarcodeDecodeResult.Text; else return "NoBarcode"; } catch (Exception e) { return "Fail"; } }
private void Worker() { //iphone 4 : 960 x 640 //iphone 3 : 320 x 480 if (DeviceHardware.Version == DeviceHardware.HardwareVersion.iPhone4) { //picFrame = new RectangleF (0, 146 * 2, 320 * 2, 157 * 2); picFrame = new RectangleF(300, 300, 320 * 2, 157 * 2); } if (hints == null) { var list = new ArrayList (); list.Add (com.google.zxing.BarcodeFormat.EAN_8); list.Add (com.google.zxing.BarcodeFormat.EAN_13); hints = new Hashtable (); hints.Add (com.google.zxing.DecodeHintType.POSSIBLE_FORMATS, list); hints.Add ( com.google.zxing.DecodeHintType.NEED_RESULT_POINT_CALLBACK, new ResultCallBack (this) ); } if (_multiFormatOneDReader == null) { _multiFormatOneDReader = new com.google.zxing.oned.MultiFormatOneDReader (hints); } // Capturing screen image using (var screenImage = CGImage.ScreenImage.WithImageInRect(new RectangleF(0,0,640,960))) { UIImage image = UIImage.FromImage(screenImage); var srcbitmap = new System.Drawing.Bitmap (image); LuminanceSource source = null; BinaryBitmap bitmap = null; try { source = new RGBLuminanceSource(srcbitmap, (int)image.Size.Width, (int)image.Size.Height); bitmap = new BinaryBitmap (new HybridBinarizer (source)); Reader barcodeReader = new MultiFormatReader(); Result bresult = null; try { bresult = barcodeReader.decode(bitmap);} catch(Exception ex){ //Console.Write(ex.ToString()); } if(bresult != null) { var a = bresult.BarcodeFormat.Name; BeepOrVibrate (); _parentViewController.BarCodeScanned (bresult); } // QRCodeReader barcodeReader = new QRCodeReader (); // source = new RGBLuminanceSource ( // srcbitmap, // (int)screenImage.Width, // (int)screenImage.Height // ); // bitmap = new BinaryBitmap (new HybridBinarizer (source)); // Result result = null; // try { // result = barcodeReader.decode (bitmap); // } catch (Exception ex) { // Console.Write ("Result error: " + ex.ToString ()); // } // if (result == null) { // Console.Write ("result is null."); // } else { // Console.Write (result.BarcodeFormat); // } com.google.zxing.common.BitArray row = new com.google.zxing.common.BitArray (screenImage.Width); int middle = screenImage.Height >> 1; int rowStep = System.Math.Max (1, screenImage.Height >> (4)); for (int x = 0; x < 9; x++) { // Scanning from the middle out. Determine which row we're looking at next: int rowStepsAboveOrBelow = (x + 1) >> 1; bool isAbove = (x & 0x01) == 0; // i.e. is x even? int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : - rowStepsAboveOrBelow); if (rowNumber < 0 || rowNumber >= screenImage.Height) { // Oops, if we run off the top or bottom, stop break; } // Estimate black point for this row and load it: try { row = bitmap.getBlackRow (rowNumber, row); var resultb = _multiFormatOneDReader.decodeRow (rowNumber, row, hints); if (resultb.Text != null) { BeepOrVibrate (); _parentViewController.BarCodeScanned (resultb); break; } else { continue; } } catch (ReaderException re) { continue; } } // var result = _barcodeReader.decodeWithState(bitmap); // // if(result.Text!=null) // { // _multiFormatOneDReader = null; // BeepOrVibrate(); // _parentViewController.BarCodeScanned(result); // } } catch (Exception ex) { Console.WriteLine (ex.Message); } finally { if (bitmap != null) bitmap = null; if (source != null) source = null; if (srcbitmap != null) srcbitmap = null; } } }
private string DecodeImage(Image image) { //image = image.Substring("data:image/png;base64,".Length); //var buffer = Convert.FromBase64String(image); //// TODO: I am saving the image on the hard disk but //// you could do whatever processing you want with it //string documentName = DateTime.Now.ToString("yyyyMMddHHmmssfff")+".png"; //System.IO.File.WriteAllBytes(Server.MapPath("~/app_data/"+documentName), buffer); //byte[] bytes = Convert.FromBase64String(image); //Image _image; //using (MemoryStream ms = new MemoryStream(bytes)) //{ // _image = Image.FromStream(ms); //} Result result = null; try { Reader reader = new com.google.zxing.MultiFormatReader(); Bitmap image1 = new Bitmap(image); LuminanceSource source = new RGBLuminanceSource(image1, image1.Width, image1.Height); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); result = reader.decode(bitmap); //QRCodeDecoder decoder = new QRCodeDecoder(); ////QRCodeDecoder.Canvas = new ConsoleCanvas(); //String decodedString = decoder.decode(new QRCodeBitmapImage(new Bitmap(_image))); ////txtDecodedData.Text = decodedString; } catch { return null; } return result.Text; }