問題描述
我正在使用相機的 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!