Android byte[] 到 Camera.onPreviewFrame 中的图像
当尝试使用 BitmapFactory.decodeByteArray
将 Camera.onPreviewFrame
的 byte[]
转换为 Bitamp
时,出现错误SkImageDecoder::Factory 返回 null
以下是我的代码:
public void onPreviewFrame(byte[] data, Camera camera) {
Bitmap bmp=BitmapFactory.decodeByteArray(data, 0, data.length);
}
When trying to convert the byte[]
of Camera.onPreviewFrame
to Bitamp
using BitmapFactory.decodeByteArray
gives me an error SkImageDecoder::Factory returned null
Following is my code:
public void onPreviewFrame(byte[] data, Camera camera) {
Bitmap bmp=BitmapFactory.decodeByteArray(data, 0, data.length);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这已经很难找到了!但从 API 8 开始,android.graphics 中有一个 YuvImage 类。它不是图像后代,因此您所能做的就是将其保存到 Jpeg,但您可以将其保存到内存流,然后加载到位图图像(如果您需要的话)。
This has been hard to find! But since API 8, there is a YuvImage class in android.graphics. It's not an Image descendent, so all you can do with it is save it to Jpeg, but you could save it to memory stream and then load into Bitmap Image if that's what you need.
从 Android 3.0 开始,您可以使用
TextureView
和TextureSurface
来显示相机,然后使用mTextureView.getBitmap()
检索友好的 RGB 预览框架。TextureView 文档中给出了如何执行此操作的一个非常简单的示例。请注意,您必须将
android:hardwareAccelerated="true"
放入清单中,将应用程序或 Activity 设置为硬件加速。Since Android 3.0 you can use a
TextureView
andTextureSurface
to display the camera, and then usemTextureView.getBitmap()
to retrieve a friendly RGB preview frame.A very skeletal example of how to do this is given in the TextureView docs. Note that you'll have to set your application or activity to be hardware accelerated by putting
android:hardwareAccelerated="true"
in the manifest.很久之后我找到了答案。就是这样...
我没有使用 BitmapFactory,而是使用自定义方法将此 byte[] 数据解码为有效的图像格式。要将图像解码为有效的图像格式,需要通过调用
camera.getParameters().getPictureFormat()
。这将返回一个由ImageFormat
定义的常量。知道格式后,使用适当的编码器对图像进行编码。就我而言,
byte[] 数据
采用YUV
格式,因此我寻找YUV
到BMP
转换并解决了我的问题。I found the answer after a long time. Here it is...
Instead of using
BitmapFactory
, I used my custom method to decode thisbyte[] data
to a valid image format. To decode the image to a valid image format, one need to know what picture format is being used by the camera by callingcamera.getParameters().getPictureFormat()
. This returns a constant defined byImageFormat
. After knowing the format, use the appropriate encoder to encode the image.In my case, the
byte[] data
was in theYUV
format, so I looked forYUV
toBMP
conversion and that solved my problem.你可以试试这个:
此示例将相机帧发送到服务器
you can try this:
This example send camera frames to server