Barcode Recognition Sample Code
Using Barcode XpressTM
The included C# source code listed below demonstrates using the ImagXpress v8, ScanFix Xpress v5 and
Barcode Xpress v5 SDK’s to perform the following operations:
1) Opening a TIFF image with ImagXpress
2) Passing the image data to ScanFix to perform an image processing action on it
3) Passing the image to the BarcodeXpress control, reading the values and returning the results as a string
Please download the Barcode Xpress toolkit from www.pegasusimaging.com prior to getting started.
ImagXpress Standard and ScanFix Xpress Lite are included.
try
{
//Create the ImagXpress object which controls the licensing
PegasusImaging.WinForms.ImagXpress8.ImagXpress imagXpress1 = new PegasusImaging.WinForms.ImagXpress8.ImagXpress();
//Create the ImageX object which handles the actual image, load a TIF image
PegasusImaging.WinForms.ImagXpress8.ImageX image = PegasusImaging.WinForms.ImagXpress8.ImageX.FromFile("input.tif");
//Create the ScanFix object which handles image cleanup
PegasusImaging.WinForms.ScanFix5.ScanFix scanFix1 = new PegasusImaging.WinForms.ScanFix5.ScanFix();
//Pass the loaded image from ImagXpress to ScanFix
scanFix1.FromHdib(image.ToHdib(false));
//Despeckle the image for speckles of dimension 2x2
PegasusImaging.WinForms.ScanFix5.DespeckleOptions speckOpts = new PegasusImaging.WinForms.ScanFix5.DespeckleOptions(2, 2);
scanFix1.Despeckle(speckOpts);
//Pass the cleaned up image from ScanFix to ImagXpress
image = PegasusImaging.WinForms.ImagXpress8.ImageX.FromHdib(scanFix1.ToHdib(true));
//Create the BarcodeXpress object for reading barcodes
PegasusImaging.WinForms.BarcodeXpress5.BarcodeXpress barcodeXpress1 = new PegasusImaging.WinForms.BarcodeXpress5.BarcodeXpress();
//Set the barcode type to search for unknown 1D barcodes
barcodeXpress1.reader.BarcodeTypes = new PegasusImaging.WinForms.BarcodeXpress5.BarcodeType[] {
PegasusImaging.WinForms.BarcodeXpress5.BarcodeType.UnknownBarcode };
//Analyze the image for barcodes and return the result
System.Int32 DIB;
DIB = imageXView1.Image.ToHdib(false).ToInt32();
PegasusImaging.WinForms.BarcodeXpress5.Result[] result = barcodeXpress1.reader.Analyze(DIB);
//Create a string to store the results
string resultString;
//Iterate through the results array and output the type, location, and value of each barcode found
for (int i = 0; i < result.Length; i++)
{
resultString = "Type: " + result[i].BarcodeType.ToString() + "\nX: " + result[i].Area.X.ToString()
+ "\nY: " + result[i].Area.Y + "\nW: " + result[i].Area.Width.ToString() + "\nH: " + result[i].Area.Height.ToString()
+ "\nValue: " + result[i].BarcodeValue + "\nConfidence: " + result[i].Confidence.ToString() + "%";
MessageBox.Show(resultString, "Results", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//Free the DIB that was passed into the Analyze method
Marshal.FreeHGlobal(new System.IntPtr(DIB));
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
For more information, demos, and full-featured trial downloads visit our
website at www.pegasusimaging.com.