0

I have a camera preview in TextureView. How to send camera frames to my barcode detector?

barcodeDetector = new BarcodeDetector.Builder(this)
    .setBarcodeFormats(Barcode.ALL_FORMATS)
    .build();

barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {

    @Override
    public void release() {
    }

    @Override
    public void receiveDetections(Detector.Detections<Barcode> detections) {
        final SparseArray<Barcode> barcodes = detections.getDetectedItems();
        if (barcodes.size() != 0) {
           // do the operation
        }  
    }
}
Mike
  • 4,550
  • 4
  • 33
  • 47
Andrey Rankov
  • 1,964
  • 2
  • 17
  • 33

1 Answers1

2

You need to create a camera instance and link it to your detector (which already is linked to your processor)

mCameraSource = new CameraSource.Builder(context, barcodeDetector)
                   .setFacing(CameraSource.CAMERA_FACING_BACK)
                   .setRequestedFps(15.0f)
                   .build();

To link the camera to your SurfaceView and start it use code like this (when the SurfaceView is availble)

mCameraSource.start(mSurfaceView.getHolder());

You have a working example provided here by Google.

For TextureView use manual detection

public void onSurfaceTextureUpdated(SurfaceTexture surface) {
          // Invoked every time there's a new Camera preview frame
           mTextureView.getBitmap(bitmap);
           Frame frame = new Frame.Builder().setBitmap(bitmap).build();
           SparseArray<Barcode> barcodes = barcodeDetector.detect(frame);
      }
Radu Ionescu
  • 3,462
  • 5
  • 24
  • 43
  • There is SurfaceView in this example. I need to use TextureView instead. – Andrey Rankov Mar 03 '16 at 09:58
  • It is easy to replace a `SurfaceView` with a `TextureView` (you can simply follow the example from the [JavaDoc](http://developer.android.com/reference/android/view/TextureView.html) ) – Radu Ionescu Mar 03 '16 at 10:00
  • Here is the recommendation from Google. How to implement it? https://github.com/googlesamples/android-vision/issues/15#issuecomment-135068919 – Andrey Rankov Mar 03 '16 at 10:01
  • Here is the example of camera usage in TextureView http://www.tutorialspoint.com/android/android_textureview.htm Can't understand, how to get frames from it and send them to Detector. – Andrey Rankov Mar 03 '16 at 10:07
  • Great, it worked this way! `Frame frame = new Frame.Builder().setBitmap(mTextureView.getBitmap()).build(); barcodeDetector.receiveFrame(frame);` – Andrey Rankov Mar 03 '16 at 11:21