We have received many requests and questions about how to save images based on their barcode type. This would be terribly simple if one understood how to use the barcode reader engine. Thus, by implementing the saving of images based on their barcode type, we will actually be showing how to use those BarcodeReader functions, or at least, part of them.
First is first, as usual we have assumptions and explanations to make.
We need:
1. A file directory that has the images in - The images do not have to be in a folder on desk, they could be from your scanner directly using our Twain functions, but for this tutorial's sake, we will assume they are on the hard drive somewhere.
2. A GdPictureImaging object called Imaging1.
3. You have created folders in your DistinationFolder for every barcode type adhering to the barcode naming convention in GdPicture, example: Barcode1DReaderCode39, and Barcode1DReaderCode128.
Please remark that the following example will save the image multiple times in the same folder or different folders, depending on how many barcodes the image has and what types they are. If you want it saved only once, just do not loop the number of barcodes found and save it according to the first barcode found.
Also this program will save all images in tiff format with AutoCompression. You can save them in any format or compression type you want or need.
Ok, here we go:
First let us get the images in the folder directory:
Code: Select all
String filePath= "C:\\Users\\GdPicture\\Pictures\\barcodes";
String [] allImages = Directory.GetFiles(filePath);
Code: Select all
String distPath = "C:\\Users\\GdPicture\\Pictures\\barcodeTypes\\";
Code: Select all
int bcfound;
int m_Image = 0;
foreach (string imagePath in allImages)
{
bcfound = 0;
if (m_Image != 0) Imaging1.ReleaseGdPictureImage(m_Image);
Now we load the image:
Code: Select all
m_Image = Imaging1.CreateGdPictureImageFromFile(imagePath);
if (m_Image == 0)
{
MessageBox.Show("Error: " + Imaging1.GetStat().ToString());
return;
}
We finally scan for barcodes, then check that there was no error in the scanning process:
Code: Select all
Imaging1.Barcode1DReaderDoScan(m_Image, Barcode1DReaderScanMode.BestSpeed);
if (Imaging1.GetStat() != GdPictureStatus.OK)
{
MessageBox.Show("Error: " + Imaging1.GetStat().ToString());
return;
}
Code: Select all
bcfound = Imaging1.Barcode1DReaderGetBarcodeCount();
Code: Select all
if (bcfound > 0)
{
for (int i = 1; i <= Imaging1.Barcode1DReaderGetBarcodeCount(); i++)
{
String barcodeType = Imaging1.Barcode1DReaderGetBarcodeType( i ).ToString();
String folderName = String.Concat(barcodeType, "\\");
String imageName= String.Concat(i.ToString(), ".tif");
String completePath = String.Concat(Path.Combine(distPath, folderName), imageName);
Imaging1.SaveAsTIFF(m_Image, completePath, TiffCompression.TiffCompressionAUTO);
}
}
1. Loop through each barcode found in the image
2. Get the barcode Type
3. Add the barcodeType to the distination folder
4. Add the image name by calling it by the number of the barcode found. i.e: if it is the second barcode, the image will be called 2.tiff, if it is the 3rd barcode in the image, it will be called 3.tiff.
5. Combine all, distpath, barcodetype, and image name
6. Save it to the folder path
I am sure there are a million other ways of doing the path saving, probably there are many better too. Feel free to use whatever method you prefer.
Now let us put it all together:
Code: Select all
String filePath = "C:\\Users\\GdPicture\\Pictures\\barcodes";
String distPath = "C:\\Users\\GdPicture\\Pictures\\barcodeTypes\\";
String[] allImages = Directory.GetFiles(filePath);
int bcfound;
int m_Image = 0;
foreach (string imagePath in allImages)
{
bcfound = 0;
if (m_Image != 0) Imaging1.ReleaseGdPictureImage(m_Image);
m_Image = Imaging1.CreateGdPictureImageFromFile(imagePath);
if (m_Image == 0)
{
MessageBox.Show("Error: " + Imaging1.GetStat().ToString());
return;
}
Imaging1.Barcode1DReaderDoScan(m_Image, Barcode1DReaderScanMode.BestSpeed);
if (Imaging1.GetStat() != GdPictureStatus.OK)
{
MessageBox.Show("Error: " + Imaging1.GetStat().ToString());
return;
}
if (Imaging1.GetStat() == GdPictureStatus.OK)
{
bcfound = Imaging1.Barcode1DReaderGetBarcodeCount();
if (bcfound > 0)
{
for (int i = 1; i <= Imaging1.Barcode1DReaderGetBarcodeCount(); i++)
{
String barcodeType = Imaging1.Barcode1DReaderGetBarcodeType(i).ToString();
String folderName = String.Concat(barcodeType, "\\");
String imageName = String.Concat(i.ToString(), ".tif");
String completePath = String.Concat(Path.Combine(distPath, folderName), imageName);
Imaging1.SaveAsTIFF(m_Image, completePath, TiffCompression.TiffCompressionAUTO);
}
}
}
}
Cheers,
Sami