将捕获的图像转换为位图

问题描述 投票:1回答:1

我正在使用Java探索android的CameraX API,并尝试构建捕获图像用例。这是

的代码
private void takePicture() {
        imageCapture.takePicture(new ImageCapture.OnImageCapturedListener() {
            @Override
            public void onCaptureSuccess(ImageProxy image, int rotationDegrees) {
                Bitmap bitmap = rotateImage(toBitmap(image.getImage()), (float) rotationDegrees);
                ImageView takenImage.setImageBitmap(bitmap);
                super.onCaptureSuccess(image, rotationDegrees);
            }
        });
    }

并将捕获的图像转换为位图。我正在尝试在以下用Java编写的问题中实现@Ahwar的答案:Converting ImageProxy to Bitmap这是此处给出的代码答案:

private Bitmap toBitmap(Image image) {
        Image.Plane[] planes = image.getPlanes();
        ByteBuffer yBuffer = planes[0].getBuffer();
        ByteBuffer uBuffer = planes[1].getBuffer();
        ByteBuffer vBuffer = planes[2].getBuffer();

        int ySize = yBuffer.remaining();
        int uSize = uBuffer.remaining();
        int vSize = vBuffer.remaining();

        byte[] nv21 = new byte[ySize + uSize + vSize];
        //U and V are swapped
        yBuffer.get(nv21, 0, ySize);
        vBuffer.get(nv21, ySize, vSize);
        uBuffer.get(nv21, ySize + vSize, uSize);

        YuvImage yuvImage = new YuvImage(nv21, ImageFormat.NV21, image.getWidth(), image.getHeight(), null);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        yuvImage.compressToJpeg(new Rect(0, 0, yuvImage.getWidth(), yuvImage.getHeight()), 75, out);

        byte[] imageBytes = out.toByteArray();
        return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
    }

我的问题:当我调用toBitmap(image)函数时,我得到ArrayIndexOutOfBoundsException,并且在调试之后,我了解到图像平面的大小为仅1而不是3。我已在此处附加了收到的异常,

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.camerax3, PID: 17342
    java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
        at com.example.camerax3.activity.MainActivity.toBitmap(MainActivity.java:210)
        at com.example.camerax3.activity.MainActivity.access$100(MainActivity.java:45)
        at com.example.camerax3.activity.MainActivity$2.onCaptureSuccess(MainActivity.java:239)
        at androidx.camera.core.ImageCapture$ImageCaptureRequest.dispatchImage(ImageCapture.java:1341)
        at androidx.camera.core.ImageCapture$10.onImageAvailable(ImageCapture.java:657)
        at androidx.camera.core.MetadataImageReader$3.run(MetadataImageReader.java:279)
        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.app.ActivityThread.main(ActivityThread.java:7099)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

任何人都可以提出解决此问题的方法吗?我已经在Kotlin中阅读了许多答案,但是我正在尝试在Java中实现相同的答案。在Java中将图像转换为位图的任何其他方法也将受到赞赏。

java android indexoutofboundsexception android-bitmap android-camerax
1个回答
0
投票

此代码对我有用,请尝试。

  private Bitmap getBitmap(ImageProxy image) {
        ByteBuffer buffer = image.getPlanes()[0].getBuffer();
        buffer.rewind();
        byte[] bytes = new byte[buffer.capacity()];
        buffer.get(bytes);
        byte[] clonedBytes = bytes.clone();
        return BitmapFactory.decodeByteArray(clonedBytes, 0, clonedBytes.length);
    }
© www.soinside.com 2019 - 2024. All rights reserved.