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
CameraXfor capturing and displaying the video streaming. - Receive the
ImageProxyfromImageAnalysis.Analyzerand transfer theImageProxytoiImageDataso that it can be recognized byDynamsoftBarcodeReader. - Add configurations to interface
ImageSource. Let the methodgetImagereturns theiImageDatayou generated fromImageProxy. The barcode reader can continuously obtain theiImageDatavia methodgetImage.
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() {
@Override
public void analyze(@NonNull ImageProxy imageProxy) {
try {
// insert your code here.
// after done, release the ImageProxy object
if (isShowingDialog) {
mImageData = null;
return;
}
byte[] data = new byte[imageProxy.getPlanes()[0].getBuffer().remaining()];
imageProxy.getPlanes()[0].getBuffer().get(data);
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 {
imageProxy.close();
}
}
};
}
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
ImageSourceorDynamsoftCameraEnhancer. - Set up the
DBRTextResultListenerfor receiving the barcode result from the callback. - Trigger the method
startScanningwhen 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) {
e.printStackTrace();
}
// Set the ImageSource so that DBR will read barcode from ImageSource.
mReader.setImageSource(new ImageSource() {
@Override
// 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() {
@Override
public void textResultCallback(int i, ImageData imageData, TextResult[] textResults) {
// Add code to execute when barcode results are received.
}
});
}
@Override
public void onResume() {
super.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.
mReader.startScanning();
}
@Override
public void onPause() {
super.onPause();
mReader.stopScanning();
}
}
If you still have questions about the usage of CameraX when implementing video barcode decoding, you can view the sample for more details.