Uday Konduru
Uday Konduru

Reputation: 213

Image is not getting displayed from byte array in java

I had two doubts:
1. When i am trying to print The byte array i am getting from java into client side.I am getting strange value kind. I am not sure whether it's a byte array or pixel data.If it's not byte array then what correction needed to do.Code reference are pasted belowenter image description here
2.currently i am reading image from the database and writing into one byte array in server side and i am adding this byte array into json object and sending.But image is not getting displayed Please see the code below:

//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();
        });
    }

Might be it got little complicated to make you understand guyz but i tried my best.But let me know i will try in some other way.

Upvotes: 0

Views: 2199

Answers (1)

Wouter Lievens
Wouter Lievens

Reputation: 4019

To show in image with a data/base64 url, you need to encode it in Base64 format (see http://en.wikipedia.org/wiki/Base64). You could modify your backend to write your image into base64 format (see Java BufferedImage to PNG format Base64 String), as a string rather than an array. It's also way more compact for your JSON!

Upvotes: 1

Related Questions