BitmapFactory.decodeByteArray() 返回 NULL

2022-11-01Java开发问题
6

本文介绍了BitmapFactory.decodeByteArray() 返回 NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用相机的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13