简体   繁体   English

Camera2上的ImageReader Api android

[英]ImageReader on Camera2 Api android

I'm starting to use Camera2 Api instead of the deprecated Camera Api but I have a problem. 我开始使用Camera2 Api而不是弃用的Camera Api,但我遇到了问题。 I set up an ImageReader with 我用它设置了一个ImageReader

ImageReader.newInstance(100, 130, ImageFormat.YUV_420_888, 2);

But when I log the image dimensions in onImageAvailable: 但是当我在onImageAvailable中记录图像尺寸时:

Image img = null;
        img = imageReader.acquireLatestImage();

        if(img != null){

            Log.d("Dimen3", Integer.toString(img.getHeight()) + " " + img.getWidth());}

It says: 它说:

08-24 18:25:43.115 28363-28363/com.example.ale.camera2prova D/Dimen3: 144 176

Why the dimensions are changed? 为什么尺寸会发生变化?

If I understand your question correctly, you are asking why you can't use arbitrary dimensions for your ImageReader image. 如果我正确理解您的问题,您就会问为什么不能为ImageReader图像使用任意尺寸。 The answer to that is that is how they designed the ImageReader. 答案就是他们设计ImageReader的方式。 The ImageReader is probably choosing a pre defined size which is closest to your requested size. ImageReader可能正在选择最接近您请求大小的预定义大小。 To avoid this, choose a size from the pre defined list. 要避免这种情况,请从预定义列表中选择一个大小。 I wrote a little routine which gets the list of sizes and chooses the smallest one which is equal to or bigger than the screen size: 我写了一个小例程,它获取大小列表并选择最小的一个等于或大于屏幕大小:

private Size selectImageSize() {
    CameraCharacteristics cameraCharacteristics = null;
    try {
        cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);
    } catch (CameraAccessException e) {
        return null;
    }

    StreamConfigurationMap streamConfigurationMap = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
    if (streamConfigurationMap == null) {
        return null;
    }
    jpegSizes = streamConfigurationMap.getOutputSizes(ImageFormat.JPEG);

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int screenLength = size.x;
    if (screenLength < size.y) {
        screenLength = size.y;
    }

    int index = 0;
    int finalIndex = 0;
    int finalJpegLength = Integer.MAX_VALUE;
    for (Size jpegSize : jpegSizes) {
        int jpegLength = jpegSize.getWidth();
        if (jpegLength < jpegSize.getHeight()) {
            jpegLength = jpegSize.getHeight();
        }
        if (jpegLength >= screenLength) {
            if (jpegLength < finalJpegLength) {
                finalJpegLength = jpegLength;
                finalIndex = index;
            }
        }
        index++;
    }

    Size returnSize = new Size(jpegSizes[finalIndex].getWidth(), jpegSizes[finalIndex].getHeight());
    return returnSize;
}

I am trying to get the smallest image size which is equal to or larger than the screen size because using that provides the fastest speed while providing an image which will fill the device's screen. 我试图获得等于或大于屏幕尺寸的最小图像尺寸,因为使用它可以提供最快的速度,同时提供将填满设备屏幕的图像。

You can just set in your imageReader the available sizes that your phone support. 您只需在imageReader中设置手机支持的可用尺寸即可。 You can know it with this method: 你可以用这种方法知道它:

public static Size[] previewSizes;
public static Size[] videoSizes;
public static Size[] pictureSizes;

     StreamConfigurationMap map = cameraCharacteristics.get(                    CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

        previewSizes = map.getOutputSizes(SurfaceTexture.class);
        videoSizes = map.getOutputSizes(MediaRecorder.class);
        pictureSizes = map.getOutputSizes(ImageFormat.JPEG);

You can just set in your imageReader the results from your pictureSizes call(also, depending from which format of ImageReader are we talking about, some devices will support some sizes in YUV or JPEG format, but not in RAW for example). 您可以在imageReader中设置pictureSizes调用的结果(同样,根据我们所讨论的ImageReader格式,某些设备将支持YUV或JPEG格式的某些尺寸,但不支持RAW)。

Also you can't configure multiple ImageReaders in specific sizes, it should be a problem if you are working in a Camera app with so many modes. 此外,您无法配置特定大小的多个ImageReader,如果您在具有如此多模式的Camera应用程序中工作,这应该是一个问题。 You can read more about it in the next link: https://developer.android.com/reference/android/hardware/camera2/CameraDevice.html 您可以在下一个链接中阅读更多相关信息: https//developer.android.com/reference/android/hardware/camera2/CameraDevice.html

Hope that it will help you! 希望它能帮到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM