Android CameraX - Get camera info (view angle, preview image size)

Viewed 2803

I'm trying to switch from old Android camera API to new CameraX API. I'm using preview mode for an Augmented Reality App and I need to get some info like angle of view and size on my camera used by the Preview.

This is my code so far:

PreviewConfig config = new PreviewConfig.Builder()
                .setLensFacing(CameraX.LensFacing.BACK)
                .setTargetResolution(new Size(dsiWidth, dsiHeight))
                .build();
Preview preview = new Preview(config);
preview.setOnPreviewOutputUpdateListener(new Preview.OnPreviewOutputUpdateListener() {
            @Override
            public void onUpdated(Preview.PreviewOutput output) {
                tvCameraView.setSurfaceTexture(output.getSurfaceTexture());
            }
        });
CameraX.bindToLifecycle(this, preview);

This works so far. But how do I get information on the camera used by the Preview? Thanks a lot in advance!

1 Answers

When you use the "androidx.camera:camera-camera2:1.0.0-alpha02" dependency you can have a look at the class Camera2CameraFactory. There you can see how the front and back facing camera is determined.

    @Override
    public Set<String> getAvailableCameraIds() throws CameraInfoUnavailableException {
        List<String> camerasList = null;
        try {
            camerasList = Arrays.asList(mCameraManager.getCameraIdList());
        } catch (CameraAccessException e) {
            throw new CameraInfoUnavailableException(
                    "Unable to retrieve list of cameras on device.", e);
        }
        // Use a LinkedHashSet to preserve order
        return new LinkedHashSet<>(camerasList);
    }

    @Nullable
    @Override
    public String cameraIdForLensFacing(LensFacing lensFacing)
            throws CameraInfoUnavailableException {
        Set<String> cameraIds = getAvailableCameraIds();

        // Convert to from CameraX enum to Camera2 CameraMetadata
        Integer lensFacingInteger = -1;
        switch (lensFacing) {
            case BACK:
                lensFacingInteger = CameraMetadata.LENS_FACING_BACK;
                break;
            case FRONT:
                lensFacingInteger = CameraMetadata.LENS_FACING_FRONT;
                break;
        }

        for (String cameraId : cameraIds) {
            CameraCharacteristics characteristics = null;
            try {
                characteristics = mCameraManager.getCameraCharacteristics(cameraId);
            } catch (CameraAccessException e) {
                throw new CameraInfoUnavailableException(
                        "Unable to retrieve info for camera with id " + cameraId + ".", e);
            }
            Integer cameraLensFacing = characteristics.get(CameraCharacteristics.LENS_FACING);
            if (cameraLensFacing == null) {
                continue;
            }
            if (cameraLensFacing.equals(lensFacingInteger)) {
                return cameraId;
            }
        }

        return null;
    }

It boils down to picking the first camera that matches the orientation from the camera service. I would assume that they will expand those apis in the future camerax release.

Related