问题描述
我正在使用相机的 previewCallback 来尝试抓取图像.这是我正在使用的代码
I am using the previewCallback from the camera to try and grab images. Here is the code I am using
private Camera.PreviewCallback mPrevCallback = new Camera.PreviewCallback()
{
public void onPreviewFrame( byte[] data, Camera Cam ) {
Log.d("CombineTestActivity", "Preview started");
Log.d("CombineTestActivity", "Data length = "
+ data.length );
currentprev = BitmapFactory.decodeByteArray( data, 0,
data.length );
if( currentprev == null )
Log.d("CombineTestActivity", "currentprev is null" );
Log.d("CombineTestActivity", "Preview Finished" );
}
};
数据的长度总是与576000相同.
the length of the data always comes otu the same as 576000.
我还尝试更改相机的参数,以便图像以不同的格式返回.这是我这样做时的样子.
Also I have tried changing the parameters of the camera so the image comes back as different formats. Here is what it looks like when I do that.
mCamera = Camera.open();
camParam = mCamera.getParameters();
camParam.setPreviewFormat( ImageFormat.RGB_565 );
mCamera.setParameters( camParam );
mCamera.setPreviewCallback( mPrevCallback );
但是,当我更改预览格式以及将其保留为 NV21 的默认值时,BitmapFactory.decodeByteArray 返回为空.我还尝试将预览格式更改为 JPEG 类型.我什至在 ddms 中得到了一个调试语句,这就是我得到的
However both when I change the preview format and when I leave it as its default of NV21, BitmapFactory.decodeByteArray comes back as null. I have also tried changing the preview format to JPEG type. I even get a debug statement in the ddms, this is what I get
D/skia (14391): --- SkImageDecoder::Factory 返回 null"
"D/skia (14391): --- SkImageDecoder::Factory returned null"
推荐答案
好的,希望这会有所帮助.
Alright, hopefully this will help.
搜索互联网寻找快速解决方案,并找到了完美的东西.
Scoured the internet looking for a fast solution, and found the perfect thing.
这适用于 Android 2.1
感谢 此页面的 off3nsiv3.
Thanks to off3nsiv3 from this page.
// Convert to JPG
Size previewSize = camera.getParameters().getPreviewSize();
YuvImage yuvimage=new YuvImage(data, ImageFormat.NV21, previewSize.width, previewSize.height, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yuvimage.compressToJpeg(new Rect(0, 0, previewSize.width, previewSize.height), 80, baos);
byte[] jdata = baos.toByteArray();
// Convert to Bitmap
Bitmap bmp = BitmapFactory.decodeByteArray(jdata, 0, jdata.length);
只需对 off3nsiv3 的代码稍作修改即可.与手动解码相比,FPS 仍然非常高.
Just a little modification to off3nsiv3's code and you're set. The FPS is still incredibly high compared to manual decoding.
对于上面的代码,80 是 jpeg 质量(0 从 100,100 最好).
For the above code, the 80 is the jpeg quality (0 from 100, 100 being best).
这篇关于BitmapFactory.decodeByteArray() 返回 NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!