Android将图像从图像的字节数组解码为位图

时间:2016-05-17 12:25:25

标签: android json image bitmap

我有一个问题。我不知道如何将图像从json响应中解码成位图。

response: [{"namae":"example",
    photo:[125,122,10,22,34,5,5,56,5,0,0,..........................,23]}]

我不知道如何从照片参数的值获取位图图像。

提前致谢。

2 个答案:

答案 0 :(得分:0)

使用decodeByteArray()

中的BitmapFactory

答案 1 :(得分:0)

从json获取字节,让我们调用它

bytes photoBytes=array.getJSONArray(0);

这样做:

BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(photoBytes,0,photoBytes.length,options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, <preferredWidth>, <preferredHeight>);
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        Bitmap bmp1=BitmapFactory.decodeByteArray(photoBytes,0,photoBytes.length,options);

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

    final int halfHeight = height / 2;
    final int halfWidth = width / 2;

    // Calculate the largest inSampleSize value that is a power of 2 and keeps both
    // height and width larger than the requested height and width.
    while ((halfHeight / inSampleSize) > reqHeight
            && (halfWidth / inSampleSize) > reqWidth) {
        inSampleSize *= 2;
    }
}

return inSampleSize;}

检查google site了解更多信息