Either multipage scan or singlepage depending on the 1d barcode I specified

Discussions about TWAIN & WIA scanning in GdPicture.NET using GdPictureImaging.
Post Reply
nyalim
Posts: 8
Joined: Mon May 01, 2017 9:01 am

Either multipage scan or singlepage depending on the 1d barcode I specified

Post by nyalim » Tue May 16, 2017 9:51 am

Hi all,

We have a scanner application that works with gdpicture for years. It has both multipage and singlepage scanning options and now we want to add an another feature that is scanning through barcodes that we specified. So we have 2 unique barcodes and wherever it recognizes the one for the multipage, I want app to scan the documents as multipage tiff and singlepage tiffs if it sees the other one and this should be dynamic and happen back to back. This means, among the documents there might be ones that are supposed to be multipage tiffs and some others singlepage tiffs.

For example, 3 different multipage tiffs followed by 2 singlepage tiffs and again followed by 1 multipage tiff and so on...

The problem is, if I am to recognize the barcode, I have to scan it first anyway. How can I make this before deciding whether they will become multipages or singlepage tiff? By the way, barcodes are applied on an empty page that is put on the documents so I dont want them to get scanned as well. Algorithm and code examples would be very helpful because I'm getting some kind of exceptions whatever I implement according to the code examples in the online guide and samples.

Any kind of help are appreciated.

Cedric
Posts: 269
Joined: Sun Sep 02, 2012 7:30 pm

Re: Either multipage scan or singlepage depending on the 1d barcode I specified

Post by Cedric » Tue May 16, 2017 3:31 pm

That's actually TWAIN acquisition with separator sheets and that is relatively easy to implement.

Basically, what you need first is your acquisition loop and then, for each acquired image you need to establish if it is a separator sheet or not and if so, what mode should be set for the following pages. Finally, with this information, you can either save the image as a single document or stack the images until the next separator sheet and save them as a whole document. The only trick will actually be to know when a multipage document is finished so it can be saved and released.

What I've done is produce two kinds of separator sheets: simple blank pages with a Code128 barcode which value is either "single page" or "multipage".
Then I built a batch with a few documents mixing single and multipage in random order and used it against this code:

Code: Select all

private void Button1_Click(object eventSender, EventArgs eventArgs)
{
	int imageCount = 0;
	int documentCount = 0;
	int multipageHandle = 0;
	bool multipageMode = false;
	bool multipageOnGoing = false;

	if (GdPictureImaging.TwainOpenDefaultSource(Handle))
	{
		GdPictureImaging.TwainSetAutoFeed(true);
		GdPictureImaging.TwainSetAutoScan(true);
		GdPictureImaging.TwainEnableDuplex(false);
		GdPictureImaging.TwainSetResolution(200);
		GdPictureImaging.TwainSetPixelType(TwainPixelType.TWPT_BW);
		GdPictureImaging.TwainSetBitDepth(1);
		GdPictureImaging.TwainSetHideUI(true);
		do
		{
			int imageId = GdPictureImaging.TwainAcquireToGdPictureImage(Handle);
			if (imageId != 0)
			{
				imageCount++;
				bool isSeparatorSheet = IsSeparatorSheet(GdPictureImaging, imageId, ref multipageMode);
				if (isSeparatorSheet)
				{
					if (multipageOnGoing)
					{
						imageCount = 1;
						GdPictureImaging.TiffCloseMultiPageFile(multipageHandle);
						GdPictureImaging.ReleaseGdPictureImage(multipageHandle);
						multipageOnGoing = false;
					}
				}
				// skip to the next if separator sheet
				if (isSeparatorSheet)
				{
					imageCount = 0;
					continue;
				}
				// proceed with the saving if regular page
				if (multipageMode)
				{
					if (imageCount == 1)
					{
						multipageOnGoing = true;
						documentCount++;
						multipageHandle = imageId;
						GdPictureImaging.TiffSaveAsMultiPageFile(multipageHandle, "document" + documentCount + ".tif", TiffCompression.TiffCompressionAUTO);
					}
					else
					{
						GdPictureImaging.TiffAddToMultiPageFile(multipageHandle, imageId);
						GdPictureImaging.ReleaseGdPictureImage(imageId);
					}
				}
				else
				{
					imageCount = 0;
					documentCount++;
					GdPictureImaging.SaveAsTIFF(imageId, "document" + documentCount + ".tif", TiffCompression.TiffCompressionAUTO);
					GdPictureImaging.ReleaseGdPictureImage(imageId);
				}
			}
			else
			{
				MessageBox.Show("Error: " + GdPictureImaging.GetStat());
			}
		}
		while (GdPictureImaging.TwainGetState() > TwainStatus.TWAIN_SOURCE_ENABLED);
		if (multipageOnGoing)
		{
			GdPictureImaging.TiffCloseMultiPageFile(multipageHandle);
			GdPictureImaging.ReleaseGdPictureImage(multipageHandle);
		}
		GdPictureImaging.TwainCloseSource();
		MessageBox.Show("Done !");
	}
	else
	{
		MessageBox.Show("can't open default source, twain state is: " + GdPictureImaging.TwainGetState());
	}
}

