java - java 中的字节数组未显示图像

标签 java javascript html json image

我有两个疑问:
1.当我尝试将从java获取的字节数组打印到客户端时。我得到了奇怪的值类型。我不确定它是字节数组还是像素数据。如果不是字节数组那么需要做什么校正。代码引用粘贴在下面 enter image description here
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/

相关文章:

javascript - 与 div 中子项的垂直方向水平对齐

java - Jackson 如果 JSON 数组中存在某个值,则不解析整个项目

BigDecimal、除法和 MathContext - 非常奇怪的行为

java - 如何更改 ResponseEntity 的时间戳字段格式?

java - 如何垂直而不是水平排列数组?

javascript - 在 onClick 函数中传递输入文本框值 - php

javascript - Node.js 从其他函数调用函数不起作用

javascript - 切换多个元素的选项卡

javascript - 如何调用嵌套在表格单元格中的文本区域的属性?

html - 如何仅对 mat-card 应用不透明度,而不对 mat-card 内的其余内容应用不透明度?