private void buttonReadBarcodes_Click(object sender, EventArgs e) { if (_image != null) { // init settigs _reader.Settings.AutomaticRecognition = true; _reader.Settings.MinConfidence = 95; readerResults.Text = "Recognition..."; readerResults.Refresh(); // read barcodes IBarcodeInfo[] infos; try { infos = _reader.ReadBarcodes(_image); } catch (NotImplementedException ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); readerResults.Text = ""; return; } // get results readerResults.Text = GetResults(infos); readerResults.Refresh(); // draw barcode rectangles DrawBarcodeRectangles(infos); } }
static void Main(string[] args) { // load PDF document PDFDocument doc = new PDFDocument(@"C:\Users\Edward\Documents\GitHub\scan-qr-pdf\ScanQRfromPDF\Documents\CódigoQR.pdf"); // get the page you want to scan BasePage page = doc.GetPage(0); // set reader setting ReaderSettings setting = new ReaderSettings(); // set type to read setting.AddTypesToRead(BarcodeType.QRCode); // read barcode from PDF page Barcode[] barcodes = BarcodeReader.ReadBarcodes(setting, page); // get each data foreach (Barcode barcode in barcodes) { // print the loaction of barcode on image //Console.WriteLine(barcode.BoundingRectangle.X + " " + barcode.BoundingRectangle.Y); // output barcode data onto screen Console.WriteLine(barcode.DataString); } // input data Console.Read(); }
void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { while (!_backgroundWorker.CancellationPending) { _eventLog.WriteEntry(string.Format("{0}: DoWork Start", ServiceName)); // generate image with barcode DateTime timeNow = DateTime.Now; string imageWithBarcodePath = ""; Bitmap imageWithBarcode = null; try { BarcodeWriter barcodeWriter = new BarcodeWriter(); barcodeWriter.Settings.Barcode = BarcodeType.Code39; barcodeWriter.Settings.Value = timeNow.ToString(); barcodeWriter.Settings.Padding = 20; imageWithBarcode = (Bitmap)barcodeWriter.GetBarcodeAsBitmap(); imageWithBarcodePath = string.Format(@"d:\barcodes\{0}.png", timeNow.Millisecond); imageWithBarcode.Save(imageWithBarcodePath); } catch (Exception ex) { _eventLog.WriteEntry(string.Format("{0}: Barcode writing error: {1}", ServiceName, ex.Message)); } finally { } // read barcode from the image try { BarcodeReader barcodeReader = new BarcodeReader(); barcodeReader.Settings.ScanBarcodeTypes = BarcodeType.Code39; barcodeReader.Settings.ScanDirection = ScanDirection.LeftToRight; IBarcodeInfo[] barcodeInfo = barcodeReader.ReadBarcodes(imageWithBarcode); string result = "No barcodes found."; if (barcodeInfo.Length != 0) { result = string.Format("Barcode found: {0}", barcodeInfo[0]); } _eventLog.WriteEntry(string.Format("{0}: {1}", ServiceName, result)); File.WriteAllText(string.Format(@"d:\barcodes\{0}.txt", timeNow.Millisecond), result); } catch (Exception ex) { _eventLog.WriteEntry(string.Format("{0}: Barcode reading error: {1}", ServiceName, ex.Message)); } finally { Thread.Sleep(1000); } _eventLog.WriteEntry(string.Format("{0}: DoWork Finish", ServiceName)); } }
/// <summary> /// Checks active license. /// </summary> private void CheckLicense() { try { BarcodeReader reader = new BarcodeReader(); reader.ReadBarcodes(new GrayscaleImageSource(1, 1, new byte[1])); } catch (Exception ex) { ShowInfoDialog(ex.GetType().ToString(), ex.Message); } }
/// <summary> /// Read barcodes. /// </summary> private void StartReadBarcodes(object state) { _barcodes = null; try { _barcodes = _reader.ReadBarcodes(LoadImage(_imageFilename, ref _pageIndex)); } catch (System.ComponentModel.LicenseException) { throw; } catch (Exception ex) { Dispatcher.BeginInvoke(new ShowBarcodesInformationDelegate(ShowBarcodesInformation)); MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } Dispatcher.BeginInvoke(new ShowBarcodesInformationDelegate(ShowBarcodesInformation)); }
static void Main(string[] args) { try { if (args.Length < 1) { Console.WriteLine("Usage: BarcodeReaderConsoleDemo imageFilename [BarcodeType1] [BarcodeType2] ..."); Console.WriteLine("Usage: BarcodeReaderConsoleDemo imageFilename ReaderSettings.xml"); return; } #if NETCORE // register custom encodings for QR and HanXin Code barcodes // (System.Text.Encoding.CodePages package) System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); #endif // image with barcode string filename = args[0]; try { // create barcode reader using (BarcodeReader reader = new BarcodeReader()) { if (args.Length == 2 && File.Exists(args[1])) { // deserialize the barcode reader settings from file XmlSerializer serializer = new XmlSerializer(typeof(ReaderSettings)); using (FileStream stream = new FileStream(args[1], FileMode.Open, FileAccess.Read)) reader.Settings = (ReaderSettings)serializer.Deserialize(stream); } else { // set default reader settings reader.Settings.AutomaticRecognition = true; reader.Settings.ScanDirection = ScanDirection.Horizontal | ScanDirection.Vertical; // set scan barcode types reader.Settings.ScanBarcodeTypes = BarcodeType.None; if (args.Length == 1) { SetAllBarcodeTypes(reader.Settings); } else { for (int i = 1; i < args.Length; i++) { AddBarcodeType(reader.Settings, args[i]); } } } // read barcodes Console.Write("Recognition..."); IBarcodeInfo[] infos = reader.ReadBarcodes(filename); Console.WriteLine(string.Format("{0} ms.", reader.RecognizeTime.TotalMilliseconds)); Console.WriteLine(); // display recognition results PrintResults(infos); } } catch (Exception ex) { Console.WriteLine(string.Format("Error: {0}", ex.Message)); } } finally { Console.WriteLine("Press any key..."); Console.ReadKey(); } }
/// <summary> /// Recognizes barcodes synchronously. /// </summary> public void ReadBarcodesSync() { if (_isRecognitionStarted) { throw new InvalidOperationException("Recognition process is executing at this moment."); } _isRecognitionStarted = true; if (InvokeRequired) { Invoke(new OnRecoginitionStartedDelegate(OnRecoginitionStarted), EventArgs.Empty); } else { OnRecoginitionStarted(EventArgs.Empty); } try { #if !REMOVE_BARCODE_SDK if (ImageViewer != null) { VintasoftImage image = ImageViewer.Image; if (image != null) { ChangePixelFormatCommand convertCommand = null; switch (image.PixelFormat) { case PixelFormat.Gray16: convertCommand = new ChangePixelFormatCommand(PixelFormat.Gray8); break; } if (convertCommand != null) { image = convertCommand.Execute(image); } // set settings if (Rectangle.Size.IsEmpty) { ReaderSettings.ScanRectangle = Rectangle.Empty; } else { ReaderSettings.ScanRectangle = Rectangle; } // recognize barcodes using (Image bitmap = image.GetAsBitmap()) RecognitionResults = _reader.ReadBarcodes(bitmap); if (convertCommand != null) { image.Dispose(); } } else { RecognitionResults = null; } } else { RecognitionResults = null; } #endif } catch (Exception ex) { DemosTools.ShowErrorMessage(ex); } finally { _isRecognitionStarted = false; _isAsyncRecognitionStarted = false; if (InvokeRequired) { Invoke(new OnRecoginitionFinishedDelegate(OnRecoginitionFinished), EventArgs.Empty); } else { OnRecoginitionFinished(EventArgs.Empty); } } }