1

我正在使用 camerax 库 1.0.0-alpha006 这是我的 Logcat

    java.lang.IllegalStateException: Detaching occurs more times than attaching
    at androidx.camera.core.DeferrableSurface.notifySurfaceDetached(DeferrableSurface.java:160)
    at androidx.camera.camera2.impl.Camera.notifyDetachFromUseCaseSurfaces(Camera.java:667)
    at androidx.camera.camera2.impl.Camera.removeOnlineUseCase(Camera.java:789)
    at androidx.camera.camera2.impl.Camera$13.run(Camera.java:772)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:214)
    at android.os.HandlerThread.run(HandlerThread.java:65)

这是我的相机预览和图像捕捉代码

    if (mPreview != null) {
        CameraX.unbindAll();
    }
    DisplayMetrics metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    Rational screenAspectRatio = new Rational(metrics.widthPixels, metrics.heightPixels);
    PreviewConfig config =
            new PreviewConfig.Builder()
                    .setLensFacing(CameraX.LensFacing.BACK)
                    .setTargetResolution(new Size(metrics.widthPixels, metrics.heightPixels))
                    .setTargetRotation(getDisplaySurfaceRotation())
                    .build();
    mPreview = AutoFitPreviewBuilder.Companion.build(config, textureView);
    ImageCaptureConfig imageCaptureConfig = new ImageCaptureConfig.Builder()
            .setLensFacing(CameraX.LensFacing.BACK)
            .setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
            .setTargetResolution(new Size(metrics.widthPixels, metrics.heightPixels))
            .setTargetRotation(getDisplaySurfaceRotation())
            .build();

    imageCapture = new ImageCapture(imageCaptureConfig);
    CameraX.bindToLifecycle((LifecycleOwner) getActivity(), mPreview, imageCapture);

这是我的图像捕获代码

    public File getFile(ContentResolver contentResolver) {

    File sdcard = Environment.getExternalStorageDirectory();
    if (sdcard != null) {
        File mediaDir = new File(sdcard, "LibPoly/Images");
        if (!mediaDir.exists()) {
            mediaDir.mkdirs();
        }
        final String filePath = System.currentTimeMillis() + ".jpg";
        cameraFragment.imageCapture.takePicture(new File(mediaDir, filePath), new Executor() {
            @Override
            public void execute(Runnable runnable) {
                runnable.run();
            }
        }, new ImageCapture.OnImageSavedListener() {
            @Override
            public void onImageSaved(@NonNull File file) {
                Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                ExifInterface ei = null;
                try {

                    ei = new ExifInterface(file.getAbsolutePath());
                    if (!"LANDSCAPE".equalsIgnoreCase(orientation)) {
                        int eiAttributeInt = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                                ExifInterface.ORIENTATION_UNDEFINED);
                        switch (eiAttributeInt) {
                            case ExifInterface.ORIENTATION_ROTATE_90:
                                bitmap = rotateImage(bitmap, 90);
                                break;

                            case ExifInterface.ORIENTATION_ROTATE_180:
                                bitmap = rotateImage(bitmap, 180);
                                break;

                            case ExifInterface.ORIENTATION_ROTATE_270:
                                bitmap = rotateImage(bitmap, 270);
                                break;

                            case ExifInterface.ORIENTATION_NORMAL:
                            default:
                                bitmap = bitmap;

                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("ERROR", e.getMessage() + "");
                }

                OutputStream os = null;
                try {
                    os = new BufferedOutputStream(new FileOutputStream(file));
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
                    os.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }


                final MediaInfo image = cameraFragment
                        .getMediaFromContentUri(Utils.getImageContentUri(getContentResolver(), file), MediaInfo.SOURCE_NAME_CAMERA, MediaInfo.MediaType.IMAGE, null, context);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        addImage(image);
                    }
                });
            }

            @Override
            public void onError(@NonNull ImageCapture.ImageCaptureError
                                        imageCaptureError, @NonNull String message, @Nullable Throwable cause) {

                Log.e("ImageCaptureError", message);
            }
        });
    }
    return null;
}

我使用的是 camerax 版本 1.0.0-alpha005,它工作正常(除了某些设备,问题出在预览版上),现在我正面临这个崩溃。

4

0 回答 0