How to implement a simple barcode scan application on Android

Image capture and processing is now used on smartphones widely. 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 of how to implement a simple barcode scan application on the Android platform. 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="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
2. 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) {
                backed = i
            }
Camera mCamera = Camera.open(backId);
        }
</div>
</div>
3. 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.
<div>
<div id="highlighter_873527">
Log.i("parameters", mCamera.getParameters().flatten());
</div>
</div>
<b>Note:</b> 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.
<div>
<div id="highlighter_144772">
private Camera.PreviewCallback mPreviewCallback = new Camera.PreviewCallback() {
        @Override
        public void onPreviewFrame(byte[] data, Camera camera) {       
            // TODO Auto-generated method stub
        }
    };

Read More >

Subscribe Newsletter

Subscribe to our mailing list to get the monthly update.

Subscribename@email.com