4

ImageReader を使用してプレビューされていないカメラを実装しようとして camera2 のドキュメントに従ってきましたが、イベント リスナーで割り当てられている必要があると確信しているにもかかわらずcameraDevice.createCaptureSession(outputs, mccsStateCallback, cameraHandler);、オブジェクトが null になっています。 CameraCaptureSession 自体が null であるため、トリガーされません)。私はこれをすべて完全に間違っているか、何か大きなものを見逃しています。これが私のコードです:cameraDeviceCameraCaptureSession.StateCallback

private CameraDevice cameraDevice;
private String cameraId;
private Handler cameraHandler = new Handler();
private CameraCharacteristics cameraCharacteristics;
private ImageReader jpgReader;
Bitmap bitmap;
private Handler imgHandler = new Handler();
private CameraCaptureSession mSession;
private CameraManager cameraManager;

private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
    @Override
    public void onOpened(@NonNull CameraDevice camera) {
        cameraDevice = camera;
    }

    @Override
    public void onDisconnected(@NonNull CameraDevice camera) {
        cameraDevice.close();
    }

    @Override
    public void onError(@NonNull CameraDevice camera, int error) {
        cameraDevice.close();
        cameraDevice = null;
    }
};

OnImageAvailableListener imageAvailableListener = new OnImageAvailableListener() {
    @Override
    public void onImageAvailable(ImageReader reader) {
        Image image = reader.acquireLatestImage();
        Image.Plane[] planes = image.getPlanes();
        Buffer buffer = planes[0].getBuffer().rewind();
        bitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.ARGB_8888);
        bitmap.copyPixelsFromBuffer(buffer);
        mFaceOverlayView.setBitmap(bitmap);

    }
};

private CameraCaptureSession.StateCallback mccsStateCallback = new CameraCaptureSession.StateCallback() {
    @Override
    public void onConfigured(@NonNull CameraCaptureSession session) {
        try {
            if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(getBaseContext(), android.Manifest.permission.CAMERA)) {
                mSession = session;
                CaptureRequest.Builder request = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
                request.addTarget(jpgReader.getSurface());
                mSession.setRepeatingRequest(request.build(), new CameraCaptureSession.CaptureCallback() {
                    @Override
                    public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
                        super.onCaptureCompleted(session, request, result);
                    }
                }, null);
                cameraManager.openCamera(cameraId, mStateCallback, cameraHandler);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void onConfigureFailed(CameraCaptureSession session) {

    }
};

private void initialiseCamera() {
    cameraManager = (CameraManager) FilesPlayer.this.getSystemService(Context.CAMERA_SERVICE);
    try {
        cameraId = getFrontFacingCameraId(cameraManager);
        if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)) {

            cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);

            StreamConfigurationMap streamConfigurationMap = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            android.util.Size[] jpegSizes = streamConfigurationMap.getOutputSizes(ImageFormat.JPEG);

            Size biggestSize = new Size(0, 0);
            for (Size size : jpegSizes) {
                if (size.getHeight() >= biggestSize.getHeight() && size.getWidth() >= biggestSize.getWidth()) {
                    biggestSize = size;
                }
            }
            jpgReader = ImageReader.newInstance(biggestSize.getWidth(), biggestSize.getHeight(), ImageFormat.JPEG, 1);
            jpgReader.setOnImageAvailableListener(imageAvailableListener, imgHandler);
            List<Surface> outputs = Arrays.asList(jpgReader.getSurface());
            cameraDevice.createCaptureSession(outputs, mccsStateCallback, cameraHandler);

        }
        else{
            Log.d("NOOO","NOOOO");
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

private String getFrontFacingCameraId(CameraManager cameraManager) {
    try {
        for (String id : cameraManager.getCameraIdList()) {
            CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(id);
            Integer cameraOrientation = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING);
            if (cameraOrientation != null && cameraOrientation == CameraMetadata.LENS_FACING_FRONT) {
                return id;
            }
        }

    } catch (CameraAccessException ex) {
        ex.printStackTrace();
    }
    return null;
}

編集:私は CameraDevice を自分自身に割り当てることができましcameraManager.openCamera(cameraId,mStateCallback,cameraHandler);た。現在、カメラ出力が ImageReader のサーフェスに向けられていないという問題が発生しており、コンソールに.cameraDevice.createCaptureSession(outputs, mccsStateCallback, cameraHandler);initialiseCamera()E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa1ae7000

4

1 に答える 1