Skip to content

Barcode scanning

Barcode scanning capabilities depend on the Iristick smart glasses model. The following table summarizes the differences:

Feature G1 / G2 G1 PRO / G2 PRO / H1
Scan with center camera
Scan with zoom camera
Select barcode to scan with laser
Scan multiple barcodes
Handle rotated barcodes
Advanced barcode formats
Enhanced performance

There are two ways to scan barcodes using the Iristick SDK. If you occasionally require your user to scan a single barcode, you can use the barcode scanning intent. If you have heavier barcode scanning flow, you can use the barcode scanning API.

Example code

The Barcode scanner example provided in the SDK package show how to use both the barcode scanning intent to scan a single barcode and the barcode scanning API to continuously scan for barcodes overlaying the results on top of the camera preview.

Using the barcode scanning intent

The easiest way to scan a barcode is to launch an intent with action ACTION_SCAN_BARCODE. The Iristick Services will then show its barcode scanning UI, asking the user to point the laser to the barcode to scan. Once the user's head is kept steady, the zoom camera will be used, if available, at different zoom levels to scan for the barcode. Once found, control is returned to your application.

During the process, the user can cancel the operation by swiping down on the touchpad, saying go backwards, or tapping on the Android back key.

The list of barcode formats that have to be recognized can optionally be set through the EXTRA_BARCODE_SCAN_FORMATS intent extra. If not provided, a subset of most common barcode formats is chosen. For better performance, try to enable as few formats as needed.

public class MyActivity extends Activity {

    private static final int REQUEST_CODE = 0;

    // ...

    public void scanBarcode() {
        Intent intent = new Intent(Intents.ACTION_SCAN_BARCODE);
        intent.putExtra(Intents.EXTRA_BARCODE_SCAN_FORMATS, "QR_CODE"); // optional
        startActivityForResult(intent, REQUEST_CODE);
    }

    protected void onActivityResult(int requestCode, int resultCode,
                                    @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            switch (resultCode) {
            case Intents.RESULT_OK:
                String text = data.getStringExtra(Intents.EXTRA_BARCODE_RESULT);
                String format = data.getStringExtra(Intents.EXTRA_BARCODE_FORMAT);
                /* The barcode was successfully scanned:
                 * do something with the result. */
                break;
            case Intents.RESULT_CANCELLED:
                /* The user cancelled the operation. */
                break;
            default:
                /* An error occured. */
            }
        }
    }

}

Using the barcode scanning API

For more advanced barcode scanning workflows the barcode scanning extensions to the camera API can be used. Please read the camera API guide first for an introduction to the camera API.

As with all camera capabilities, barcode scanning capabilities can be queried through the CameraCharacteristics object describing a camera. The following keys concern barcode scanning:

To enable barcode scanning, build capture requests with the TEMPLATE_BARCODE template. You can optionally set some CaptureRequest keys to configure the scanning process.

CaptureRequest createCaptureRequest(CameraDevice camera) {
    /* Create the builder with the BARCODE template. */
    CaptureRequest.Builder builder =
        camera.createCaptureRequest(CameraDevice.TEMPLATE_BARCODE);

    /* Add the preview surface(s) */
    builder.addTarget(mPreviewSurface);

    /* Optional: set number of barcodes to scan
     * (defaults to max). */
    builder.set(CaptureRequest.POSTPROCESS_BARCODE_COUNT, 1);

    /* Optional: set barcode formats to recognize
     * (defaults to a common subset).*/
    builder.set(CaptureRequest.POSTPROCESS_BARCODE_FORMATS,
                new String[]{CaptureRequest.QR_CODE});

    /* Build the capture request. */
    return builder.build();
}

The capture requests can then be submitted, preferably as repeating requests. Be sure to provide a capture listener to be notified of recognized barcodes. The capture listener must extend the CaptureListener2 abstract class. Barcode scanning results are only available in the onPostProcessCompleted callback.

class MyCaptureListener extends CaptureListener2 {
    @Override
    public void onPostProcessCompleted(@NonNull CaptureSession session,
                                       @NonNull CaptureRequest request,
                                       @NonNull CaptureResult result) {
        Barcode[] barcodes = result.get(CaptureResult.POSTPROCESS_BARCODES);
        if (barcodes != null && barcodes.length > 0) {
            /* Do something with the recognized barcodes. */
        }
    }
}