Hello,
Please find the attached sample image. In this image we have two types of barcode. line Barcode and DataMatrix barcode. In Our code we are using DataMatrix type to read the barcode. Below code is not working for DataMatrix type Barcode.
Dll version 4.2.0.0.
Aspose.BarCode.dll
Aspose.BarCodeRecognition.dll
You can test your DEMO site also with this image.
Kindly help out to resolve this issue as soon.
Thanks in Advance.
Regards,
P. Saravanan.
saravanan.palanisamy@xerox.com
Please reply to my email.
I have attached the sample file for your reference. Please check with this attachemnt image
Source code sample
private void button16_Click(object sender, EventArgs e)
{
Bitmap bitmap = null;
Bitmap newBitmap = null;
MemoryStream byteStream = null;
Aspose.BarCodeRecognition.BarCodeReader reader = null;
List<string> barcodes = new List<string>();
// -- get the image path from the IFO
OpenFileDialog flg = new OpenFileDialog();
flg.Filter = "All files (*.*)|*.*";
flg.InitialDirectory = "E:";
string FullImageName = string.Empty;
if (flg.ShowDialog() == DialogResult.OK)
FullImageName = flg.FileName;
try
{
//Convert to bitonal since color stacks would error or or not read
bitmap = (Bitmap)System.Drawing.Image.FromFile(FullImageName);
newBitmap = ConvertToBitonal(bitmap);
byteStream = new MemoryStream();
newBitmap.Save(byteStream, ImageFormat.Tiff);
// -- read the barcodes
//Aspose.BarCodeRecognition.BarCodeReader reader = new Aspose.BarCodeRecognition.BarCodeReader(FullImageName, Aspose.BarCodeRecognition.BarCodeReadType.Code39Extended);
//reader = new Aspose.BarCodeRecognition.BarCodeReader(byteStream, Aspose.BarCodeRecognition.BarCodeReadType.Code39Extended);
reader = new Aspose.BarCodeRecognition.BarCodeReader(byteStream, Aspose.BarCodeRecognition.BarCodeReadType.DataMatrix);
string bc = string.Empty;
// -- loop the barcodes found
while (reader.Read())
{
bc = reader.GetCodeText().Trim();
MessageBox.Show("Barcode value" + bc.ToString());
// -- we want to check the barcode against the regex
// -- if we don't find a match, throw this barcode away
// -- but only do this logic if they have at least one regex defined
if (bc.Trim() != string.Empty)
barcodes.Add(bc.Trim());
}
}
finally
{
reader.Close();
reader = null;
bitmap = null;
newBitmap = null;
if (byteStream != null)
{
byteStream.Close();
}
byteStream = null;
}
}
#region ConvertToBitonial
public static Bitmap ConvertToBitonal(Bitmap original)
{
Bitmap source = null;
// If original bitmap is not already in 32 BPP, ARGB format, then convert
if (original.PixelFormat != PixelFormat.Format32bppArgb)
{
source = new Bitmap(original.Width, original.Height, PixelFormat.Format32bppArgb);
source.SetResolution(original.HorizontalResolution, original.VerticalResolution);
using (Graphics g = Graphics.FromImage(source))
{
g.DrawImageUnscaled(original, 0, 0);
}
}
else
{
source = original;
}
// Lock source bitmap in memory
BitmapData sourceData = source.LockBits(new System.Drawing.Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
// Copy image data to binary array
int imageSize = sourceData.Stride * sourceData.Height;
byte[] sourceBuffer = new byte[imageSize];
Marshal.Copy(sourceData.Scan0, sourceBuffer, 0, imageSize);
// Unlock source bitmap
source.UnlockBits(sourceData);
// Create destination bitmap
Bitmap destination = new Bitmap(source.Width, source.Height, PixelFormat.Format1bppIndexed);
// Lock destination bitmap in memory
BitmapData destinationData = destination.LockBits(new System.Drawing.Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed);
// Create destination buffer
imageSize = destinationData.Stride * destinationData.Height;
byte[] destinationBuffer = new byte[imageSize];
int sourceIndex = 0;
int destinationIndex = 0;
int pixelTotal = 0;
byte destinationValue = 0;
int pixelValue = 128;
int height = source.Height;
int width = source.Width;
int threshold = 500;
// Iterate lines
for (int y = 0; y < height; y++)
{
sourceIndex = y * sourceData.Stride;
destinationIndex = y * destinationData.Stride;
destinationValue = 0;
pixelValue = 128;
// Iterate pixels
for (int x = 0; x < width; x++)
{
// Compute pixel brightness (i.e. total of Red, Green, and Blue values)
pixelTotal = sourceBuffer[sourceIndex + 1] + sourceBuffer[sourceIndex + 2] + sourceBuffer[sourceIndex + 3];
if (pixelTotal > threshold)
{
destinationValue += (byte)pixelValue;
}
if (pixelValue == 1)
{
destinationBuffer[destinationIndex] = destinationValue;
destinationIndex++;
destinationValue = 0;
pixelValue = 128;
}
else
{
pixelValue >>= 1;
}
sourceIndex += 4;
}
if (pixelValue != 128)
{
destinationBuffer[destinationIndex] = destinationValue;
}
}
// Copy binary image data to destination bitmap
Marshal.Copy(destinationBuffer, 0, destinationData.Scan0, imageSize);
// Unlock destination bitmap
destination.UnlockBits(destinationData);
// Return
return destination;
}
#endregion