0

I have an image with multiple barcodes inside the picture box.

I have no idea how to select the specific barcode area and decode it.

I had done the high light function but after I high light the barcode it will show me some error like "'Crop Rectangle is larger than the input image". I have stuck by these few days and no idea how to proceed it.

private Rectangle Rect = new Rectangle();
private Brush selectionBrush = new SolidBrush(Color.FromArgb(128, 72, 145, 220));

private void IronBarcode()
{
      if (pcbox.Image != null)
      {
      BarcodeResult[] ImageResults=BarcodeReader.ReadAllBarcodes("barcode.jpg", 
      BarcodeEncoding.All,BarcodeReader.BarcodeRotationCorrection.Low, 
      BarcodeReader.BarcodeImageCorrection.DeepCleanPixels);
      }
       foreach (var PageResult in ImageResults)
       {
        string Value = PageResult.Value;
         BarcodeEncoding BarcodeType = PageResult.BarcodeType;

         decoded += "Decode: " + PageResult.Value + Type: " + BarcodeType";    
        }
     if (decoded != "")
     {
      txtoutput.Text = decoded;
     }
}


private void pcbox_Paint(object sender, PaintEventArgs e)
{
// Draw the rectangle...
  if (pcbox.Image != null)
  {
     if (Rect != null && Rect.Width > 0 && Rect.Height > 0)
     {
       e.Graphics.FillRectangle(selectionBrush, Rect);
     }
  }
}
private void pcbox_MouseDown(object sender, MouseEventArgs e)
{
  // Determine the initial rectangle coordinates...
  RectStartPoint = e.Location;
  Invalidate();
}

private void pcbox_MouseMove(object sender, MouseEventArgs e)
{
  if (e.Button != MouseButtons.Left)
  return;
   Point tempEndPoint = e.Location;
   Rect.Location = new Point(
   Math.Min(RectStartPoint.X, tempEndPoint.X),
   Math.Min(RectStartPoint.Y, tempEndPoint.Y));
   Rect.Size = new Size(
   Math.Abs(RectStartPoint.X - tempEndPoint.X),
   Math.Abs(RectStartPoint.Y - tempEndPoint.Y));
   pcbox.Invalidate();
}

private void pcbox_MouseUp(object sender, MouseEventArgs e)
{
  if (e.Button == MouseButtons.Right)
  if (Rect.Contains(e.Location))
  {
     BarcodeResult[] InvoiceResults = 
     BarcodeReader.ReadAllBarcodesInCropArea("barcode.jpg",Rect, 
     BarcodeEncoding.All, BarcodeReader.BarcodeRotationCorrection.Low, 
     BarcodeReader.BarcodeImageCorrection.None);

        foreach (var rs in InvoiceResults)
        {
          txtoutput.Text = r s.Text;
        }

   }
}

I only want to decode the specific barcode I have selected.enter image description here

Frankie
  • 23
  • 5
  • could you explain what you try to realize? Do you want to highlight a area with the mouse and these area should be analysed? – Mar Tin Jul 11 '19 at 09:59
  • @MarTin yes sir, I want to decode the barcode inside the area I highlighted like the photo sample. – Frankie Jul 11 '19 at 13:35
  • do you want to decide which barcode should analyse manually (per dragging mouse and highlight the barcode) or automatically (by passing the name "Rebecca"?) – Mar Tin Jul 12 '19 at 07:52
  • @MarTin I want to decode it manually by highlight the barcode I want. – Frankie Jul 12 '19 at 07:53
  • last question: _"I have no idea how to select the specific barcode area and decode it."_ do you highlight you example image with your application in runtime or manually with a image editor? Do you have already an analysing function for the barcode? – Mar Tin Jul 12 '19 at 08:08
  • @MarTin Yes, I used mouse event to trigger the function to highlight my barcode when during the runtime, and also using IronBarcode DLL to analyzing the barcode. I try to search online for open source barcode decoder but most of the DLL is for trial version. – Frankie Jul 12 '19 at 08:15
  • @MarTin yea, I also got try using zxing to decode. But I haven't try to use the server decoder to generate the result. But the main issue is I cannot select the specific barcode to decode. For example, an image includes multiple barcodes and I only want to select one of the barcodes I want to analyze. I have no idea how to read the highlighted area. – Frankie Jul 12 '19 at 08:58
  • I understand your issue, a possible workaround to avoid analysing a QR code from scratch, is to use this web api: [zxing](https://zxing.org/w/decode.jspx). You could send your image to the server and read out the result over a html lib. If I put your image to the decoder, the server generate [this](https://zxing.org/w/decode?u=https%3A%2F%2Fi.stack.imgur.com%2FAawkH.png) result. Does that helps? – Mar Tin Jul 12 '19 at 09:00
  • you know the name **Rebecca** before? If yes you still could use the zxing api and isolate all the `goo.gl/` links. For example your highlighted QR code is `goo.gl/LO4pe` the decoded url contain the name `rebecca`, so you can decode all links and searching for `rebecca` in the url. You can do that [here](http://checkshorturl.com/) – Mar Tin Jul 12 '19 at 09:06
  • @MarTin Ermm, actually the QR code photo is I get from google for testing decode purposes, I would like to import other pictures that contain barcode inside in the future. I develop this program is to allow the user to import a barcode image then select which barcode they want to decode. – Frankie Jul 12 '19 at 09:15
  • take your `Rect` variable after a user highlight a barcode (`eventMouseUp`) and execute `Bitmap croppedImage = originalBitmap.Clone(Rect, originalBitmap.PixelFormat);` use the `croppedImage` and the zxing api to analyse the QR code. Source: https://stackoverflow.com/questions/11457679/extract-sub-image-from-an-image-using-c-sharp – Mar Tin Jul 12 '19 at 09:29
  • @MarTin okay, I will try and see, thank you Mr Martin – Frankie Jul 12 '19 at 10:32

0 Answers0