Android的 - 如何从webview.capturePicture转换图片()为byte []和回位图 [英] Android - How to convert picture from webview.capturePicture() to byte[] and back to bitmap

查看:420
本文介绍了Android的 - 如何从webview.capturePicture转换图片()为byte []和回位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕捉的画面我是从webview.capturePicture获得()将其保存到一个sqliteDatabase,做我需要将图像转换为一个byte [],以便能够将其保存为BLOB在我的表,然后通过能够检索的byte [],并将其转换回位图。

I'm trying to capture the picture I'm getting from webview.capturePicture() to save it to an sqliteDatabase, to do I need to convert the image to a byte[] to be able to save it as a BLOB in my table, and then by able to retrieve that byte[] and convert it back to a bitmap.

下面是我在做什么:

Picture p = webView.capturePicture();  
ByteArrayOutputStream bos = new ByteArrayOutputStream();  
p.writeToStream(bos);

byte[] ba = bos.toByteArray());

然后,我通过检索图像:

I then retrieve the image by:

byte[] image = cursor.getBlob(imageColumnIndex);
Bitmap bm = BitmapFactory.decodeByteArray(image, 0, image.length);

我能够获取的byte []得很好,但我得到一个空的位图,从bitmapfactory所有的时间。

I'm able to retrieve the byte[] just fine but I get a null bitmap all the time from bitmapfactory.

我也注意到,如果我log.d(TAG,+ BOS)我得到很长的字节序列预期,但如果我这样做,以巴就在我做b​​os.toByteArray()我只是得到一个短阵,有些事情是这样的:[B @ 2b0a7c60

I also notice that if I log.d(TAG, ""+bos) I get a long sequence of bytes as expected but if I do the same to ba just after I do bos.toByteArray() I just get a short array, some thing like this: [B@2b0a7c60

我猜我有麻烦也许是的OutputStream转换到的字节数组。莫非这是因为通过capturePiture()方法返回一个ByteArrayOutputStream的OutputStream的呢?

I'm guessing I'm having trouble perhaps to convert by OutputStream to byteArray. Could this by because capturePiture() method returns an OutputStream instead of a ByteArrayOutputStream?

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

使用下面的二元函数转换::::

Use the below two function convert::::

public String convertBitmapToString(Bitmap src) {
    String str =null; 
    if(src!= null){
        ByteArrayOutputStream os=new ByteArrayOutputStream();
    src.compress(android.graphics.Bitmap.CompressFormat.PNG, 100,(OutputStream) os);
    byte[] byteArray = os.toByteArray();
    str = Base64.encodeToString(byteArray,Base64.DEFAULT);   
    }
    return str;         
}

public static Bitmap getBitMapFromString(String src)    {
    Bitmap bitmap = null;
if(src!= null){
   byte[] decodedString = Base64.decode(src.getBytes(), Base64.DEFAULT);
   bitmap = BitmapFactory.decodeByteArray(decodedString,0,decodedString.length);
}
return bitmap;
}

更新::

//Convert Picture to Bitmap
private static Bitmap pictureDrawable2Bitmap(Picture picture){
        PictureDrawable pictureDrawable = new PictureDrawable(picture);
        Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawPicture(pictureDrawable.getPicture());
        return bitmap;
    }

这篇关于Android的 - 如何从webview.capturePicture转换图片()为byte []和回位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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