1. Video streaming
HTML5 The Media Capture API provides programmable access to the camera. Users can directly use getUserMedia to obtain the video stream provided by the camera. What we need to do is add an HTML5 Video tag and use the video obtained from the camera as the input source of this tag (please note that currently only Chrome and Opera support getUserMedia).
Video streaming
2. Take photos
For the camera function, we use HTML5 Canvas to capture the content of the Video tag in real time. The Video element can be used as the input of the Canvas image, which is great. The main code is as follows:
3. Image acquisition
Next we are going to get image data from Canvas. The core idea is to use canvas's toDataURL to convert the Canvas data into a base64-bit encoded PNG image, similar to the format of "data:image/png;base64,xxxxx".
Because the real image data is the part after the comma in base64 encoding, the image data processed by our actual server should be this part. We can obtain it in two ways.
The first method is to intercept the string after 22 bits at the front end as image data, for example:
If you want to get the size of the image before uploading, you can use:
The second method is to use the background language to intercept the string after 22 digits after obtaining the transmitted data at the back end. For example, in PHP:
4. Image upload
On the front end, you can use Ajax to upload the image data obtained above to the background script. For example when using jQuery:
Please note that the above solution can not only be used for Web App photo uploading, but you can also implement the function of converting Canvas output into image uploading. In this way, you can use Canvas to provide users with image editing, such as cropping, coloring, and graffiti drawing board functions, and then save the user's edited images to the server.