Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am storing binary data in array (videoDataArr) and want to display in browser using javascript.

What I have tried:

for (var i = 0; i < video1Size - 14; i++)
{
    video1Data += String.fromCharCode(chunk8View[dataIndex++]);
}
        
video1Data += video1;
// alert("vid1:"+video1Data);       
       
if(partnumber == 003)
{
    //alert("partnumber:" + partnumber + "packetnumber:");

    videoDataArr.push(video1Data);

    alert("videoDataArr:" + videoDataArr);
              
    var imgdata=btoa(videoDataArr);
    //displayImg(imgdata);
            
    window.open('data:image/jpeg;,' + imgdata);
}
Posted
Updated 22-Jul-16 5:35am
v2
Comments
Beginner Luck 7-Jul-16 4:31am    
you using which?? canvas or svg or image to display because they use a bit different way??

1 solution

Need to convert it in base64.

JS have btoa() function for it.

For example:

JavaScript
var img = document.createElement('img');
img.src = 'data:image/jpeg;base64,' + btoa('your-binary-data');
document.body.appendChild(img);

But i think what your binary data in pastebin is invalid - the jpeg data must be ended on 'ffd9'.

Update:

Need to write simple hex to base64 converter:

JavaScript
function hexToBase64(str) {
    return btoa(String.fromCharCode.apply(null, str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
}

And use it:

JavaScript
img.src = 'data:image/jpeg;base64,' + hexToBase64('your-binary-data');

See working example with your hex data on jsfiddle
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900