Read Barcode from Different Image Sources
The DBR algorithm provides multiple ways to read barcode from different image sources. This article will introduce the following methods.
DecodeFile
: Decode barcodes from a still image. You have to specify the file path when using this method to decode the barcodes.DecodeFileInMemory
: Decode barcodes from an image file in memory.DecodeBuffer
: Decode barcodes with buffered image data and the width, height, strides and pixel format of the image.
DecodeFile
- Android
- Objective-C
- Swift
- C
- C++
- C#
- Java
- Python
try{ BarcodeReader reader = new BarcodeReader(); TextResult[] result = reader.decodeFile("your file path"); } catch (BarcodeReaderException ex) { ex.printStackTrace(); }
NSError* err=nil; DynamsoftBarcodeReader *reader = [[DynamsoftBarcodeReader alloc] init]; NSArray<iTextResult*>* result = [reader decodeFileWithName:@"your file path" error:&err];
let reader = DynamsoftBarcodeReader.init() let result = try? reader.decodeFileWithName("your file path")
int errorCode = DBR_DecodeFile(barcodeReader, "C:\\Program Files (x86)\\Dynamsoft\\{Version number}\\Images\\AllSupportedBarcodeTypes.tif", "");
int errorCode = reader->DecodeFile("C:\\Program Files (x86)\\Dynamsoft\\{Version number}\\Images\\AllSupportedBarcodeTypes.tif", "");
try{ TextResult[] result = reader.DecodeFile(@"C:\Program Files (x86)\Dynamsoft\{Version number}\Images\AllSupportedBarcodeTypes.tif", ""); } catch (BarcodeReaderException exp) { Console.WriteLine(exp.Message); }
try{ TextResult[] result = reader.decodeFile("your file path", ""); } catch (BarcodeReaderException ex) { ex.printStackTrace(); }
try: results = dbr.decode_file(image_path) except BarcodeReaderError as bre: print(bre)
DecodeFileInMemory
DecodeFileInMemory
is the method designed for decoding barcodes from the raw image data in bytes. The required parameters are as follows:
fileBytes
: The image data in bytes. When images are read in memory, they are transferred into byte data.templateName
: The barcode decoding template name.
- Android
- Objective-C
- Swift
- C
- C++
- C#
- Java
- Python
try{ BarcodeReader reader = new BarcodeReader(); //TODO: read the image file and get the byte data `fileBytes` TextResult[] result = reader.decodeFileInMemory(fileBytes); } catch (BarcodeReaderException ex) { ex.printStackTrace(); }
NSError* err=nil; DynamsoftBarcodeReader *reader = [[DynamsoftBarcodeReader alloc] init]; //TODO: read the image file and get the byte data `fileBytes` NSArray<iTextResult*>* result = [reader decodeFileInMemory:fileBytes error:&err];
let reader = DynamsoftBarcodeReader() //TODO: read the image file and get the byte data `fileBytes` let result = try? reader.decodeFileInMemory(fileBytes)
barcodeReader = DBR_CreateInstance(); FILE* fp; fopen_s(&fp, "../../../images/AllSupportedBarcodeTypes.png", "rb"); if (fp == NULL) { perror(""); return NULL; } fseek(fp, 0, SEEK_END); long int fileSize = ftell(fp); rewind(fp); unsigned char* fileByte = NULL; fileByte = (unsigned char*)malloc(fileSize + 1); fread(fileByte, 1, fileSize, fp); fileByte[fileSize] = '\0'; fclose(fp); errorCode = DBR_DecodeFileInMemory(barcodeReader, fileByte, fileSize, "");
FILE* fp; fopen_s(&fp, path, "rb"); if (fp == NULL) { perror(""); return NULL; } fseek(fp, 0, SEEK_END); long int fileSize = ftell(fp); rewind(fp); unsigned char* fileByte = NULL; fileByte = new unsigned char[fileSize + 1]; fread(fileByte, 1, fileSize, fp); fileByte[fileSize] = '\0'; fclose(fp); int errorCode = reader->DecodeFileInMemory(fileByte, fileSize, "");
try{ TextResult[] results = null; FileStream fileStream = new FileStream("your-file-path", FileMode.Open, FileAccess.Read); byte[] byteBuffer = new byte[fileStream.Length]; fileStream.Read(byteBuffer, 0, (int)fileStream.Length); fileStream.Close(); results = dbr.DecodeFileInMemory(byteBuffer, ""); } catch (BarcodeReaderException exp) { Console.WriteLine(exp.Message); }
private static byte[] getFileBytes(String filePath) { byte[] buffer = null; FileInputStream fis = null; ByteArrayOutputStream bos = null; try { fis = new FileInputStream(new File(filePath)); bos = new ByteArrayOutputStream(); byte[] tempBuffer = new byte[1024]; int iReadSize; while ((iReadSize = fis.read(tempBuffer)) != -1) { bos.write(tempBuffer, 0, iReadSize); } buffer = bos.toByteArray(); } catch (IOException ex) { ex.printStackTrace(); } finally { if (null != bos) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != fis) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } return buffer; } TextResult[] result = reader.decodeFileInMemory(getFileBytes("your-file-path"), "");
// Read the image file and transfer it into byte with open(image_path,"rb") as f: bytes = f.read() results = dbr.decode_file_stream(bytearray(bytes))
DecodeBuffer
DecodeBuffer
is the method designed for decoding barcodes from the memory buffer containing image pixels in a defined format. This API is generally used in video stream decoding. After obtaining a frame of image data, you can invoke this API to decode the frame. The required parameters are as follows:
- BufferBytes: The array of bytes that contains the image data.
- Width: The width of the image (in pixel).
- Height: The height of the image (in pixel).
- Stride: The stride (or scan width) of the image.
- Format: The image pixel format used in the image byte array. View all available templates in
EnumImagePixelFormat
. - TemplateName: The template name. It indicates which barcode decoding template you are going to use when decoding the buffer.
- Android
- Objective-C
- Swift
- C
- C++
- C#
- Java
- Python
try{ BarcodeReader reader = new BarcodeReader(); //TODO: parse the image and convert it into raw buffer data `bufferBytes`. The arrangement of the pixels in the `bufferBytes` is determined by the `format` parameter. TextResult[] result = reader.decodeBuffer(bufferBytes, width, height, stride, format); } catch (BarcodeReaderException ex) { ex.printStackTrace(); }
NSError* err=nil; DynamsoftBarcodeReader *reader = [[DynamsoftBarcodeReader alloc] init]; //TODO: parse the image and convert it into raw buffer data `bufferBytes`. The arrangement of the pixels in the `bufferBytes` is determined by the `format` parameter. NSArray<iTextResult*>* result = [reader decodeBuffer:bufferBytes withWidth:width height:height stride:stride format:format error:&err];
let reader = DynamsoftBarcodeReader() //TODO: parse the image and convert it into raw buffer data `bufferBytes`. The arrangement of the pixels in the `bufferBytes` is determined by the `format` parameter. let result = try? reader.decodeBuffer(bufferBytes, withWidth:width, height:height, stride:stride, format:format)
barcodeReader = DBR_CreateInstance(); int errorCode = DBR_DecodeBuffer(barcodeReader, pBufferBytes, iWidth, iHeight, iStride, format, "");
int errorCode = reader->DecodeBuffer(pBufferBytes, iWidth, iHeight, iStride, format, "");
Bitmap bBMP = new Bitmap(@"C:\Program Files (x86)\Dynamsoft\{Version number}\Images\AllSupportedBarcodeTypes.tif"); BitmapData bmdat = bBMP.LockBits(new Rectangle(Point.Empty, bBMP.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); byte[] buffer = new byte[stride * bmdat.Height]; try{ TextResult[] result = reader.DecodeBuffer(buffer, bBMP.Width, bBMP.Height, bmdat.Stride, EnumImagePixelFormat.IPF_ARGB_8888, ""); } catch (BarcodeReaderException exp) { Console.WriteLine(exp.Message); }
ImageData image = cvtToImageData(filePath); try { TextResult[] results = dbr.decodeBuffer(image.bytes, image.width, image.height, image.stride, image.format, ""); } catch (BarcodeReaderException ex){ ex.printStackTrace(); } catch (IOException ex){ ex.printStackTrace(); }
# Decoding with opencv image. # Convert an image to a cv buffer. cv_buffer = cv2.imread("The file path of the image") # Use DBR decode_buffer to decode the buffered image. results = dbr.decode_buffer(cv_buffer)