如何将字节的json数组转换为位图

时间:2016-03-24 09:06:27

标签: java android bitmap bytearray

我需要将一个json数组的byte转换为位图图像,使用android.i得到像这样的json响应

{"type":"Buffer","data":[137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,1,140,0,0,1,34,4,3,0,0,0,24,1,248,231,0,0,0,21,80,76,84,69,255,255,255,0,0,0,220,217,207,0,100,0,176,196,222,0,153,204,204,255,255,227,179,150,92,0,0,1,29,73,68,65,84,120,156,237,207,1,13,128,64,12,4,193,211,130,5,44,96,1,11,248,151,192,91,104,66,66,191,153,85,176,147,99,68,249,123,224,155,48,58,181,24,217,62,140,78,205,98,156,165,146,171,84,114,151,74,158,82,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,157,25,187,135,209,169,73,140,9,97,116,106,8,227,5,178,0,69,109,104,207,36,38,0,0,0,0,73,69,78,68,174,66,96,130]} 

从postgres DB(类型:Bytea)读取此图像字节数组。

如何将字节数组转换为实际图像。

这是我转换数据的代码

img = profPic.getJSONArray("data");
bImage = Base64.decode(img.toString().getBytes(), Base64.DEFAULT);
bmp = BitmapFactory.decodeByteArray(bImage, 0, bImage.length);

2 个答案:

答案 0 :(得分:3)

试试这个

JSONObject obj=new JSONObject("{\"type\":\"Buffer\",\"data\":[137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,1,140,0,0,1,34,4,3,0,0,0,24,1,248,231,0,0,0,21,80,76,84,69,255,255,255,0,0,0,220,217,207,0,100,0,176,196,222,0,153,204,204,255,255,227,179,150,92,0,0,1,29,73,68,65,84,120,156,237,207,1,13,128,64,12,4,193,211,130,5,44,96,1,11,248,151,192,91,104,66,66,191,153,85,176,147,99,68,249,123,224,155,48,58,181,24,217,62,140,78,205,98,156,165,146,171,84,114,151,74,158,82,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,157,25,187,135,209,169,73,140,9,97,116,106,8,227,5,178,0,69,109,104,207,36,38,0,0,0,0,73,69,78,68,174,66,96,130]} ");
Bitmap bitmap=null;
byte[] tmp=new byte[obj.getJSONArray("data").length()];
for(int i=0;i<obj.getJSONArray("data").length();i++){
     tmp[i]=(byte)(((int)obj.getJSONArray("data").get(i)) & 0xFF);
}
bitmap= BitmapFactory.decodeByteArray(tmp, 0, tmp.length);

答案 1 :(得分:0)

试试这个

byte[] bitmapdata = blob.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);