Byte数组图像位图 [英] Byte array image to bitmap

查看:939
本文介绍了Byte数组图像位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想字节数组图像转换为位图,但转换之后它给了我黑色的形象,为什么呢?

  //相机ARG转换为位图
                位图cameraBitmap = BitmapFactory.de codeByteArray(为arg0,0,
                        arg0.length);
                 位图背景= Bitmap.createBitmap(cameraBitmap.getWidth()
                 cameraBitmap.getHeight(),Bitmap.Config.ARGB_8888);
 

我希望相机的图像转换为位图。任何建议。在此先感谢!

 二月7日至17日:22:18.149:E / AndroidRuntime(398):致命异常:主要
二月七号至17号:22:18.149:E / AndroidRuntime(398):java.lang.IllegalArgumentException:如果只支持ImageFormat.NV21和ImageFormat.YUY2现在
二月七号至17号:22:18.149:E / AndroidRuntime(398):在android.graphics.YuvImage< INIT>(YuvImage.java:82)
二月七号至17号:22:18.149:E / AndroidRuntime(398):在com.exercise.AndroidCamera.AndroidCamera $ 4.onPictureTaken(AndroidCamera.java:225)
二月七号至17号:22:18.149:E / AndroidRuntime(398):在android.hardware.Camera $ EventHandler.handleMessage(Camera.java:320)
二月七号至17号:22:18.149:E / AndroidRuntime(398):在android.os.Handler.dispatchMessage(Handler.java:99)
二月七号至17号:22:18.149:E / AndroidRuntime(398):在android.os.Looper.loop(Looper.java:123)
二月七号至17号:22:18.149:E / AndroidRuntime(398):在android.app.ActivityThread.main(ActivityThread.java:4627)
二月七号至17号:22:18.149:E / AndroidRuntime(398):在java.lang.reflect.Method.invokeNative(本机方法)
二月七号至17号:22:18.149:E / AndroidRuntime(398):在java.lang.reflect.Method.invoke(Method.java:521)
二月七号至17号:22:18.149:E / AndroidRuntime(398):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
二月七号至17号:22:18.149:E / AndroidRuntime(398):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
二月七号至17号:22:18.149:E / AndroidRuntime(398):在dalvik.system.NativeStart.main(本机方法)
 

解决方案

不幸的是,相机的图像格式不是ARGB_8888。最有可能的是像YUV420P或者甚至是JPEG-COM pressed数据。

首先,决定什么是你的情况。

您有两种选择:为YUV(这是一种交错格式)使用一些转换格式和JPEG创建一个MemoryStream您将arg0阵列并从中读出位图

有类似的问题在这里:在安卓 BitmapFactory空问题

还有就是即使在一个解决方案:<一href="http://stackoverflow.com/questions/5212531/android-byte-to-image-in-camera-on$p$pviewframe">Android byte []的图像在Camera.on previewFrame

编辑:你只需要裂伤的字节数组中的一点点

维基文章介绍如何将YUV422 / 420到YUY2转换,由Android的要求API。 搜索Y'UV422也可以pssed在YUY2格式的FourCC code EX $ P $串那里。

I'm trying to convert the byte array image to bitmap, but after conversion it gives me black image, why?

// Camera arg conversion to Bitmap
                Bitmap cameraBitmap = BitmapFactory.decodeByteArray(arg0, 0,
                        arg0.length);
                 Bitmap background = Bitmap.createBitmap(cameraBitmap.getWidth(),
                 cameraBitmap.getHeight(), Bitmap.Config.ARGB_8888);

I want the camera image converted to bitmap. Any suggestion. Thanks in advance!!

07-17 02:22:18.149: E/AndroidRuntime(398): FATAL EXCEPTION: main
07-17 02:22:18.149: E/AndroidRuntime(398): java.lang.IllegalArgumentException: only support ImageFormat.NV21 and ImageFormat.YUY2 for now
07-17 02:22:18.149: E/AndroidRuntime(398):  at android.graphics.YuvImage.<init>(YuvImage.java:82)
07-17 02:22:18.149: E/AndroidRuntime(398):  at com.exercise.AndroidCamera.AndroidCamera$4.onPictureTaken(AndroidCamera.java:225)
07-17 02:22:18.149: E/AndroidRuntime(398):  at android.hardware.Camera$EventHandler.handleMessage(Camera.java:320)
07-17 02:22:18.149: E/AndroidRuntime(398):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 02:22:18.149: E/AndroidRuntime(398):  at android.os.Looper.loop(Looper.java:123)
07-17 02:22:18.149: E/AndroidRuntime(398):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-17 02:22:18.149: E/AndroidRuntime(398):  at java.lang.reflect.Method.invokeNative(Native Method)
07-17 02:22:18.149: E/AndroidRuntime(398):  at java.lang.reflect.Method.invoke(Method.java:521)
07-17 02:22:18.149: E/AndroidRuntime(398):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-17 02:22:18.149: E/AndroidRuntime(398):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-17 02:22:18.149: E/AndroidRuntime(398):  at dalvik.system.NativeStart.main(Native Method)

解决方案

Unfortunately, the camera's image format is not ARGB_8888. Most likely it's something like YUV420p or maybe even the JPEG-compressed data.

First of all, determine what is your case.

You have two options: for YUV (which is an interleaved format) use some conversion format and for JPEG create a memorystream for your arg0 array and read the Bitmap from it.

There are similar questions here: BitmapFactory null issue in android

And there is even a solution here: Android byte[] to image in Camera.onPreviewFrame

EDIT: you just have to mangle the bytes in your array a little.

This wiki article explains how to convert the YUV422/420 to YUY2, required by the Android API. Search for the "Y'UV422 can also be expressed in YUY2 FourCC format code" substring there.

这篇关于Byte数组图像位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