1. Videostream
HTML5 TheMedia Capture API provides the programmable access to video camera on phones, users canuse the getUserMedia() method to get the video stream provided by video camera.What we need to do is to add a HTML <video> tag and make the videostream from the video camera as the input to the <video> tag. (Please note currently only Chrome and Operasupport getUserMedia() method)
- <video id="video" autoplay=""></video>
- <script>
- var video_element = document.getElementById('video');
- if (navigator.getUserMedia) { // opera should use opera.getUserMedia now
- navigator.getUserMedia('video',success, error);
- }
- function success(stream) {
- video_element.src =stream;
- }
- </script>
Video stream
2. Takingphoto
We will use HTML5’s Canvas tocapture the contents of <video> tag in real time. <video> tag canbe the input of Canvas. The code of this functionality is :
- <script>
- var canvas =document.createElement('canvas');
- var ctx = canvas.getContext('2d');
- var cw = vw;
- var ch = vh;
- ctx.fillStyle = "#ffffff";
- ctx.fillRect(0, 0, cw, ch);
- ctx.drawImage(video_element, 0, 0, vvw,vvh, 0,0, vw,vh);
- document.body.append(canvas);
- </script>
3. Capturepicture
Next we willretrieve picture data from Canvas, the key here is to use Canvas’s toDaatURL(0to convert picture data into base64 encoded PNG picture data. The data has theformat similar to “data image/png,base64,xxxxxâ€
- var imgData =canvas.toDataURL("image/png");
Because the useful picture data is the portion after “base64,†on the above whichis xxxxx.So we actually only need to processthis portion of data. We can use two methods here/
a). We retrieve the data after 22 bytes of the data as picture data onclient side
- var data = imgData.substr(22);
If we want to get the picture size before uploading it, we can use
var length = atob(data).length;// atob decodes a string of data which has been encoded using base-64 encoding
b). On theserver side to retrieve the picture data after 22 bytes. For example in PHP:
- $image = base64_decode( str_replace('data:image/jpeg;base64,', '',$data);
4. Pictureuploading
On client side we can use Ajax to uploading the picture data to serverside script. If using jQuery:
$.post('upload.php',{ 'data' : data } );
On serverside we can use PHP script to receive and store the picture
- function convert_data($data){
- $image = base64_decode( str_replace('data:image/jpeg;base64,', '',$data);
- save_to_file($image);
- }
- function save_to_file($image){
- $fp = fopen($filename, 'w');
- fwrite($fp, $image);
- fclose($fp);
- }
Please note, the above solution can be used to do Web App pictureuploading, also you can convert the output of Canvas into picture. By doing so,you can use Canvas to provide image editing capability such as cropping,rendering and doodling, then you can savethe picture onto server using the edited picture.
Canvasdoodle
Drivenby HTML5, Is there really a big gap which cannot be jumped over between Web Appand Native App? I will answer this question on Baidu Developer Conference on 23rdMarch 2012.
References:
The MediaCapture API:http://www.w3.org/TR/media-capture-api/
Canvas:http://dev.w3.org/html5/2dcontext/
Source : http://blog.csdn.net/hfahe/article/details/7354912