private static bool IsSeparatorSheet(GdPictureImaging oGdPictureImaging, int imageId, ref bool multipageMode)
{
	bool isSeparatorSheet = false;
	oGdPictureImaging.Barcode1DReaderDoScan(imageId, Barcode1DReaderScanMode.BestSpeed);
	if (oGdPictureImaging.GetStat() == GdPictureStatus.OK)
	{
		if (oGdPictureImaging.Barcode1DReaderGetBarcodeCount() > 0)
		{
			isSeparatorSheet = true; // assuming that only separator sheets have barcodes
			for (int i = 1; i <= oGdPictureImaging.Barcode1DReaderGetBarcodeCount(); i++)
			{
				multipageMode = oGdPictureImaging.Barcode1DReaderGetBarcodeValue(i) == "multipage";
			}
		}
	}
	else
	{
		MessageBox.Show("Error: " + oGdPictureImaging.GetStat());
	}
	return isSeparatorSheet;
}
Of course this code is only meant for educational purpose, I had to set arbitrary values (200 DPI black and white acquisition from default source) and assume that only the separator sheets have barcodes on it which may not reflect the reality of your documents. The error checking is also very minimal and should be enhanced but you get the idea.

nyalim
Posts: 8
Joined: Mon May 01, 2017 9:01 am

Re: Either multipage scan or singlepage depending on the 1d barcode I specified

Post by nyalim » Wed May 17, 2017 10:16 am

Oh yes separator sheets!....I'm glad that I was able to explain what I need:)

I just finished checking codes. Looks like I can adapt this easily into my application with few changes here and there of course like you say. After this I will need to change the "keep scanning" feature as well but for now this will do it.

Thanks Cedric.

Cedric
Posts: 269
Joined: Sun Sep 02, 2012 7:30 pm

Re: Either multipage scan or singlepage depending on the 1d barcode I specified

Post by Cedric » Wed May 17, 2017 10:18 am

You're welcome!

nyalim
Posts: 8
Joined: Mon May 01, 2017 9:01 am

Re: Either multipage scan or singlepage depending on the 1d barcode I specified

Post by nyalim » Thu May 18, 2017 10:49 am

Hi Cedric,

Well unfortunately I couldn't make it work exactly:(....everything seems ok code-wise but it doesn't separate files as I want it to. In order to be sure I illustrated the behavior I want so there would be no questions in mind;

https://www.imageupload.co.uk/images/20 ... sheets.jpg

please read the graph attentively.

By making things clear, I hope we can understand whether the problem was your code(maybe it doesn't do the exact operation I want) or my adapting it to my application.

Helps are appreciated.

Cedric
Posts: 269
Joined: Sun Sep 02, 2012 7:30 pm

Re: Either multipage scan or singlepage depending on the 1d barcode I specified

Post by Cedric » Thu May 18, 2017 12:59 pm

That's exactly what the code I provided does, I tested it a few minutes ago with a different batch of sheets and it works as expected.
All the pages behind a separator sheet with multipage barcode end up in a multipage TIFF and pages behind a separator sheet with single page barcode end up in separate single page TIFF files.

Please find attached the full working C# project as well as the separator sheets I've been using for my tests.
Attachments
TWAIN_Separator_Sheets.zip
(7.6 KiB) Downloaded 602 times
Separator_Sheets.pdf
(3.55 KiB) Downloaded 589 times

nyalim
Posts: 8
Joined: Mon May 01, 2017 9:01 am

Re: Either multipage scan or singlepage depending on the 1d barcode I specified

Post by nyalim » Thu May 18, 2017 1:35 pm

Thanks again Cedric. I figured the codes were correct but I just needed to be sure:)

I guess there are many stuff that I need to change in my application to adapt this into it. I couldn't see the control for singlepage barcode separator though...that's not necessary?

Cedric
Posts: 269
Joined: Sun Sep 02, 2012 7:30 pm

Re: Either multipage scan or singlepage depending on the 1d barcode I specified

Post by Cedric » Thu May 18, 2017 1:37 pm

For this demo application I assumed that there's only two possible modes, multipage and single page so if the barcode isn't multipage, it can only be single page.

nyalim
Posts: 8
Joined: Mon May 01, 2017 9:01 am

Re: Either multipage scan or singlepage depending on the 1d barcode I specified

Post by nyalim » Tue May 23, 2017 10:39 am

Hi Cedric,

I want you to know that I was able to fully implement your algorithm to my application and it works as intended, well, mostly:)

I have few issues that need to be fixed;

1-)

if (Program.Settings.Filter.RemoveBlankPages)
{
if (_gdPictureImaging.IsBlank(imageId, 99.9f, true))
{
_gdPictureImaging.ReleaseGdPictureImage(imageId);
imageId = 0;
return;
}
}

skipping blank(white) pages somehow breaks your code therefore program stops working correctly. I'm aware why this happens but releasing image and setting id to 0 are the only thing I can think of to get rid of white pages. Any ideas?

2-) After seeing single page barcode, software scans everything as a individual tiff files, which is correct. But if duplex is enabled, I want singlepage scenario to work like a multipage one so those single page tiffs should be 2-page multipage tiffs formed by both front and back of the page actually. Is that possible?

Thanks for your help.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest