我有两个疑问:
1.当我尝试将从java获取的字节数组打印到客户端时。我得到了奇怪的值类型。我不确定它是字节数组还是像素数据。如果不是字节数组那么需要做什么校正。代码引用粘贴在下面
2.目前我正在从数据库读取图像并写入服务器端的一个字节数组,并将该字节数组添加到json对象中并发送。但是图像没有显示请参阅下面的代码:
//this line will fetch the image data from db and i am storing into byte array
byte[] imageData = smpResource.getValue();
//i am adding this byte array into json object and sending.
JSONObject result = new JSONObject();
result.put("image", imageData);
//Client side code looks like:
var temp = null;
var jqxhr = jQuery.post(
'$link.getContextPath()/setEmployee.do',
{
empID: getSelectedEmpID(), empName: getSelectedEmpName()
},
function (data) {
jQuery.each(data, function(field, value){
// here in value i will be getting that byte array and i am storing in the below img src
if( "image" == field ) {
temp = value;
// please check the attachments and please confirm whether it was printing byte array or pixel data
alert("temp" + temp);
}
});
selectedImage.innerHTML = "<img src='data:image/jpg;base64, temp'/>";
,
"json"
).error( function(){
// if there was an error, select the parent...
selectedTreeNode.remove();
});
}
可能要让你理解 Guyz 有点复杂,但我尽力了。但是让我知道我会尝试其他方式。
最佳答案
要在带有 data/base64 url 的图像中显示,您需要将其编码为 Base64 格式(请参阅 http://en.wikipedia.org/wiki/Base64 )。您可以修改后端以将图像写入 base64 格式(请参阅 Java BufferedImage to PNG format Base64 String ),作为字符串而不是数组。它对于 JSON 来说也更加紧凑!
关于java - java 中的字节数组未显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25117383/