Barcode Detection
Note
This API requires a valid software license.
The barcode detection API allows you to detect and decode barcodes in a 2D image.
Barcodes are a visual, machine-readable representation of data, typically in a pattern of parallel lines, spaces or shapes that encode information such as product numbers. Zivid SDK supports the following linear (1D) and matrix (2D) barcode formats:
Linear: EAN-13, EAN-8, Code 128, Code 93, Code 39, UPC-A, UPC-E
Matrix: QR Code, Data Matrix
Examples of supported barcode formats.
Barcodes may come in various sizes, where the width of the smallest bar or space is the main sizing parameter. For example, standard retail linear barcodes typically vary between 7 mil (0.178 mm) and 13 mil (0.330 mm). Smaller barcodes require higher resolution to be detected and decoded reliably.
Examples of different Code 128 barcode sizes captured from the same distance.
Barcode Detection API
Start by initializing the barcode detector. Note that this should only be done once and not for every capture.
Then specify which of the supported barcode formats to detect, rather than looking for all formats. This will speed up detection and make decoding more robust. In this example we generalize and select all supported formats for simplicity.
// Select your specific barcode formats for optimal performance
const auto linearFormatFilter = LinearBarcodeFormat::code128 | LinearBarcodeFormat::code93
| LinearBarcodeFormat::code39 | LinearBarcodeFormat::ean13
| LinearBarcodeFormat::ean8 | LinearBarcodeFormat::upcA
| LinearBarcodeFormat::upcE;
const auto matrixFormatFilter = MatrixBarcodeFormat::qrcode | MatrixBarcodeFormat::dataMatrix;
# Select your specific barcode formats for optimal performance
linear_format_filter = {
LinearBarcodeFormat.code128,
LinearBarcodeFormat.code93,
LinearBarcodeFormat.code39,
LinearBarcodeFormat.ean13,
LinearBarcodeFormat.ean8,
LinearBarcodeFormat.upcA,
LinearBarcodeFormat.upcE,
}
matrix_format_filter = {MatrixBarcodeFormat.qrcode, MatrixBarcodeFormat.dataMatrix}
Successfully detecting and decoding barcodes depends heavily on a sharp and well-exposed 2D image. Use the suggested 2D settings from the barcode detector and capture a 2D frame.
Then perform barcode detection and decoding on the captured 2D frame. Here we read both linear and matrix barcodes, but make sure to only read your specific barcode type.
Finally, print out the resulting barcode codes, formats and pixel positions.
if(!linearBarcodeResults.empty())
{
std::cout << "Detected " << linearBarcodeResults.size() << " linear barcodes:" << std::endl;
for(const auto &result : linearBarcodeResults)
{
std::cout << "-- Detected barcode " << result.code() << " on format " << toString(result.codeFormat())
<< " at pixel " << result.centerPosition() << std::endl;
}
}
if(!matrixBarcodeResults.empty())
{
std::cout << "Detected " << matrixBarcodeResults.size() << " matrix barcodes:" << std::endl;
for(const auto &result : matrixBarcodeResults)
{
std::cout << "-- Detected barcode " << result.code() << " on format " << toString(result.codeFormat())
<< " at pixel " << result.centerPosition() << std::endl;
}
}
if linear_barcode_results:
print(f"Detected {len(linear_barcode_results)} linear barcodes:")
for result in linear_barcode_results:
print(
f"-- Detected barcode {result.code()} on format {result.code_format()} at pixel {result.center_position()}"
)
if matrix_barcode_results:
print(f"Detected {len(matrix_barcode_results)} matrix barcodes:")
for result in matrix_barcode_results:
print(
f"-- Detected barcode {result.code()} on format {result.code_format()} at pixel {result.center_position()}"
)
Barcode Detection in Zivid Studio
Tip
Barcode detection in Zivid Studio does not require a software license.
You can easily test barcode detection using Zivid Studio. Connect to a camera and perform a 2D capture of a scene containing barcodes by selecting the barcode preset. Then click on View → Show barcodes to visualize decoded barcodes in the image.
Detected barcodes visualized in Zivid Studio.
You can speed up the detection by selecting your specific barcode format in View → Configure barcodes to detect.
Barcode Detection Performance
The performance of detection and decoding depends on multiple factors, including:
Barcode size
Distance to barcode
Image resolution
Image focus
Horizontal and vertical FOV
These factors effectively make up the spatial resolution on the barcode surface, which is important to maximize for reliable detection and decoding.
Image resolution and focus can be controlled by the camera settings, where we recommend BarcodeDetector::suggestSettings(camera) for optimal capture settings.
The camera’s horizontal and vertical FOV are fixed for each camera model, where larger FOVs lead to lower spatial resolution.
Distance to the barcode can be controlled by the application within some constraints, but is also limited by the camera’s working range.
The recommended cameras for barcode detection are therefore Zivid 2+ MR130 and MR60, as these provide sufficient spatial resolution within their working ranges. Below are some benchmarks for a variety of static barcodes qualified at different distances, orientations and placements within the FOV.
Tip
Stay within the green area for reliable decoding results.
Version History
SDK |
Changes |
|---|---|
2.17.0 |
Barcode detection API is added. |