We are using websocket to transfer the data in bytearray. On the receiving end, the javascript has to determine whether the ArrayBuffer received is of type String or Image. If it is String then convert to String otherwise convert to Blob. Something as below, except i don't know how to recognize the arraybuffer is string or image.
if (message instanceof ArrayBuffer) {
if(message of type string)
var bytearray = new Uint8Array(message);
to convert to string
var out = String.fromCharCode.apply(null, bytearray);
console.log(out);
}
if(message of type image){
imageId.height = 400;
imageId.width = 600;
imageId.src = convertArrayBufferBlobUrl(message);
}
Update:
From the server i will send the image as folloing java code. How can i append or prepend flag in the byte array to identify as image or string by the client browser(javascript)?
ImageIO.write(getSubImage(bufferedImage,width,height), "png", baos ); byte[] result=baos.toByteArray();