Extracting binary from blob in JS

Source: Internet
Author: User

Article structure:

First, the problems encountered

Second, the solution

One

Server side through the WebSocket to the browser side of the picture (binary), you need to display the picture in different locations according to different pictures, a feasible way is to first convert the picture into a binary array, and then the binary array and a byte of the picture to be stitched together in a piece to the browser side, Finally split on the browser side. Looking for a long time to find some JS processing binary related methods, here to record a bit.

Two

1, server-side binary splicing:

 Public byte [] Mergebyte (byte[] b1,byte[] b2) {                byte[] b3=newbyte[ b1.length+B2.length];                System.arraycopy (B1,0,b3,0, b1.length);                System.arraycopy (B2,0, b3,b1.length,b2.length);                 return b3;            }

2, browser-side binary split

The main use of JS in the Blob and FileReader, about these two classes of knowledge, you can view

Http://www.iunbug.com/archives/2012/06/06/273.html

Https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader

Ws.onmessage =function(event) {if(Event.datainstanceofBlob) {            varBlob =Event.data; The Blob is split first, the first byte is the identityvarNewblob=blob.slice (0,1);
The blob in JS does not have a method to read its data directly, and FileReader to read the relevant datavarReader =NewFileReader (); Reader.readasbinarystring (NEWBLOB); varImgflag;
Called when the read operation completes successfully. Reader.onload=function(evt) {if(Evt.target.readyState = =Filereader.done) { varImgflag =Evt.target.result; if(imgflag== ' @ ') {img=document.getelementbyid (' er '); }Else if(imgflag== ' A ') {img=document.getelementbyid (' Photo '); }Else{img=document.getelementbyid (' Photo '); } Newblob=blob.slice (1, blob.size); Window. URL= window. URL | |Window.webkiturl; varSource =window. Url.createobjecturl (NEWBLOB); IMG.SRC=source; } } }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.