In This Topic
Programming / Barcode Reading & Writing / Saving images into folders based on their barcode

Saving images into folders based on their barcode

In This Topic

This sample will show you how to sort image files and save them as TIFF files in different folders based on the barcode type that has been detected within the image.

Copy Code
'We assume that GdPicture has been correctly installed and unlocked.
Dim oGdPictureImaging As New GdPictureImaging()
Dim filePath As [String] = "Pictures\barcodes"
Dim distPath As [String] = "Pictures\barcodeTypes\"
Dim allImages As [String]() = Directory.GetFiles(filePath)
Dim bcfound As Integer = 0
Dim m_Image As Integer = 0
Dim status As GdPictureStatus = GdPictureStatus.OK
For Each imagePath As String In allImages
    bcfound = 0
    m_Image = oGdPictureImaging.CreateGdPictureImageFromFile(imagePath)
    If oGdPictureImaging.GetStat() <> GdPictureStatus.OK Then
        MessageBox.Show("Error: " + oGdPictureImaging.GetStat().ToString(), "Barcode Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit For
    End If
    status = oGdPictureImaging.Barcode1DReaderDoScan(m_Image, Barcode1DReaderScanMode.BestSpeed)
    If status <> GdPictureStatus.OK Then
        MessageBox.Show("Error: " + status.ToString(), "Barcode Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
        oGdPictureImaging.ReleaseGdPictureImage(m_Image)
        Exit For
    End If
    bcfound = oGdPictureImaging.Barcode1DReaderGetBarcodeCount()
    If bcfound > 0 Then
        For i As Integer = 1 To bcfound
            Dim barcodeType As [String] = oGdPictureImaging.Barcode1DReaderGetBarcodeType(i).ToString()
            Dim folderName As [String] = [String].Concat(barcodeType, "\")
            Dim imageName As [String] = [String].Concat(i.ToString(), ".tif")
            Dim completePath As [String] = [String].Concat(Path.Combine(distPath, folderName), imageName)
            status = oGdPictureImaging.SaveAsTIFF(m_Image, completePath, TiffCompression.TiffCompressionAUTO)
            If status <> GdPictureStatus.OK Then
                MessageBox.Show("Error: " + status.ToString(), "Barcode Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit For
            End If
        Next
    End If
    oGdPictureImaging.ReleaseGdPictureImage(m_Image)
    If status <> GdPictureStatus.OK Then
        Exit For
    End If
Next
oGdPictureImaging.Dispose()
Copy Code
//We assume that GdPicture has been correctly installed and unlocked.
GdPictureImaging oGdPictureImaging = new GdPictureImaging();
String filePath = "Pictures\\barcodes";
String distPath = "Pictures\\barcodeTypes\\";
String[] allImages = Directory.GetFiles(filePath);
int bcfound = 0;
int m_Image = 0;
GdPictureStatus status = GdPictureStatus.OK;
foreach (string imagePath in allImages)
{
    bcfound = 0;
    m_Image = oGdPictureImaging.CreateGdPictureImageFromFile(imagePath);
    if (oGdPictureImaging.GetStat() != GdPictureStatus.OK)
    {
        MessageBox.Show("Error: " + oGdPictureImaging.GetStat().ToString(), "Barcode Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
        break;
    }
    status = oGdPictureImaging.Barcode1DReaderDoScan(m_Image, Barcode1DReaderScanMode.BestSpeed);
    if (status != GdPictureStatus.OK)
    {
        MessageBox.Show("Error: " + status.ToString(), "Barcode Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
        oGdPictureImaging.ReleaseGdPictureImage(m_Image);
        break;
    }
    bcfound = oGdPictureImaging.Barcode1DReaderGetBarcodeCount();
    if (bcfound > 0)
    {
        for (int i = 1; i <= bcfound; i++)
        {
            String barcodeType = oGdPictureImaging.Barcode1DReaderGetBarcodeType(i).ToString();
            String folderName = String.Concat(barcodeType, "\\");
            String imageName = String.Concat(i.ToString(), ".tif");
            String completePath = String.Concat(Path.Combine(distPath, folderName), imageName);
            status = oGdPictureImaging.SaveAsTIFF(m_Image, completePath, TiffCompression.TiffCompressionAUTO);
            if (status != GdPictureStatus.OK)
            {
                MessageBox.Show("Error: " + status.ToString(), "Barcode Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
                break;
            }
        }
    }
    oGdPictureImaging.ReleaseGdPictureImage(m_Image);
    if (status != GdPictureStatus.OK)
        break;
}
oGdPictureImaging.Dispose();