Get Started With Barcode Scanning#

In this guide you will learn step by step how to add barcode capture to your application. Roughly, the steps are:

  • Create a new DataCaptureContext instance, initialized with your license key.
  • Create a BarcodeCaptureSettings and enable the barcode symbologies you want to read in your application.
  • Create a new barcode capture mode instance and initialize it with the settings created above.
  • Register a listener to receive scan events. Process the successful scans according to your applications needs, e.g. by looking up information in a database. After a successful scan, decide whether more codes will be scanned, or the scanning process should be stopped.
  • Obtain a camera instance and set it as the frame source on the data capture context.
  • Optionally, if displaying a preview, create a new DataCaptureView and add a BarcodeCaptureOverlay.

Prerequisites#

Before starting with this mode, make sure that you have a valid Scandit Data Capture SDK license key and that you added the necessary dependencies. If you have not done that yet, check this guide.

Create the Data Capture Context#

The first step to add barcode capture capabilities to your application is to create a new data capture context. The context expects a valid Scandit Data Capture SDK license key during construction.

Note

You can retrieve your Scandit Data Capture SDK license key, by signing in to your account at ssl.scandit.com.

DataCaptureContext dataCaptureContext = DataCaptureContext.forLicenseKey("-- ENTER YOUR SCANDIT LICENSE KEY HERE --");

Configure the Barcode Scanning Behavior#

Barcode scanning is orchestrated by the BarcodeCapture data capture mode. This class is the main entry point for scanning barcodes. It is configured through BarcodeCaptureSettings and allows to register one or more listeners that will get informed whenever new codes have been recognized.

For this tutorial, we will setup the barcode scanning for a small list of different barcode types, called symbologies. The list of symbologies to enable is highly application specific. We recommend that you only enable the list of symbologies your application requires.

BarcodeCaptureSettings settings = new BarcodeCaptureSettings();
settings.enableSymbology(Symbology.CODE128, true);
settings.enableSymbology(Symbology.CODE39, true);
settings.enableSymbology(Symbology.QR, true);
settings.enableSymbology(Symbology.EAN8, true);
settings.enableSymbology(Symbology.UPCE, true);
settings.enableSymbology(Symbology.EAN13_UPCA, true);

If you are not disabling barcode capture immediately after having scanned the first code, consider setting the BarcodeCaptureSettings.codeDuplicateFilter to around 500 or even -1 if you do not want codes to be scanned more than once.

Next, create a BarcodeCapture instance with the settings initialized in the previous step:

barcodeCapture = BarcodeCapture.forDataCaptureContext(dataCaptureContext, settings);

Register the Barcode Capture Listener#

To get informed whenever a new code has been recognized, add a BarcodeCaptureListener through BarcodeCapture.addListener() and implement the listener methods to suit your application’s needs.

First implement the BarcodeCaptureListener interface. For example:

@Override
public void onBarcodeScanned(@NonNull BarcodeCapture barcodeCapture,
        @NonNull BarcodeCaptureSession session, @NonNull FrameData frameData) {
    List<Barcode> recognizedBarcodes = session.getNewlyRecognizedBarcodes();
    // Do something with the barcodes.
}

Then add the listener:

barcodeCapture.addListener(this);

Use the Built-in Camera#

The data capture context supports using different frame sources to perform recognition on. Most applications will use the built-in camera of the device, e.g. the world-facing camera of a device. The remainder of this tutorial will assume that you use the built-in camera.

When using the built-in camera there are recommended settings for each capture mode with which the camera should be initialized. The following couple of lines show how to get the recommended settings and created the camera from it:

CameraSettings cameraSettings = BarcodeCapture.createRecommendedCameraSettings();

// Depending on the use case further camera settings adjustments can be made here.

Camera camera = Camera.getDefaultCamera();

if (camera != null) {
    camera.applySettings(cameraSettings);
}

Because the frame source is configurable, the data capture context must be told which frame source to use. This is done with a call to DataCaptureContext.setFrameSource():

dataCaptureContext.setFrameSource(camera);

The camera is off by default and must be turned on. This is done by calling FrameSource.switchToDesiredState() with a value of FrameSourceState.ON:

if (camera != null) {
  camera.switchToDesiredState(FrameSourceState.ON);
}

Use a View to Display the Scan Process#

When using the built-in camera as frame source, you will typically want to display the camera preview on the screen together with UI elements that guide the user through the capturing process. To do that, add a DataCaptureView to your view hierarchy:

DataCaptureView dataCaptureView = DataCaptureView.newInstance(this, dataCaptureContext);
setContentView(dataCaptureView);

To visualize the results of barcode scanning, the following overlay can be added:

BarcodeCaptureOverlay overlay = BarcodeCaptureOverlay.newInstance(barcodeCapture, dataCaptureView);

Disabling Barcode Capture#

To disable barcode capture, for instance as a consequence of a barcode being recognized, set BarcodeCapture.isEnabled to false. There will not be any new results until the capture mode is enabled again. Note that disabling the capture mode does not stop the camera, the camera continues to stream frames until it is turned off.