簡體   English   中英

無法從Java中的字節數組顯示圖像

[英]Image is not getting displayed from byte array in java

我有兩個懷疑:
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轉換為PNG格式Base64 String )。 對於您的JSON來說,它也更緊湊!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM