How to implement a simple barcode scan application on Android

Barcode scanning, especially QR code scans, are now widely used in various applications. This includes advertisements, newspapers, social networks, signage, and so on. The growth of QR codes is in large part due to the smartphone market’s booming growth. As a result, scanning technology tends to be more and more useful and important to businesses and consumers. In this article, we will take a glimpse on how to implement a simple barcode scan application on the Android platform.

]Dynamsoft Barcode Reader SDK Ads Powered by Dynamsoft

Basic Steps to Implement an Android Barcode Application

The implementation of a barcode scan application mainly consists of two parts: invoking an Android camera and leveraging the ZXing barcode library, respectively.

Step 1: Get the image data of a barcode from an Android camera.

Step 2: Transfer the barcode image data into ZXing for barcode recognition.

Both Android and ZXing provide detailed APIs and sample code.

Avoiding common mistakes in using the Android camera

  1. It’s important to make sure you enable proper permissions for the camera, to avoid user problems. To do this, set camera access permission in AndroidManifest.xml. Otherwise, users will get a permission error when they launch your application. Here’s the code to do it:
      <uses-permission android:name="android.permission.CAMERA" />
      <uses-feature android:name="" />
      <uses-feature android:name="" />
  1. Do not 100% trust your eyes to instantiate a Camera Object directly. The following code may help you distinguish which camera you want to use.
int frontId = 0, backId = 0;
CameraInfo cameraInfo = new CameraInfo();
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
            	frontId = i;
            else if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
                backId = i 
Camera mCamera =;
  1. To set camera parameters, it’s recommended you log out and check all your camera parameters in advance. This is a handy way to know your hardware.
Log.i("parameters", mCamera.getParameters().flatten());

Note: some of the camera parameters may not work on different devices, especially for some third-party API providers, such as Samsung. If you set some parameters that work for Google’s device, there is no guarantee that they definitely work for Samsung’s.

4. Now, implement the relevant callback function to obtain the image data.

private Camera.PreviewCallback mPreviewCallback = new Camera.PreviewCallback() {
		public void onPreviewFrame(byte[] data, Camera camera) {		
			// TODO Auto-generated method stub

How to use ZXing

  1. Install SVN software on your Windows/Mac. For windows users, TortoiseSVN is a good choice. It is free and comes with a GUI. Versions is an option for Mac users. It has a beautiful GUI, but it costs $59.

  2. Create a directory named “ZXing”, and type in “svn checkout” to download ZXing source code.

D:\\zxing>svn checkout
  1. Import ZXing to your camera project, and transfer the data to ZXing for barcode recognition.


private Camera.PreviewCallback mPreviewCallback = new PreviewCallback() {

        public void onPreviewFrame(byte[] data, Camera camera) {
            // TODO Auto-generated method stub

        	if (mDialog.isShowing())

        	LuminanceSource source = new PlanarYUVLuminanceSource(data, mWidth, mHeight, mLeft, mTop, mAreaWidth, mAreaHeight, false);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(
            Result result;

            try {
				result = mMultiFormatReader.decode(bitmap, null);
				if (result != null) {
			} catch (NotFoundException e) {
				// TODO Auto-generated catch block

Source Code

Search Blog Posts