[Tutorial]Saving Images into folders based on their barcode

Discussions about barcode reading and writing
Post Reply
SamiKharma
Posts: 352
Joined: Tue Sep 27, 2011 11:47 am

[Tutorial]Saving Images into folders based on their barcode

Post by SamiKharma » Mon Jun 18, 2012 1:38 pm

Hello again,

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);
Now we need to establish a destination folder, this folder will have all the folders named after the barcode types:

Code: Select all

String distPath = "C:\\Users\\GdPicture\\Pictures\\barcodeTypes\\";
Now, the we will need a foreach loop to loop through the images, but first we will define two parameters, one to hold the image handler, and the other to hold the number of barcodes found in each image:

Code: Select all

 int bcfound;
 int m_Image = 0; 
 foreach (string imagePath in allImages)
 {
    bcfound = 0;
    if (m_Image != 0) Imaging1.ReleaseGdPictureImage(m_Image);
As you have see, we have set the number of barcodes found to 0 with each new image, and released the image if it has not been released from the previous loop.

Now we load the image:

Code: Select all

 m_Image = Imaging1.CreateGdPictureImageFromFile(imagePath);
 if (m_Image == 0)
 {
    MessageBox.Show("Error: " + Imaging1.GetStat().ToString());
    return;
 }
As you see, we need to check that the image is loaded, and if not, we need to return an error Message.

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;
                         }
Now we retrieve the number of barcodes found in the image:

Code: Select all

bcfound = Imaging1.Barcode1DReaderGetBarcodeCount();
If the number of barcodes found is bigger than 0, we continue to saving:

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);
  }
}
What we have done here is the following:
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);
                    }
                 }

              }
           }
As usual, if you have any questions please do not hesitate to post them here.

Cheers,
Sami

Gabriela
Posts: 436
Joined: Wed Nov 22, 2017 9:52 am

Re: [Tutorial]Saving Images into folders based on their barcode

Post by Gabriela » Wed Jan 30, 2019 10:02 am

Hello,

You can find the complete code snippet here:
https://www.gdpicture.com/guides/gdpicture/web ... rcode.html

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest