如何获取 720p Android 相机预览数据?
我想使用相机预览进行图像识别。为了我的 目的,我需要预览分辨率尽可能高 (同时,向用户显示预览)。
我创建了一个 Preview 类,扩展 SurfaceView 并设置 预览尺寸为 1280x720。我添加了一个 PreviewCallBack 来获取实时信息 图片:
camera = Camera.open();
parameters = camera.getParameters();
parameters.setPreviewSize(1280,720);
camera.setParameters( parameters);
byte[] b = new byte[camera.getParameters().getPreviewSize().width *
camera.getParameters().getPreviewSize().height *
ImageFormat.getBitsPerPixel(camera.getParameters().getPreviewFormat()) / 8];
camera.addCallbackBuffer(b);
camera.setPreviewCallbackWithBuffer(new CameraPreviewCallback());
try {
camera.setPreviewDisplay(this.getHolder());
camera.startPreview()
}
我的字节数组 b 是 1382400 字节,我的 CameraPreviewCallback.onPreviewFrame() 函数接收那些 1382400 字节 - 但只有第一个 497664 字节包含数据(与 768x432 分辨率(HTC Desire))。
我在不同的设备上进行了测试,所有设备的显示分辨率均为 800x480(HTC Desire、LG Optimus 3D、三星 Galaxy S2、三星 Galaxy 选项卡,...)。我的代码唯一适用的设备是 HTC Desire HD。
有谁知道如何以字节形式接收完整的 720p 分辨率 大批? “某些东西”似乎会降低预览分辨率以适应智能手机的显示屏。
问候
乔恩
I want to use the camera preview for image recognition. For my
purposes, I need the preview resolution to be as high as possible
(and, at the same time, display the preview to the user).
I created a Preview class, extending SurfaceView and set the
PreviewSize to 1280x720. I added a PreviewCallBack to get the live
Image:
camera = Camera.open();
parameters = camera.getParameters();
parameters.setPreviewSize(1280,720);
camera.setParameters( parameters);
byte[] b = new byte[camera.getParameters().getPreviewSize().width *
camera.getParameters().getPreviewSize().height *
ImageFormat.getBitsPerPixel(camera.getParameters().getPreviewFormat()) / 8];
camera.addCallbackBuffer(b);
camera.setPreviewCallbackWithBuffer(new CameraPreviewCallback());
try {
camera.setPreviewDisplay(this.getHolder());
camera.startPreview()
}
My Byte Array b is 1382400 Byte and my
CameraPreviewCallback.onPreviewFrame() function receives those 1382400
Bytes - but only the first 497664 Byte contain data (matching a
768x432 resolution (HTC Desire)).
I tested this on different devices, all with display resolutions of
800x480 (HTC Desire, LG Optimus 3D, Samsung Galaxy S2, Samsung Galaxy
Tab, ...). The only device my Code works for is a HTC Desire HD.
Does anyone know how to receive the full 720p resolution as a Byte
Array?
"Something" seems to reduce the preview resolution to fit the smartphone display.
Regards
Joern
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能使用任何随机分辨率进行相机预览。
某些设备仅支持某些分辨率。您可以通过
Camera.Parameters.getSupportedPreviewSizes()< 查询这些/代码>
1。如果您的设备未在此列表中返回
1280x720
,则您无法使用它。选择一种接近您所需分辨率的受支持分辨率。1 一般情况下,您应该在使用任何分辨率(无论是否为 720p)之前执行此操作
You can't use any random resolution for a camera preview.
Certain devices only support certain resolutions. You can query these via
Camera.Parameters.getSupportedPreviewSizes()
¹. If your device doesn't return1280x720
in this list, you can't use it. Select a supported resolution that comes close to your desired one instead.¹ You should do so in general before using any resolution, 720p or not
尽管这是一个老问题,但答案没有改变:每个设备支持的预览尺寸列表并不准确。据我所知,默认预览大小始终有效。其他尺寸似乎最多只能根据具体情况而定 - 您可以有理由相信默认预览尺寸在屏幕上不会不正确,但仅此而已。特别是,即使“支持”,该字节数组也不会缩放以适应更大的图像格式,并且只有一些“支持”的预览尺寸将以分辨率正确渲染,并且不会出现绿色/粉色失真。
要知道分辨率是否正确以及 onPreviewFrame() 中的数据是否良好,唯一可靠的方法是手动测试。
Although this is an old question, the answer is unchanged: the list of supported preview sizes for each device is not accurate. The default preview size is, to the best of my knowledge, always valid. Other sizes appear to work at most on a case-by-case basis - you can be reasonably confident that a default preview size won't look incorrect on screen, but that's all. In particular, that Byte Array won't scale to accommodate larger image formats even if they're "supported", and only some of the "supported" preview sizes will be properly rendered at-resolution and without green/pink distortion.
The only reliable way to know that the resolution is correct and the data from onPreviewFrame() is good is to test this manually.