Barcode1DReaderGetBarcodeXY

Discussions about barcode reading and writing
Post Reply
dixus
Posts: 39
Joined: Wed Feb 10, 2010 9:23 am

Barcode1DReaderGetBarcodeXY

Post by dixus » Wed Feb 24, 2010 5:33 pm

Hello,

i used the Barcode1DReaderGetBarcodeX1, X2.. methods to draw a red rectangle on an gdimage. I split images by barcode that are acquired by twain and show thumbnails during the process. So far so good.

But if i do the same (exactely the same method to draw the rect is called) by a FileSource and not a TwainSource, the coordinates arent correct.

Height and Width look fine, but the coordinates are like 200 instead of 1600 and so on. Its like its inverted. The barcode is in the upper right corner and the rect is shown on the left side of the image...

It would help me to know by what this can be caused by. I have no clue because the same method that saves the tif by barcode splitting works if the imageID is created by TwainAcquireToGdPictureImage.. but not with TiffCreateMultiPageFromFile / TiffSelectPage ...

cheers
dix

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Barcode1DReaderGetBarcodeXY

Post by Loïc » Wed Feb 24, 2010 5:38 pm

Hi Dix,

Interesting issue. Could you give me a code snippet to reproduce this problem ?

Also, could you attach the image on which coordinates are reversed ?

Kind regards,

Loïc

dixus
Posts: 39
Joined: Wed Feb 10, 2010 9:23 am

Re: Barcode1DReaderGetBarcodeXY

Post by dixus » Wed Feb 24, 2010 5:46 pm

uh that will need some time because the methods are part of my little framework that uses you great lib.. i will see what i can do.

dixus
Posts: 39
Joined: Wed Feb 10, 2010 9:23 am

Re: Barcode1DReaderGetBarcodeXY

Post by dixus » Wed Feb 24, 2010 6:10 pm

I tried to extract the core of the methods so that you can use it.

Here is a function that draws a rect around a barcode:

Code: Select all

	private void AddBarodeRectangle(int DocumentImgID, int BarcodeNo)
	{            
		int x1 = GdLib.Barcode1DReaderGetBarcodeX1(BarcodeNo);
		int x2 = GdLib.Barcode1DReaderGetBarcodeX2(BarcodeNo);
		int x3 = GdLib.Barcode1DReaderGetBarcodeX3(BarcodeNo);
		int x4 = GdLib.Barcode1DReaderGetBarcodeX4(BarcodeNo);
		int y1 = GdLib.Barcode1DReaderGetBarcodeY1(BarcodeNo);
		int y2 = GdLib.Barcode1DReaderGetBarcodeY2(BarcodeNo);
		int y3 = GdLib.Barcode1DReaderGetBarcodeY3(BarcodeNo);
		int y4 = GdLib.Barcode1DReaderGetBarcodeY4(BarcodeNo);

		int left = x1;
		int top = y1;
		int right = x2;
		int bottom = y4;

		if (x3 < x1)
			left = x3;
		if (y2 < y1)
			top = y2;
		if (x4 > x2)
			right = x4;
		if (y3 > y4)
			bottom = y3;

		int width = right - left;
		int height = bottom - top;

		int margin = (int)(((double)height + (double)width) / 2) / 5;

		GdLib.DrawRectangle(DocumentImgID, left - margin, top - margin, width + margin * 2, height + margin * 2, 20, Color.Red, true);
	}

Here is the method that is used by a twain and a spitter service:

Code: Select all

	protected bool SplittTifByBarcode(string path, int page, ref int DocumentImgID, int SourceImageID)
	{
		bool result = false;
		if (GdLib.Barcode1DReaderDoScan(SourceImageID, Barcode1DReaderScanMode.BestQuality) == GdPictureStatus.OK)
		{
			int BarcodeCount = GdLib.Barcode1DReaderGetBarcodeCount();                
			if (BarcodeCount > 0)
			{
				if (DocumentImgID != 0)
					GdLib.ReleaseGdPictureImage(DocumentImgID);

				string value = GdLib.Barcode1DReaderGetBarcodeValue(BarcodeCount);
				DocumentImgID = GdLib.CreateClonedGdPictureImageI(SourceImageID);                    
				GdLib.TiffSaveAsMultiPageFile(DocumentImgID, "testfile.tif", (TiffCompression)Config.TiffCompressionMode);
				result = true;

				#warning "This works if image comes from twain but not from multipage tiff"
                               int tmpID = GdLib.CreateClonedGdPictureImage(DocumentImgID);
				AddBarodeRectangle(DocumentImgID, BarcodeCount);
			   
			}
			else
				GdLib.TiffAddToMultiPageFile(DocumentImgID, SourceImageID);
		}
		return result;
	}
