Android中某些相机分辨率下的不同捕获输出大小 [英] Different capture output size on certain camera resolutions in Android

查看:83
本文介绍了Android中某些相机分辨率下的不同捕获输出大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android Camera2 API开发自定义相机应用程序,您可以在其中切换手机中可用的不同相机分辨率和视频分辨率.它还提供了拍摄1:1平方照片的可能性.要拍摄方形图片,我拍摄一张普通的4:3图片,然后将其裁剪以保持1:1.(因此4032x3024将是3024x3024).

I am developing a custom camera application using the Android Camera2 API where you can switch between the different camera and video resolutions available in the phone. It also offers the possibility to take squared 1:1 pictures. To take the square pictures, I take a normal 4:3 picture and then crop it to maintain the 1:1. (So 4032x3024 would be 3024x3024).

在某些分辨率下以1:1的比例拍摄照片时,我注意到一个问题,输出被略微裁剪(缩放).这是用两种不同分辨率拍摄的同一张照片的结果:

I noticed a problem when taking a 1:1 picture on certain resolutions, the output is slightly cropped (zoomed). This is the result of the same picture taken with two different resolutions:

第一张照片拍摄于1944x1944

第二张照片是在3024x3024拍摄的

我的Nexus 5X在4:3上支持12MP,8MP,5MP和2MP.当我使用大于5MP的任何分辨率时,就会发生此问题.

My Nexus 5X supports 12MP, 8MP, 5MP and 2MP on 4:3. This problem happens when I use any resolution bigger than 5MP.

我用来裁剪图像的方法如下:

The method I use to crop the images is the following:

ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
cropSquareImageByteArray(bytes);

cropSquareImageByteArray方法:

The cropSquareImageByteArray method:

public static byte[] cropSquareImageByteArray(byte[] bytes) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Bitmap dst = Bitmap.createBitmap(bitmap, 0, h - w, w, w);
    dst.compress(Bitmap.CompressFormat.JPEG, 98, bos);
    return bos.toByteArray();
}

我猜测裁剪的原因是在16:9容器中的4:3图像.因为我注意到在致电

I am guessing the reason for the cropping is a 4:3 image in a 16:9 container. Because I noticed that when calling

    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

生成的位图输出的尺寸在2MP中为1280x960(4:3),在5MP中为1600x1200(4:3),但对于较大的分辨率为1920x1080(16:9),因此将4:3图像调整为16:9位图,可能会导致裁剪.

The dimensions for the generated bitmap output are 1280x960 (4:3) in 2MP, 1600x1200 (4:3) in 5MP but for bigger resolutions are 1920x1080 (16:9), so the 4:3 image is adjusted to a 16:9 bitmap, maybe causing the crop.

我正在尝试找出解决方法.我还检查了此帖子 Android5.0预览表面上的作物区域错误并捕获了静止图像,但在那里找不到解决方案.

I am trying to figure out how to solve this. I also checked this post Android 5.0 Wrong crop regions on preview surface and captured still image but didn't find a solution there.

* edit:我的ImageReader的配置如下:

*edit: My ImageReader is configured like this:

public void configureImageReader(Size pictureSizeValue, ImageReader.OnImageAvailableListener listener) {

    if (mImageReader == null) {
        mImageReader = ImageReader.newInstance(pictureSizeValue.getWidth(), pictureSizeValue.getHeight(),
                ImageFormat.JPEG, 2);
    } 
    mImageReader.setOnImageAvailableListener(listener, mBackgroundHandler);

}

pictureSizeValue 的值是我想要的输出.因此,对于平方的图像,它类似于3024x3024.

The value for pictureSizeValue is the output I want. So for a squared image, it is something like 3024x3024.

推荐答案

在特定点之前,摄像头设备会将您请求的尺寸四舍五入为支持的尺寸,以使摄像头预览的设置更加灵活,但最多只能1080p(通常可以保证合理的预览帧速率).

Up to a certain point,the camera device will round your requested size to a supported one, to make camera preview more flexible to set up, but only up to 1080p (to generally guarantee a reasonable frame rate for preview).

过去,您实际上需要从设备提供的列表中选择一个受支持的分辨率,否则您将舍入到1080p.

Past that, you need to actually select a supported resolution from the list provided by the device, or you'll just get rounded down to 1080p.

可以使用 StreamConfigurationMap查询相机支持的输出大小和格式. .getOutputSizes().您需要在列表中扫描符合您要求的尺寸,并且其纵横比与图像传感器的纵横比匹配(可通过

The camera's supported output sizes and formats can be queried with StreamConfigurationMap.getOutputSizes(). You'll need to scan the list for a size that matches your requirements and has an aspect ratio that matches the aspect ratio of the image sensor (available through activeArraySize)

我还要仔细检查所有1080p以下的尺寸,以确保您确实得到了想要的东西.四舍五入总是将分辨率降低,以最大程度地降低性能和内存要求.

I'd double-check all the sizes below 1080p as well, to make sure you're actually getting what you want. The rounding is always to a lower resolution, to minimize performance and memory requirements.

这篇关于Android中某些相机分辨率下的不同捕获输出大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