Dev Center
Table of contents

DecodeWithCameraX Sample

DecodeWithCameraX is a sample that demonstrate how to decode barcodes from video streaming when you are using CameraX as the source of video streaming. When using CameraX for barcode decoding, the key points are:

  • Set up CameraX for capturing and displaying the video streaming.
  • Receive the ImageProxy from ImageAnalysis.Analyzer and transfer the ImageProxy to iImageData so that it can be recognized by DynamsoftBarcodeReader.
  • Add configurations to interface ImageSource. Let the method getImage returns the iImageData you generated from ImageProxy. The barcode reader can continuously obtain the iImageData via method getImage.

View the Sample(s)

Generate ImageData from ImageAnalysis

iImageData is the data type that can be recognized by DynamsoftBarcodeReader as an image source for barcode decoding. The following code snippet shows you how to transfer ImageProxy, which is produced by CameraX, to an iImageData.

Code Snippet

public class CameraFragment extends Fragment {
    private ImageData mImageData;
    private final ImageAnalysis.Analyzer mBarcodeAnalyzer = new ImageAnalysis.Analyzer() {
        public void analyze(@NonNull ImageProxy imageProxy) {
            try {
                // insert your code here.
                // after done, release the ImageProxy object
                if (isShowingDialog) {
                    mImageData = null;
                byte[] data = new byte[imageProxy.getPlanes()[0].getBuffer().remaining()];
                int nRowStride = imageProxy.getPlanes()[0].getRowStride();
                int nPixelStride = imageProxy.getPlanes()[0].getPixelStride();
                ImageData imageData = new ImageData();
                imageData.bytes = data;
                imageData.width = imageProxy.getWidth();
                imageData.height = imageProxy.getHeight();
                imageData.stride = nRowStride;
                imageData.format = EnumImagePixelFormat.IPF_NV21;
                imageData.orientation = imageProxy.getImageInfo().getRotationDegrees();
                mImageData = imageData;
            } finally {

Setup Image Source

There are three key points when decoding barcodes from the video streaming:

  • Set up the source of image. The source and be either ImageSource or DynamsoftCameraEnhancer.
  • Set up the DBRTextResultListener for receiving the barcode result from the callback.
  • Trigger the method startScanning when you want to start video streaming barcode decoding.

The following code snippet shows how to use ImageSource as the source of video barcode decoding.

public class CameraFragment extends Fragment {
    // The mImageData will be updated each time when you receive new image from ImageAnalysis
    private ImageData mImageData;

    private void initBarcodeReader() {
        try {
            // Create an instance of Dynamsoft Barcode Reader.
            mReader = new BarcodeReader();
        } catch (BarcodeReaderException e) {
        // Set the ImageSource so that DBR will read barcode from ImageSource.
        mReader.setImageSource(new ImageSource() {
            // Configure the method getImage. The method will be triggered each time when the library finished processing the previous image.
            public ImageData getImage() {
                return mImageData;
        // Set text result listener so that you can receive barcode result from textResultCallback.
        mReader.setTextResultListener(new TextResultListener() {
            public void textResultCallback(int i, ImageData imageData, TextResult[] textResults) {
                // Add code to execute when barcode results are received.

    public void onResume() {
        // Methods startScanning and stopScanning are the switch of barcode decoding thread.
        // Once you have configured the source of image and trigger startScanning, you will be able to receive barcode result from textResultCallback.

    public void onPause() {

If you still have questions about the usage of CameraX when implementing video barcode decoding, you can view the sample for more details.

This page is compatible for:

Version 7.5.0

Is this page helpful?

YesYes NoNo

In this article:

version 9.6.40

  • Latest version
  • Version 10.x
    • Version 10.2.10
    • Version 10.0.21
    • Version 10.0.20
  • Version 9.x
    • Version 9.6.40
    • Version 9.6.20
    • Version 9.6.11
    • Version 9.6.10
    • Version 9.6.0
    • Version 9.4.0
    • Version 9.2.13
    • Version 9.2.11
    • Version 9.2.10
    • Version 9.0.2
    • Version 9.0.1
    • Version 9.0.0
  • Version 8.x
    • Version 8.9.3
    • Version 8.9.1
    • Version 8.9.0
    • Version 8.8.0
    • Version 8.6.0
    • Version 8.4.0
    • Version 8.2.1
    • Version 8.2.0
    • Version 8.1.2
    • Version 8.1.0
    • Version 8.0.0
  • Version 7.x
    • Version 7.6.0
    • Version 7.5.0
Change +