BarcodeScanner Mlkit example - How to get the codebar image as a Bitmap

70 views Asked by At

I am using the barcode detection example from the following repository: MLKit example

In the CameraXLivePreviewActivity activity, the BarcodeScanner class is used to detect the barcode. How can I get the image where the barcode was detected as a Bitmap and save it on the device.

I have tried to save the image in a global variable inputImage in the method

@Override
protected Task<List<Barcode>> detectInImage(InputImage image) {
    inputImage = image;
    return barcodeScanner.process(image);
}

of the BarcodeScannerProcessor but when I use that variable in the onSuccess method of the BarcodeScannerProcessor, when I call the following method inputImage.getByteBuffer() on convertImage method to convert to Bitmap, the buffer returned is null

@Override   protected void onSuccess(
  @NonNull List<Barcode> barcodes, @NonNull GraphicOverlay graphicOverlay) {
    Bitmap originalImage = convertImage(inputImage);
}

And convertImage method:

private Bitmap convertImage(InputImage originalImage){

  if (originalImage != null) {
    int width = originalImage.getWidth();
    int height = originalImage.getHeight();

    ByteBuffer buffer = originalImage.getByteBuffer();

    FrameMetadata.Builder builder = new FrameMetadata.Builder();
    builder.setHeight(height);
    builder.setWidth(width);
    builder.setRotation(90);
    FrameMetadata metadata = builder.build();

    return BitmapUtils.getBitmap(buffer, metadata);
  }
  return null;

}

0

There are 0 answers