Here is the caller service (TWAIN). The rect is where it should be...

Code: Select all

	int TiffImgID = 0;
	int page = 0;
	int TwainImgID = 0;
	int DocumentImgID = 0;

	bool barcodeFound;
	string path = Guid.NewGuid() + ".tif";

	do{
		TwainImgID = GdLib.TwainAcquireToGdPictureImage(IntPtr.Zero);
		if (TwainImgID != 0)
		{
			page++;
			barcodeFound = this.SplittTifByBarcode(path, page, ref DocumentImgID, TwainImgID);
			//we found valid barcode 
			//so we dont need to save the scan as a complete file in scandirectory
			if (!barcodeFound)
			   TiffImgID = WriteTifFile(TiffImgID, TwainImgID);
		}

	} while ((TwainImgID != 0) && (GdLib.TwainGetState() > GdPicture.TwainStatus.TWAIN_SOURCE_OPEN));


	if (DocumentImgID != 0)
		GdLib.ReleaseGdPictureImage(DocumentImgID);

	GdLib.TiffCloseMultiPageFile(TiffImgID);
	GdLib.ReleaseGdPictureImage(TiffImgID);
	GdLib.TwainCloseSourceManager(IntPtr.Zero);
	GdLib.TwainCloseSource();
and this will draw the rect in wrong position:

Code: Select all

	int page = 0;
	int DocumentImgID = 0;
	int TiffImageID = GdLib.TiffCreateMultiPageFromFile(path);
	int pageCount = GdLib.TiffGetPageCount(TiffImageID);

	//iterate over pages of multipage source tiff
	for (page = 1; page <= pageCount; page++)
	{
		//select the current tiff TiffImageID
		if (GdLib.TiffSelectPage(TiffImageID, page) == GdPictureStatus.OK)
		{
			//scan the selected page for barcodes
			SplittTifByBarcode(path, page, ref DocumentImgID, TiffImageID);
		}
	}

	if (DocumentImgID != 0)
		GdLib.ReleaseGdPictureImage(DocumentImgID);

	GdLib.TiffCloseMultiPageFile(TiffImageID);
	GdLib.ReleaseGdPictureImage(TiffImageID);

I hope that helps. I investigated that code and i couldnt see any wrong usage of gdpicture...
Attachments
example.tif
Example with a barcode on the upper right.

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Barcode1DReaderGetBarcodeXY

Post by Loïc » Thu Feb 25, 2010 12:39 pm

Hi,

i tested your image with no issue.

There is a part of your code I don't understand:

Code: Select all

      int left = x1;
      int top = y1;
      int right = x2;
      int bottom = y4;

      if (x3 < x1)
         left = x3;
      if (y2 < y1)
         top = y2;
      if (x4 > x2)
         right = x4;
      if (y3 > y4)
         bottom = y3;

      int width = right - left;
      int height = bottom - top;

      int margin = (int)(((double)height + (double)width) / 2) / 5;
i think your problem come here.

This is the code I used to draw bounding box around the barcode (not always rectangle !!!)

i think you should convert it to C++ :

Code: Select all

     
 Dim X1, X2, X3, X4, Y1, Y2, Y3, Y4 As Integer

For i = 1 To oGdPictureImaging.Barcode1DReaderGetBarcodeCount
   X1 = oGdPictureImaging.Barcode1DReaderGetBarcodeX1(i) 
   X2 = oGdPictureImaging.Barcode1DReaderGetBarcodeX2(i) 
   X3 = oGdPictureImaging.Barcode1DReaderGetBarcodeX3(i)
   X4 = oGdPictureImaging.Barcode1DReaderGetBarcodeX4(i)

   Y1 = oGdPictureImaging.Barcode1DReaderGetBarcodeY1(i) 
   Y2 = oGdPictureImaging.Barcode1DReaderGetBarcodeY2(i) 
   Y3 = oGdPictureImaging.Barcode1DReaderGetBarcodeY3(i)
   Y4 = oGdPictureImaging.Barcode1DReaderGetBarcodeY4(i) 

   oGdPictureImaging.DrawLine(m_ImageID, X1, Y1, X2, Y2, 1, Color.Red, True)
   oGdPictureImaging.DrawLine(m_ImageID, X2, Y2, X3, Y3, 1, Color.Red, True)
   oGdPictureImaging.DrawLine(m_ImageID, X3, Y3, X4, Y4, 1, Color.Red, True)
   oGdPictureImaging.DrawLine(m_ImageID, X4, Y4, X1, Y1, 1, Color.Red, True)
Next
Hope this helps ;)

Loïc

dixus
Posts: 39
Joined: Wed Feb 10, 2010 9:23 am

Re: Barcode1DReaderGetBarcodeXY

Post by dixus » Thu Feb 25, 2010 12:47 pm

