gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:48:50 25 4
gpt4 key购买 nike

我需要使用 android 将字节的 json 数组转换为位图图像。我得到了这样的 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(type:Bytea) 中读取的。

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

这是我转换数据的代码

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

最佳答案

试试这个

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);

关于java - 如何将字节的json数组转换为位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36196471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com