Using Android NDK to Optimize Barcode Reading Performance

Prerequisites

Android NDK and CMake

Open Android Studio and select Tools > SDK Manager > SDK Tools. Check NDK and CMake to install:

Dynamsoft Barcode Reader for Android

Download Dynamsoft Barcode Reader for Android.

The Simple Barcode Reader Demo

Get the source code from GitHub:

git clone https://github.com/yushulx/android-camera2-barcode

Android Barcode Decoding Using JNI

Import the project into Android Studio. It is time to do the optimization.

private native ArrayList<SimpleResult> readBarcode(long hBarcode, ByteBuffer byteBuffer, int width, int height, int stride);private native long createBarcodeReader(String license);private native void destroyBarcodeReader(long ndkBarcodeReader);
byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes);
unsigned char * buffer = (unsigned char*)env->GetDirectBufferAddress(byteBuffer);
ByteBuffer buffer = image.getPlanes()[0].getBuffer();int nRowStride = image.getPlanes()[0].getRowStride();int nPixelStride = image.getPlanes()[0].getPixelStride();byte[] bytes = new byte[buffer.remaining()];buffer.get(bytes);TextResult[] results = mBarcodeReader.decodeBuffer(bytes, mImageReader.getWidth(), mImageReader.getHeight(), nRowStride * nPixelStride, EnumImagePixelFormat.IPF_NV21, "");
ByteBuffer buffer = image.getPlanes()[0].getBuffer();int nRowStride = image.getPlanes()[0].getRowStride();int nPixelStride = image.getPlanes()[0].getPixelStride();ArrayList<SimpleResult> results = readBarcode(hBarcode, buffer, mImageReader.getWidth(), mImageReader.getHeight(), nRowStride * nPixelStride);

Building C++ Code with Android NDK and CMake

Create a CMakeLists.txt file under src/main/cpp:

Android NDK CMake Error:error: cannot find -lDynamsoftBarcodeReaderAndroid

Source Code

https://github.com/yushulx/android-camera2-barcode-ndk

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store