thanks i will try this.

the code you dont understand is just to set the correct borders ( e.g. if the barcode is rotated some degrees)...
i make a breakpoint where it reads the coordinates. they are wrong before that code is reached...


please check my mail about the site / worldwide site licence... i dont get the difference :)

Stephan78
Posts: 4
Joined: Wed Aug 09, 2017 4:08 pm

Re: Barcode1DReaderGetBarcodeXY

Post by Stephan78 » Wed Aug 09, 2017 4:32 pm

Hi there,

Sorry to revive this old thread, but I have an issue with the same function. (It is on Version 12)

Situation as follows:
I have a sheet Din-A-4 Paper scanned to a GDPictureImage from TWAIN.
On there are 4 Codeabar codes, 3 of which get recognized when I perform BC recognition. So far so good.
The first ones are nearly without skew - nothing peculiar about them. But the last one is skewed by ~70°.
There, I have following readings:

TopLeft = 427 | 2347
TopRight = 649 | 2912
BottomRight = 926 | 2801
BottomLeft = 705 | 2236

Each of them is reasonable, so there aren't some x and y wrongly associated. But the association to "TopLeft" etc seems wrong to me.
Seen relative to the barcode itself, the lefts are left and the rights are right, but they seem topped over ("Top" should be "Bottom" and vice versa).

Haven't tried a barcode with skews <45° or >90°, yet.

Costinel
Posts: 36
Joined: Mon Jul 11, 2016 9:35 am

Re: Barcode1DReaderGetBarcodeXY

Post by Costinel » Thu Aug 10, 2017 7:31 am

Hi Stephan,

Please attach the image so we may replicate the issue.
Or you may open a ticket support on https://support.orpalis.com/index.php?/Tickets/Submit/

Best regards,
Costinel Mitrea
GdPicture Team

Stephan78
Posts: 4
Joined: Wed Aug 09, 2017 4:08 pm

Re: Barcode1DReaderGetBarcodeXY

Post by Stephan78 » Thu Aug 10, 2017 3:31 pm

Hi Costinel,

Thanks for reply.

I do not want to file an invalid bug report, because I am not sure if it actually is a bug or it is just me doing something wrong.
I attach the document I used for testing. It is a docx that I printed out and scanned on a HP ScanJet Enterprise Flow 7000 s3 via TWAIN.

Regards, Stephan

P.S.: I did not use Twain Barcode recognition. I used barcode recognition on the resulting GdPictureImage after scan.
Scan params included 300 dpi, black/white, no duplex.
Attachments
Codeabar.docx
(27.5 KiB) Downloaded 550 times

Costinel
Posts: 36
Joined: Mon Jul 11, 2016 9:35 am

Re: Barcode1DReaderGetBarcodeXY

Post by Costinel » Fri Aug 11, 2017 7:38 am

Hi Stephan,

You are right, the barcode coordinates for the skewed barcode are not good.
The association to TopLeft,... should be relative to the original position of the barcode before the rotation.
It should have been:

TopLeft = 705 | 2236
TopRight = 926 | 2801
BottomRight = 649 | 2912
BottomLeft = 427 | 2347

Please save the scanned image and attach it here. Thank you.

I'm sending the issue to the development team. I will keep you updated.

Best regards,
Costinel

Stephan78
Posts: 4
Joined: Wed Aug 09, 2017 4:08 pm

Re: Barcode1DReaderGetBarcodeXY

Post by Stephan78 » Fri Aug 11, 2017 9:38 am

Hi Costinel,

Thank you very much for confirmation and support.

Attached to this post, you will find the scanned image.
Since it was scanned in b/w I did not perform any binarization or other image processing.

Please also remember, that I was using GdPicture 12. (It's not in my control if we update or not).
I do not know if this is already fixed in Version 14.

Best regards,
Stephan
Attachments
barcode_scan.zip
Scan of the barcode doc. Had to zip it since it wouldn't let me attach the tif.
(11.78 KiB) Downloaded 553 times
Last edited by Stephan78 on Wed Aug 16, 2017 8:54 am, edited 1 time in total.

Costinel
Posts: 36
Joined: Mon Jul 11, 2016 9:35 am

Re: Barcode1DReaderGetBarcodeXY

Post by Costinel » Fri Aug 11, 2017 10:14 am

Hi Stephan,

Version 12 is discontinued, so I tried with version 14. Any fix will be available only for version 14.
Please try again to attach the image.

Regards,
Costinel

Stephan78
Posts: 4
Joined: Wed Aug 09, 2017 4:08 pm

Re: Barcode1DReaderGetBarcodeXY

Post by Stephan78 » Wed Aug 16, 2017 11:12 am

Hi Costinel,

I finally managed to attach the scan. See updated post above.

Regards, Stephan

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest