Android Camera2捕获歪斜的图像 [英] Android Camera2 capture image skewed

查看:196
本文介绍了Android Camera2捕获歪斜的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:看起来与此相关:

Update: This looks like it's related to this: Image data from Android camera2 API flipped & squished on Galaxy S5 - I consider this as a bug since Nexus 5/6 works correctly and it makes no sense to need to obtain full sensor size and then cropping manually to reach the desired aspect ratio, might as well not using "supported" output sizes as well!

问题:

  1. 使用Camera2 API获取相机的特征,并提取适合MediaCodec.class的输出大小
  2. 使用适当的相机输出大小之一创建MediaCodec输入表面.将输出馈送到某些MediaMuxer或其他任何设备,以查看输出.
  3. 使用编解码器创建的表面作为目标来启动摄像机捕获请求.
  4. 编解码器输出具有正确的大小.但是结果因设备而异:

  1. Get characteristics of a camera using Camera2 API, and extract output sizes suitable for a MediaCodec.class
  2. Create a MediaCodec input surface with one of the suitable camera output sizes. Feed the output to some MediaMuxer or whatever, to see the output.
  3. Start camera capture requests using the codec's created surface as the target.
  4. Codec output has the correct size. But the result differs by device:

  • Nexus 5/6:在Android 5/6上一切正常.
  • 带有Android 5.1的三星平板电脑:对于某些分辨率,图像明显被拉伸,表明相机输出分辨率与表面尺寸不匹配.当开始旋转相机时,这一点变得非常明显-由于图像未与X/Y轴对齐,因此图像变得越来越歪斜.对于其他一些分辨率,输出正常.此处没有与大小或纵横比有关的图案.

没问题,有人会说.也许不是完全按照指定的宽度和高度或任何其他方式创建了曲面(即使输出尺寸是专门针对MediaCodec.class目标提取的).

No problem, one would say. Maybe the surface is not created exactly at the specified width and height, or whatever (even if the output sizes were extracted specifically for a MediaCodec.class target).

因此,我创建了一个OpenGL上下文,生成了一个纹理,为其创建了SurfaceTexture,将其默认缓冲区大小设置为相机输出大小,并使用该纹理创建了Surface.我将不涉及将其绘制到TextureView或回到MediaCodec的EGL表面的细节.结果是相同的-Camera2捕获请求仅针对某些分辨率输出失真的图像. 深入研究:在updateTexImage之后立即在SurfaceTexture上调用getTransformMatrix-矩阵始终是单位矩阵,符合预期.

So, I created an OpenGL context, generated a texture, a SurfaceTexture for it, set its default buffer size to the camera output size, and created a Surface using the texture. I won't go into the gory details of drawing that to a TextureView or back to the MediaCodec's EGL surface. The result is the same - the Camera2 capture requests outputs a distorted image only for some resolutions. Digging deeper: calling getTransformMatrix on the SurfaceTexture immediately after updateTexImage - the matrix is always the identity matrix, as expected.

因此,这里的真正问题是相机无法按照提供的目标表面的尺寸进行拍摄.因此,解决方案将是获取相机正在捕获的 actual 大小,其余的是纯GL矩阵变换以正确绘制.但是-我怎么知道的?

So, the real problem here is that the camera is NOT capturing at the size of the provided target surface. The solution would thereby be to get the actual size the camera is capturing, and the rest is pure GL matrix transforms to draw correctly. But - HOW DO I GET THAT?

注意:使用旧的Camera API,其预览尺寸"和目标表面完全相同(相同)(无论是MediaCodec还是自定义的),一切都很好!但是我不能使用旧的相机API,因为它已经过时,并且似乎最大捕获尺寸为1080p,而Camera2 API超出了该范围,我需要支持4k录制.

Note: using the old Camera API, with exactly the same "preview size" and the same surface as the target (either MediaCodec's or the custom one) - ALL IS FINE! But I can't use the old camera API, since it's both deprecated and also seems to have a max capture size of 1080p, while the Camera2 API goes beyond that, and I need to support 4k recording.

推荐答案

是的,这是那些三星设备上的错误.

So yes, this is a bug on those Samsung devices.

通常,当您在输出中要求多个不同的宽高比时,这种情况就会发生,并且特定于设备的相机代码会在正确裁剪和缩放所有比例时跳到自身上方.您可以通过确保所有请求的尺寸都具有相同的宽高比来避免这种情况.

Generally this happens when you ask for multiple different aspect ratios on output, and the device-specific camera code trips over itself on cropping and scaling all of them correctly. You may be able to avoid it by ensuring all requested sizes have the same aspect ratio.

分辨率可能实际上是您所要求的-但缩放比例不正确(您可以使用ImageReader以有问题的大小对其进行测试,在该分辨率下可以戳到一个明确的缓冲区.)

The resolution is probably actually what you asked for - but it's been incorrectly scaled (you could test this with an ImageReader at the problematic size, where you get an explicit buffer you can poke at.)

我们正在向Android法规遵从性测试中添加其他测试,以确保不会继续发生此类拉伸输出.

We are adding additional testing to the Android compliance tests to try to ensure these kinds of stretched outputs don't continue to happen.

这篇关于Android Camera2捕获歪斜的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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