javascript - Why won't my images load up from my java servlet? -
i'm connecting ip cam via python , running script in java servlet. loading each image in java , converting byte array base64 encoding. lastly ask data via ajax through json object. don't receive errors images don't appear either.
javascript:
$().ready(calltoserver); function calltoserver(){ $.ajax({ type:"get", url:"droneservlet.do", async:true, data: null, datatype:"json", success: drawimage,//function success error: badrequest//function error }); settimeout(calltoserver,5000); } function toarray(str){ var i; var bytes=[]; for(i=0;i<str.length;i++) { bytes.push(str.charat(i)); } return bytes; } function badrequest(){ console.log("failed"); } function drawimage(json) { var img = json.imgdata[0].imagetodisplay; var bistr=toarray(img); var data = bistr.join(''); //var base64 = window.btoa(data); document.getelementbyid("dynamic").src = "data:image/jpeg;base64," + data; }
java snippet:
byte[] imgdata = pythonrunner.grabphotobytes();//loads single image , convert byte[] jsonobject jsonresponse = new jsonobject(); jsonarray jsonarray = new jsonarray(); jsonobject img = new jsonobject(); string encodedimg = base64.encodebase64string(imgdata); img.put("imagetodisplay", encodedimg); jsonarray.add(img); jsonresponse.put("imgdata", jsonarray); out.println( jsonresponse.tojsonstring() ); // send weatherdata collection in json format
sending image base64 worst thing here, image , save on server local system using imageio
class, , of course name(maybe random).
first have saved data following, on server system /root/image_folks/trejon_house7222015-0342.jpeg
name random or anything.
response image name json response client simple string, , client ask /get-image
cgi/servlet image(like /get-image?id=trejon_house7222015-0342
).
now have simple servlet loads server local file , response client, don't forget specify mime type
Comments
Post a Comment