Thanks for downloading Dynamsoft Barcode Reader Package!
Your download will start shortly. If your download does not begin, click here to retry.
The DBR algorithm provides multiple ways to read images from different sources. This article will introduce the following methods.
For image files, DBR provides the following two APIs.
Sample codes:
//DecodeFile
CBarcodeReader* reader = new CBarcodeReader();
reader->InitLicense("enter your license");
int errorCode = reader->DecodeFile("image path", "");
delete reader;
//DecodeFileInMemory
CBarcodeReader* reader = new CBarcodeReader();
reader->InitLicense("enter your license");
unsigned char* pFileBytes;
int nFileSize = 0;
GetFileStream("image path", &pFileBytes, &nFileSize); //read file to memory
int errorCode = reader->DecodeFileInMemory(pFileBytes, nFileSize, "");
delete reader;
For the image data in the memory, DBR provides the following API.
DecodeBuffer
ImagePixelFormat
The following uses a 152 * 152 QR code to illustrate these parameters. The image has three channels of RGB, with a 24bit image depth (each pixel occupies 24bit of memory space). So in this example, the format value is IPF_RGB888
The number of bits of the memory size occupied by one line of the image is iwidth * depth, so In this example, the image is width is 152px, times 24 bits, resulting with 3648 bits.
If we now use int ( 32 bit / int )
type array to store the pixel data of the image, the length of the int
array required for a row of image pixel data is (iwidth * depth + 31) / 32, the actual number of single-line bytes of the image pixel data iStride is iStride = (iwidth * depth + 31) / 32 * 4
Then the structure of the pixel data pBufferBytes of the image is int* pBufferBytes = new int[(width * depth + 31) / 32 * iHeight];
After the construction of pBufferBytes is completed, read the image data and fill it in.
In the following example, we use opencv to read image data and call the DecodeBuffer
#include "DynamsoftBarcodeReader.h"
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
#ifdef _WIN64
#pragma comment(lib, "DBRx64.lib")
#else
#pragma comment(lib, "DBRx86.lib")
#endif
int main()
{
int ret = 0;
CBarcodeReader* reader = new CBarcodeReader();
reader->InitLicense("your license here");
Mat image = cv::imread("your img file path");
if (image.type() == CV_8UC3) {
ret = reader->DecodeBuffer(image.data, image.cols, image.rows, image.cols * 3, IPF_RGB_888);
}
else if (image.type() == CV_8UC1) {
ret = reader->DecodeBuffer(image.data, image.cols, image.rows, image.cols, IPF_GRAYSCALED);
}
TextResultArray* pResults = NULL;
reader->GetAllTextResults(&pResults);
CBarcodeReader::FreeTextResults(&pResults);
delete(reader);
return 0;
}
DecodeBase64String
DecodeDIB
DBR provides several processing interfaces for video streams, which facilitates the processing of video frame queues and multi-threading.
DBR creates a queue to be processed to store the video frame currently being processed, and then create a decoding thread to process the queue. The decoding thread takes the latest frame in the queue for processing each time, and then moves it out of the queue after decoding to perform the next processing. The results will be put into a result queue. If the length of the queue to be processed reaches the maximum value you set, the decoding thread will exit the current processing frame as soon as possible, and empty the queue to be processed, and then waiting for new frames to join the queue and restart processing to prevent a frame from taking too long.
DBR also provides the feature to filter blurred frames. When enabled, DBR will calculate the sharpness of the image frames in the queue to be processed, and the frames whose sharpness is lower than the threshold you set will be removed. The main APIs are as follows
AppendFrame
StartFrameDecoding
StartFrameDecodingEx
SetTextResultCallback
StopFrameDecoding
InitFrameDecodingParameters
FrameDecodingParameters
objectFrameDecodingParameters
maxQueueLength
maxResultQueueLength
width
height
stride
imagePixelFormat
region
autoFilter
threshold
fps
AppendFrame
.The following example demonstrates these interfaces. In this example, we use opencv to read camera data and call DBR’s video streaming interface to decode.
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <vector>
#include "Include/DynamsoftBarcodeReader.h"
using namespace cv;
using namespace std;
using std::cerr;
using std::endl;
using std::cin;
#ifdef _WIN64
#pragma comment(lib, "Lib/DBRx64.lib")
#else
#pragma comment(lib, "Lib/DBRx86.lib")
#endif
//Callback function when decoding is completed
void textResultcallback(int frameId, TextResultArray *pResults, void * pUser)
{
for (int iIndex = 0; iIndex < pResults->resultsCount; iIndex++)
{
printf("Barcode %d, Value %s\n", iIndex + 1, pResults->results[iIndex]->barcodeText);
}
CBarcodeReader::FreeTextResults(&pResults);
}
//Callback function when decoding error
void errorcb(int frameId, int errorCode, void * pUser)
{
printf("frame = %d errorcode = %d, %s\n", frameId, errorCode, CBarcodeReader::GetErrorString(errorCode));
}
int main()
{
Mat frame;
cout << "Opening camera..." << endl;
VideoCapture capture(0); // open the first camera
if (!capture.isOpened())
{
cerr << "ERROR: Can't initialize camera capture" << endl;
return 1;
}
int iRet = -1;
CBarcodeReader reader;
iRet = reader.InitLicense("enter your license");
if (iRet != 0)
{
printf("%s\n", CBarcodeReader::GetErrorString(iRet));
return -1;
}
reader.InitRuntimeSettingsWithString("enter your template path", CM_OVERWRITE);
reader.SetTextResultCallback(textResultcallback, NULL); // Set callback when decoding is complete
reader.SetErrorCallback(errorcb, NULL); // Set the callback when an error occurs
FrameDecodingParameters parameters;
reader.InitFrameDecodingParameters(¶meters);
capture >> frame;
parameters.width = capture.get(CAP_PROP_FRAME_WIDTH);
parameters.height = capture.get(CAP_PROP_FRAME_HEIGHT);
parameters.maxQueueLength = 30;
parameters.maxResultQueueLength = 30;
parameters.stride = frame.step.p[0];
parameters.imagePixelFormat = IPF_RGB_888;
iRet = reader.StartFrameDecodingEx(parameters, "");
if (iRet != 0)
{
printf("%s\n", CBarcodeReader::GetErrorString(iRet));
return -1;
}
for(;;)
{
capture >> frame; // read the next frame from camera
if (frame.empty())
{
cerr << "ERROR: Can't grab camera frame." << endl;
break;
}
reader.AppendFrame(frame.data);
imshow("Frame", frame);
int key = waitKey(1);
if (key == 27/*ESC*/)
break;
}
reader.StopFrameDecoding();
return 0;
}
version 7.6.0