How to Benchmark Barcode SDK Performance - ZXing vs ZBar

I saw many posts arguing the performance winner between open-source barcode SDKs - ZXing and ZBar. As an engineer, who is developing commercial barcode reader software for Dynamsoft, I am curious about which open source project is better, ZXing or ZBar? Considering ZXing is implemented in Java, whereas ZBar is implemented in C/C++. To fairly compare their performance, I decided to use JNI to wrap ZBar C/C++ source code and benchmark them in a Java program.

zxing zbar

Prerequisites of Barcode SDK

ZXing Source code

ZBar Source Code

ZBar Windows Installer

How to Decode TIFF in Java

I need to use a dataset that includes many TIFF files for testing barcode reading performance. The Java Class ImageIO could read many image formats such as JPEG, PNG, BMP, but not TIFF. I searched Oracle’s Website and found Java Advanced Imaging (JAI) API, which provides methods for decoding TIFF files.

Where to download JAI jar packages?

I was surprised that the download links of Oracle official Website pointed to 404 error page. If you Google Java JAI, you may find there is no valid link on the first page of searching results. I patiently looked for download links page by page, and even changed the search engine. Luckily there is a working page existed: To make JAI work, you need to download jai_codec-1.1.3.jar and jai_core-1.1.3.jar. Here is the source code demonstrating how to read TIFF file to int[]:

File file = new File(fileName);
RenderedImage tiff = JAI.create("tiffload", fileName);
BufferedImage image = PlanarImage.wrapRenderedImage(tiff).getAsBufferedImage();
int\[\] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());

How to Read Multiple Barcodes of an Image with ZXing

Previously, I shared a post - How to Write and Read QR Code with ZXing in Java, which demonstrates how to read barcodes with MultiFormatReader. But MultiFormatReader only returns one result. What if we want to read multiple barcodes from an image, such as a document with code39, code93 and QR code? In this case, we need to use another reader class GenericMultipleBarcodeReader.

About GenericMultipleBarcodeReader:

Attempts to locate multiple barcodes in an image by repeatedly decoding portion of the image. After one barcode is found, the areas left, above, right and below the barcode’s ResultPoints are scanned, recursively.

