0

so am trying to create a barcode scanner with google ML Kit that scans qr codes using android built in camera but the FirebasevisionImage class takes a media.image and i dont know how to create it . Here is my code.

public class QrActivity extends AppCompatActivity implements SurfaceHolder.Callback{

private static final String TAG = "QrActivity";

SurfaceView surfaceView = findViewById(R.id.cameraView);
SurfaceHolder holder = surfaceView.getHolder();
CameraManager cameraManager;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_qr);
}

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    surfaceHolder = this.holder ;

    try {
        Image myimage = cameraManager.openCamera(1,);
        getRotationCompensation(getCameraId(),QrActivity.this,getApplicationContext());
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent,1);
        FirebaseVisionImage image = FirebaseVisionImage.fromMediaImage(reader,  getRotationCompensation(getCameraId(),QrActivity.this,getApplicationContext()));

    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

also have a method to get and return camera id and created a sparseInt Array to get camera rotations.

private String getCameraId(){
        String mCameraId = null;
        CameraManager cameraManager =(CameraManager) getSystemService(Context.CAMERA_SERVICE);
        try {
            for(String cameraId:cameraManager.getCameraIdList()){
                CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);
                if(cameraCharacteristics.get(CameraCharacteristics.LENS_FACING)==1){
                    mCameraId = cameraId;
                    return mCameraId;
                }

            }
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
        return mCameraId;
    }

private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
    static {
        ORIENTATIONS.append(Surface.ROTATION_0, 90);
        ORIENTATIONS.append(Surface.ROTATION_90, 0);
        ORIENTATIONS.append(Surface.ROTATION_180, 270);
        ORIENTATIONS.append(Surface.ROTATION_270, 180);
    }

But what i want to know is how i can make the barcode scanner to work with the device camera.

Reference https://firebase.google.com/docs/ml-kit/android/read-barcodes

Blaqfella
  • 3
  • 5

1 Answers1

0

Seems like you are using android.hardware.camera2 API, which is indeed a little difficult to set up. Please refer to camera2 documentation, but in short, you could use android.media.ImageReader and asks the camera2 API to render the image to its "Surface" and gets the media.Image from ImageReader.OnImageAvailableListener.

ML Kit is working on improving the Quick Start app to include camera2 sample code. Stay tuned. Thanks.

Isabella Chen
  • 2,421
  • 13
  • 25
  • Thank you @Isabella. I actually managed to get it working using the google vision api which is soon to be deprecated but it gets the job done for now. – Blaqfella Aug 26 '18 at 21:14