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

https://github.com/zxing/zxing

ZBar Source Code

https://github.com/ZBar/ZBar

ZBar Windows Installer

http://sourceforge.net/projects/zbar/files/zbar/

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: http://www.java2s.com/Code/Jar/j/Downloadjaicore113jar.htm. 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<>();
		formats.add(BarcodeFormat.QR\_CODE);
		formats.add(BarcodeFormat.CODABAR);
		formats.add(BarcodeFormat.CODE\_39);
		formats.add(BarcodeFormat.CODE\_93);
		formats.add(BarcodeFormat.CODE\_128);
		formats.add(BarcodeFormat.EAN\_8);
		formats.add(BarcodeFormat.EAN\_13);
		formats.add(BarcodeFormat.ITF);
		formats.add(BarcodeFormat.UPC\_A);
		formats.add(BarcodeFormat.UPC\_E);
		formats.add(BarcodeFormat.UPC\_EAN\_EXTENSION);

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

		// read multi barcodes
		GenericMultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(
				reader);
		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) {
			e.printStackTrace();
			return;
		}

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\_\_)
#endif

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);

#ifdef MAGICK\_HOME
	// http://www.imagemagick.org/Magick++/
	//    under Windows it is necessary to initialize the ImageMagick
	//    library prior to using the Magick++ library
	Magick::InitializeMagick(MAGICK\_HOME);
#endif

	// 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.modifyImage();
	magick.write(&blob, "GRAY", 8);
	const void \*raw = blob.data();

	// 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) {
		++data\_length;
	}

	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);
		++iIndex;		
	}

	// 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.

ZBarReader.java:

package com.dynamsoft.zbar;

import com.dynamsoft.utils.BaseReader;

public class ZBarReader extends BaseReader {

	static {
		System.loadLibrary("zbarjni");
	}

	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);
			}
		}
	}

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

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

ZBarResult.java:

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 https://github.com/Dynamsoft/Dynamsoft-Barcode-Reader/tree/master/Images and https://github.com/zxing/zxing/tree/master/core/src/test/resources. To make results convincing, I used the testing images provided by ZXing: https://github.com/zxing/zxing/tree/master/core/src/test/resources/blackbox/qrcode-1 Here is the report:

F:\\resources\\blackbox\\qrcode-1\\1.png

ZXI Time cost: 122ms

ZXI Type: QR\_CODE, value: MEBKM:URL:http\\://en.wikipedia.org/wiki/Main\_Page;;

ZBA Time cost: 33ms

ZBA Type: QR-Code, value: MEBKM:URL:http\\://en.wikipedia.org/wiki/Main\_Page;;

Search Blog Posts