To read multiple barcode results, you can write Java code as follows:

         RGBLuminanceSource source = new RGBLuminanceSource(image.getWidth(),
				image.getHeight(), pixels);
		bitmap = new BinaryBitmap(new HybridBinarizer(source));

		Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
		hints.put(DecodeHintType.TRY\_HARDER, null);
		Collection<BarcodeFormat> formats = new ArrayList<>();

		hints.put(DecodeHintType.POSSIBLE\_FORMATS, formats);
		MultiFormatReader reader = new MultiFormatReader(); 

		// read multi barcodes
		GenericMultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(
		try {
			Result\[\] results = multiReader.decodeMultiple(bitmap, hints);
			System.out.println(ZXING + TIME\_COST
					+ ((System.nanoTime() - start) / 1000000) + MS);
			if (results != null) {
				for (Result result : results) {
					System.out.println(ZXING + TYPE + result.getBarcodeFormat() + VALUE + result.getText());
		} catch (NotFoundException e) {

Creating Java Native Interface (JNI) for ZBar

How to use ZBar to decode barcodes in C/C++? If no idea, you can refer to the sample scan_image.cpp provided by ZBar in source code or installer. With a few changes, the JNI sample may be as follows:

#include <iostream>
#include <Magick++.h>
#include <zbar.h>
#include <jni.h>
#define STR(s) #s

using namespace std;
using namespace zbar;

#ifndef DEBUG
#define DEBUG(...) printf(\_\_VA\_ARGS\_\_)

extern "C" {
	JNIEXPORT jobjectArray JNICALL Java\_com\_dynamsoft\_zbar\_ZBarReader\_decode(JNIEnv \*env, jobject obj, jstring fileName);

JNIEXPORT jobjectArray JNICALL Java\_com\_dynamsoft\_zbar\_ZBarReader\_decode(JNIEnv \*env, jobject obj, jstring fileName)
	const char \*pszFileName = env->GetStringUTFChars(fileName, 0);

	//    under Windows it is necessary to initialize the ImageMagick
	//    library prior to using the Magick++ library

	// create a reader
	ImageScanner scanner;

	// configure the reader
	scanner.set\_config(ZBAR\_NONE, ZBAR\_CFG\_ENABLE, 1);

	// obtain image data
	Magick::Image magick(pszFileName);  // read an image file
	int width = magick.columns();   // extract dimensions
	int height = magick.rows();
	Magick::Blob blob;              // extract the raw data
	magick.write(&blob, "GRAY", 8);
	const void \*raw =;

	// wrap image data
	Image image(width, height, "Y800", raw, width \* height);

	// scan the image for barcodes
	int n = scanner.scan(image);

	// find java class
	jclass clsZBarResult = env->FindClass("com/dynamsoft/zbar/ZBarResult");
	// create java array
	int data\_length = 0;
	for (Image::SymbolIterator symbol = image.symbol\_begin();
		symbol != image.symbol\_end();
		++symbol) {

	jobjectArray clsZBarResultArray = env->NewObjectArray(data\_length, clsZBarResult, 0);
	int iIndex = 0;

	// extract results
	for (Image::SymbolIterator symbol = image.symbol\_begin();
		symbol != image.symbol\_end();
		++symbol) {
		// do something useful with results
		//cout << "ZBR Type: " << symbol->get\_type\_name()
		//	<< ", Value \\"" << symbol->get\_data() << '"' << endl;

		// save result to java array
		jmethodID init = env->GetMethodID(clsZBarResult, "<init>", "()V");
		jobject clsZBarResultObj = env->NewObject(clsZBarResult, init);
		jfieldID jType = env->GetFieldID(clsZBarResult, "mType", "Ljava/lang/String;");
		jfieldID jValue = env->GetFieldID(clsZBarResult, "mValue", "Ljava/lang/String;");
		env->SetObjectField(clsZBarResultObj, jType, env->NewStringUTF(symbol->get\_type\_name().c\_str()));
		env->SetObjectField(clsZBarResultObj, jValue, env->NewStringUTF(symbol->get\_data().c\_str()));
		env->SetObjectArrayElement(clsZBarResultArray, iIndex, clsZBarResultObj);

	// clean up
	image.set\_data(NULL, 0);

	// release string
	env->ReleaseStringUTFChars(fileName, pszFileName);

	return clsZBarResultArray;

I changed the main function to JNI method and packaged barcode results to Java objects. See the corresponding Java classes.

package com.dynamsoft.zbar;

import com.dynamsoft.utils.BaseReader;

public class ZBarReader extends BaseReader {

	static {

	public void testZBar(String fileName) {
		long start = System.nanoTime();
		ZBarReader reader =  new ZBarReader();
		ZBarResult\[\] results = (ZBarResult\[\])reader.decode(fileName);
		System.out.println(ZBAR + TIME\_COST
				+ ((System.nanoTime() - start) / 1000000) + MS);

		if (results != null && results.length > 0) {
			mCount += 1;
			for (ZBarResult result : results) {
				System.out.println(ZBAR + TYPE + result.mType + VALUE + result.mValue);

	public int getCount() {
		// TODO Auto-generated method stub
		return super.getCount();

	public native Object\[\] decode(String fileName);

package com.dynamsoft.zbar;

public class ZBarResult {
	public String mType;
	public String mValue;

Benchmark Test between ZXing and ZBar

Dynamsoft and ZXing provide some testing image sets, you can get them from and To make results convincing, I used the testing images provided by ZXing: Here is the report:


ZXI Time cost: 122ms

ZXI Type: QR\_CODE, value: MEBKM:URL:http\\://\_Page;;

ZBA Time cost: 33ms

ZBA Type: QR-Code, value: MEBKM:URL:http\\://\_Page;;

Search Blog Posts