How to access webcam and photo with HTML5 and Javascript?

Asked

Viewed 9,724 times

4

Does anyone have an example showing how to implement this application and then save the image?

  • I don’t know if it is duplicated, but it is certainly related: http://answall.com/questions/23596/howto capture a photo-photo-atrav%C3%A9s-da-webcam-do-Usu%C3%A1rio-e-enviar-via-post

1 answer

4

You can work with the Scriptcam, a Jquery plugin for webcam use.

Follow code example to take photos with webcam:

<!DOCTYPE html>
<html>
    <head>
        <script language="JavaScript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script language="JavaScript" src="//ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
        <script language="JavaScript" src="scriptcam.js"></script>
        <script language="JavaScript"> 
            $(document).ready(function() {
                $("#webcam").scriptcam({
                    showMicrophoneErrors:false,
                    onError:onError,
                    cornerRadius:20,
                    disableHardwareAcceleration:1,
                    cornerColor:'e3e5e2',
                    onWebcamReady:onWebcamReady,
                    uploadImage:'upload.gif',
                    onPictureAsBase64:base64_tofield_and_image
                });
            });
            function base64_tofield() {
                $('#formfield').val($.scriptcam.getFrameAsBase64());
            };
            function base64_toimage() {
                $('#image').attr("src","data:image/png;base64,"+$.scriptcam.getFrameAsBase64());
            };
            function base64_tofield_and_image(b64) {
                $('#formfield').val(b64);
                $('#image').attr("src","data:image/png;base64,"+b64);
            };
            function changeCamera() {
                $.scriptcam.changeCamera($('#cameraNames').val());
            }
            function onError(errorId,errorMsg) {
                $( "#btn1" ).attr( "disabled", true );
                $( "#btn2" ).attr( "disabled", true );
                alert(errorMsg);
            }           
            function onWebcamReady(cameraNames,camera,microphoneNames,microphone,volume) {
                $.each(cameraNames, function(index, text) {
                    $('#cameraNames').append( $('<option></option>').val(index).html(text) )
                }); 
                $('#cameraNames').val(camera);
            }
        </script> 
    </head>
    <body>
        <div style="width:330px;float:left;">
            <div id="webcam">
            </div>
            <div style="margin:5px;">
                <img src="webcamlogo.png" style="vertical-align:text-top"/>
                <select id="cameraNames" size="1" onChange="changeCamera()" style="width:245px;font-size:10px;height:25px;">
                </select>
            </div>
        </div>
        <div style="width:135px;float:left;">
            <p><button class="btn btn-small" id="btn1" onclick="base64_tofield()">Snapshot to form</button></p>
            <p><button class="btn btn-small" id="btn2" onclick="base64_toimage()">Snapshot to image</button></p>
        </div>
        <div style="width:200px;float:left;">
            <p><textarea id="formfield" style="width:190px;height:70px;"></textarea></p>
            <p><img id="image" style="width:200px;height:153px;"/></p>
        </div>
    </body>
</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.