0

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.

Uday Konduru
  • 203
  • 1
  • 4
  • 16
  • Where you assign innerHTML, you just use "temp" inside the string literal. You need to concatenate the variables, i.e. selectedImage.innerHTML = ""; – Wouter Lievens Aug 04 '14 at 11:24
  • @WouterLievens--i tried in that way also but still image is not getting displayed..and wanted to know the snapshot i added whether its a byte array format or some other ..i am not getting exactly – Uday Konduru Aug 04 '14 at 11:28

1 Answers1

1

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!

Community
  • 1
  • 1
Wouter Lievens
  • 4,019
  • 5
  • 41
  • 66
  • :--thank you for guidance but still image is not getting displayed.added the byte[] imageData = smpResource.getValue(); String enocdeString = Base64.encodeBase64URLSafeString(imageData); and also in the json object changed like result.put("image", enocdeString); now i can see the in client side proper byte array format is coming,but still i can see the image is null – Uday Konduru Aug 04 '14 at 11:38
  • First try with manually created HTML and manually copying your generated base64 string, to verify if it's encoded correctly. – Wouter Lievens Aug 04 '14 at 11:44
  • @Wouter-- tried manually also by copy pasting but still i can see there is no image getting displayed – Uday Konduru Aug 04 '14 at 12:12