In this post, we discuss how to leverage Dynamsoft Barcode Reader video decoding APIs to implement the barcode scanning
functionality in camera preview scenario. Also, we demonstrate how to implement for desktop and mobile platforms respectively
with the code snippet.
This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with
information on products and services that we consider useful and of value to developers.
Barcode detection and recognition technology, especially the application of real-time barcode scanning, is imperative to industries
such as warehouse management and mobile payment. Dynamsoft Barcode Reader, as a cross-platform and cross-programing
language SDK, is a lifesaver for developers who are dedicated to building enterprise-class barcode solutions in a short time.
In this post, we discuss how to leverage Dynamsoft Barcode Reader video decoding APIs to implement the barcode scanning
functionality in camera preview scenario. Also, we demonstrate how to implement for desktop and mobile platforms respectively
with the code snippet.
To eliminate the complexity of multi-threaded programming and improve developers’ efficiency, Dynamsoft Barcode Reader 7.0
brings a set of thread-based APIs for decoding continuous frames.
License
Get a FREE 30-day trial license.
https://www.codeproject.com/Articles/5163187/Real-time-Barcode-Scanning-from-Camera-Stream-2?display=Print 1/12
31/07/2019 Real-time Barcode Scanning from Camera Stream - CodeProject
Developer’s Guide
https://www.dynamsoft.com/help/Barcode-Reader/devguide/index.html
API Documentation
https://www.dynamsoft.com/help/Barcode-Reader/index.html
Code Gallery
https://www.dynamsoft.com/Downloads/Dynamic-Barcode-Reader-Sample-Download.aspx
Online Demo
https://demo.dynamsoft.com/DBR/BarcodeReaderDemo.aspx
The user main thread is initiated when StartFrameDecoding() is being called. Invoke AppendFrame() to add frames to
an auto-managed frame queue in a video frame callback function.
Decoding Thread
The decoding thread takes all CPU-intensive work, running barcode algorithms. It uses some strategies to select and discard frames
in order to avoid lagging.
https://www.codeproject.com/Articles/5163187/Real-time-Barcode-Scanning-from-Camera-Stream-2?display=Print 2/12
31/07/2019 Real-time Barcode Scanning from Camera Stream - CodeProject
import cv2
import dbr
import time
import os
def read_barcode():
video_width = 640
video_height = 480
vc = cv2.VideoCapture(0)
vc.set(3, video_width) #set width
vc.set(4, video_height) #set height
if vc.isOpened():
dbr.initLicense('LICENSE-KEY')
rval, frame = vc.read()
else:
return
max_buffer = 2
max_results = 10
barcodeTypes = 0x3FF | 0x2000000 | 0x4000000 | 0x8000000 | 0x10000000 # 1D, PDF417, QRCODE,
DataMatrix, Aztec Code
image_format = 1 # 0: gray; 1: rgb888
while True:
cv2.imshow(windowName, frame)
rval, frame = vc.read()
start = time.time()
try:
ret = dbr.appendVideoFrame(frame)
except:
pass
https://www.codeproject.com/Articles/5163187/Real-time-Barcode-Scanning-from-Camera-Stream-2?display=Print 3/12
31/07/2019 Real-time Barcode Scanning from Camera Stream - CodeProject
dbr.stopVideoMode()
dbr.destroy()
cv2.destroyWindow(windowName)
if __name__ == "__main__":
print("OpenCV version: " + cv2.__version__)
read_barcode()
The dbr module is the Python extension built with Dynamsoft Barcode Reader. We created three methods:
startVideoMode(), appendVideoFrame() and stopVideoMode(). The corresponding C/C++ code is as follows.
static PyObject *
startVideoMode(PyObject *self, PyObject *args)
{
printf("Start the video mode\n");
CHECK_DBR();
updateFormat(iFormat);
if (!PyCallable_Check(callback))
{
PyErr_SetString(PyExc_TypeError, "parameter must be callable");
return NULL;
}
else
https://www.codeproject.com/Articles/5163187/Real-time-Barcode-Scanning-from-Camera-Stream-2?display=Print 4/12
31/07/2019 Real-time Barcode Scanning from Camera Stream - CodeProject
{
Py_XINCREF(callback); /* Add a reference to new callback */
Py_XDECREF(py_callback); /* Dispose of previous callback */
py_callback = callback;
}
if (imageformat == 0)
{
stride = width;
format = IPF_GRAYSCALED;
}
else {
stride = width * 3;
format = IPF_RGB_888;
}
appendVideoFrame()
static PyObject *
appendVideoFrame(PyObject *self, PyObject *args)
{
CHECK_DBR();
PyObject *o;
if (!PyArg_ParseTuple(args, "O", &o))
return NULL;
#if defined(IS_PY3K)
//Refer to numpy/core/src/multiarray/ctors.c
Py_buffer *view;
int nd;
PyObject *memoryview = PyMemoryView_FromObject(o);
if (memoryview == NULL) {
PyErr_Clear();
return -1;
}
view = PyMemoryView_GET_BUFFER(memoryview);
unsigned char *buffer = (unsigned char *)view->buf;
nd = view->ndim;
int len = view->len;
int stride = view->strides[0];
int width = view->strides[0] / view->strides[1];
int height = len / stride;
#else
if (pai->two != 2) {
PyErr_SetString(PyExc_TypeError, "object does not have array interface");
Py_DECREF(ao);
https://www.codeproject.com/Articles/5163187/Real-time-Barcode-Scanning-from-Camera-Stream-2?display=Print 5/12
31/07/2019 Real-time Barcode Scanning from Camera Stream - CodeProject
return NULL;
}
// Detect barcodes
ImagePixelFormat format = IPF_RGB_888;
if (width == stride)
{
format = IPF_GRAYSCALED;
}
else if (width == stride * 3)
{
format = IPF_RGB_888;
}
else if (width == stride * 4)
{
format = IPF_ARGB_8888;
}
stopVideoMode()
static PyObject *
stopVideoMode(PyObject *self, PyObject *args)
{
printf("Stop the video mode\n");
if (hBarcode)
{
int ret = DBR_StopFrameDecoding(hBarcode);
return Py_BuildValue("i", ret);
}
return 0;
}
You have to call Python code between PyGILState_Ensure() and PyGILState_Release() when executing the
callback function on the native thread:
// https://docs.python.org/2/c-api/init.html
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
PyGILState_Release(gstate);
/////////////////////////////////////////////
// Release memory
DBR_FreeTextResults(&pResults);
}
Source Code
https://github.com/dynamsoft-dbr/python/tree/7.x
cameraView.addFrameProcessor(new FrameProcessor() {
@SuppressLint("NewApi")
@Override
public void process(@NonNull final Frame frame) {
try {
if (isDetected && isCameraOpen) {
YuvImage yuvImage = new YuvImage(frame.getData(), ImageFormat.NV21,
frame.getSize().getWidth(), frame.getSize().getHeight(), null);
if (width == 0) {
width = yuvImage.getWidth();
height = yuvImage.getHeight();
stride = yuvImage.getStrides()[0];
reader.setErrorCallback(errorCallback, null);
reader.setTextResultCallback(textResultCallback, null);
reader.setIntermediateResultCallback(intermediateResultCallback,
null);
reader.startFrameDecoding(10, 10, width, height, stride,
EnumImagePixelFormat.IPF_NV21, "");
} else {
PublicRuntimeSettings s = reader.getRuntimeSettings();
int frameid = reader.appendFrame(yuvImage.getYuvData());
Log.i("FrameId", frameid + "");
}
if (bUpateDrawBox) {
bUpateDrawBox = false;
Message message = handler.obtainMessage();
Rect imageRect = new Rect(0, 0, width, height);
message.obj = imageRect;
message.what = 0x001;
handler.sendMessage(message);
}
isDetected = true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
https://www.codeproject.com/Articles/5163187/Real-time-Barcode-Scanning-from-Camera-Stream-2?display=Print 7/12
31/07/2019 Real-time Barcode Scanning from Camera Stream - CodeProject
Call startDecodingFrame() to initialize the environment and call appendFrame() repeatedly to add the preview
images.
https://www.codeproject.com/Articles/5163187/Real-time-Barcode-Scanning-from-Camera-Stream-2?display=Print 8/12
31/07/2019 Real-time Barcode Scanning from Camera Stream - CodeProject
Source Code
https://github.com/dynamsoft-dbr/android-barcode-decode-video
https://www.codeproject.com/Articles/5163187/Real-time-Barcode-Scanning-from-Camera-Stream-2?display=Print 9/12
31/07/2019 Real-time Barcode Scanning from Camera Stream - CodeProject
Call the following to initialize the environment and call -(NSInteger)appendFrame:(NSData*) bufferBytes
repeatedly to add the preview images.
https://www.codeproject.com/Articles/5163187/Real-time-Barcode-Scanning-from-Camera-Stream-2?display=Print 10/12
31/07/2019 Real-time Barcode Scanning from Camera Stream - CodeProject
(void)startFrameDecoding:(NSInteger)maxQueueLength
maxResultQueueLength:(NSInteger)maxResultQueueLength
width:(NSInteger)width
height:(NSInteger)height
stride:(NSInteger)stride
format:(EnumImagePixelFormat)format
templateName:(NSString* _Nonnull)templateName
error:(NSError* _Nullable * _Nullable)error
Source Code
https://github.com/dynamsoft-dbr/ios-barcode-decode-video
Technical Support
If you have any questions about Dynamsoft Barcode Reader SDK, please feel free to contact support@dynamsoft.com.
Release History
v7.0, 07/12/2019
New
Refactored most modules to provide a flexible barcode reading framework that allows for parameter customization suited
for a variety of barcode scenarios
Enabled access to intermediate results (grayscale image, binarized image, text zone, etc) during the decoding process
Added new interfaces to support video decoding, and frame decoding to improve interactive sensitivity
Provided methods to terminate the decoding process at different phases such as during binarization, localization or barcode
type identification.
Added a new barcode localization method, Scan Directly, to reduce decoding time significantly for high-quality images.
Improved
Enhanced error messages related to license initiation failure.
Improved detailed results for decoded barcodes, including more barcode format specification
Improved results output to enable outputting barcode results in the order of confidence level, barcode position or format.
Fix
Fixed an issue where the barcode could be calculated incorrectly on some occasions.
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
https://www.codeproject.com/Articles/5163187/Real-time-Barcode-Scanning-from-Camera-Stream-2?display=Print 11/12
31/07/2019 Real-time Barcode Scanning from Camera Stream - CodeProject
Xiao Ling
Technical Writer Dynamsoft
Canada
Xiao Ling is A technical content writer at Dynamsoft, the leading company of document capture and image processing SDKs.
He is in charge of managing Dynamsoft blog site codepool.biz and Dynamsoft GitHub community.
https://www.codeproject.com/Articles/5163187/Real-time-Barcode-Scanning-from-Camera-Stream-2?display=Print 12/12