public class MyCameraActivity
extends AppCompatActivity
implements CameraErrorListener {
// Most cardscan models require a minimum resolution of 1280x720
private static final Size MINIMUM_RESOLUTION = new Size(1280, 720);
private CameraAdapter<Bitmap> cameraAdapterInstance = null;
private CameraAdapter<Bitmap> getCameraAdapter() {
if (cameraAdapterInstance == null) {
cameraAdapterInstance = Camera2Adapter(
// A TextureView where the preview will show. If null, no
// preview will be shown.
/* previewView */ (TextureView) findViewById(R.id.textureView),
// the minimum image resolution that should be streamed.
/* minimumResolution */ MINIMUM_RESOLUTION,
/* cameraErrorListener */ this
return cameraAdapterInstance;
* Call this method to start streaming images from the camera.
public void startProcessingCameraImages() {
final CameraAdapter<Bitmap> cameraAdapter = getCameraAdapter();
cameraAdapter.bindToLifecycle(this);
cameraAdapter.getImageStream().collect(
(bitmap, continuation) -> {
processCameraImage(bitmap);
Coroutine.resumeJava(continuation, Unit.INSTANCE);
new EmptyJavaContinuation<>()
private void processCameraImage(@NotNull Bitmap previewFrame) {
// Do something with the preview frame
public void onCameraOpenError(@Nullable Throwable cause) {
// The camera could not be opened
public void onCameraAccessError(@Nullable Throwable cause) {
// The camera could not be accessed
public void onCameraUnsupportedError(@Nullable Throwable cause) {
// the camera is not supported on this device.