Pregunta

I am looking to create an ASP.Net website where the user can capture an image from there web camera and save it to a database on my server. I have tried using TWAIN but cannot seem to find any new cameras that support this. Same when trying to use Silverlight and WIA. Has anyone had any success in doing this?

The client computer will have whatever web camera I recommend so if you know of a model that works, please share. I have tried both Microsoft LifeCam and Logitech with no luck.

If you have done this or now how I would really appreciate some help. Thanks for you time.

¿Fue útil?

Solución 3

I was able to accomplish what I wanted by having the user install Google Chrome Frame on there computer and then using the canvas tag to get the image. Works well here's some sample code:

    <div>

        <p id="status" style="color:red">
            <noscript>JavaScript is disabled.</noscript>
        </p>


        <table>
            <tr>
                <td>
                    <input id="btnTakePicture" type="button" value="Take Picture"; />
                </td>
                <td>
                    <input id="btnSave" type="button" value="Save Picture"; />
                </td>
            </tr>
        </table>


        <asp:Button ID="btnSavePicture" runat="server" Text="HiddenSave" OnClick="btnSavePicture_Click" CssClass="hiddencol"  />

        <asp:Panel ID="pnlHide" runat="server" style="display:none" >    
            <textarea id="eventdata" rows="18" cols="1" style="width: 100%" runat="server"></textarea>
        </asp:Panel>

        <script type="text/javascript">

                navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
                navigator.mozGetUserMedia || navigator.msGetUserMedia;

                var webcam = (function () {


                    var video = document.createElement('video'),
                    photo = document.createElement('canvas');

                    function play() {

                        if (navigator.getUserMedia) {

                            navigator.getUserMedia({ video: true, audio: true, toString: function () { return "video,audio"; } }, onSuccess, onError);

                        } else {

                            changeStatus('getUserMedia is not supported in this browser.', true);
                        }

                    }

                    function onSuccess(stream) {

                        var source;

                        if (window.webkitURL) {

                            source = window.webkitURL.createObjectURL(stream);

                        } else {

                            source = stream; // Opera and Firefox
                        }


                        video.autoplay = true;

                        video.src = source;

                        changeStatus('Connected.', false);

                    }

                    function onError() {

                        changeStatus('Please accept the getUserMedia permissions! Refresh to try again.', true);

                    }

                    function changeStatus(msg, error) {
                        var status = document.getElementById('status');
                        status.innerHTML = msg;
                        status.style.color = (error) ? 'red' : 'green';
                    }


                    // allow the user to take a screenshot
                    function setupPhotoBooth() {
                        //var takeButton = document.createElement('button');
                        var takeButton = document.getElementById('btnTakePicture');
                        //takeButton.innerText = 'Take Picture';
                        takeButton.addEventListener('click', takePhoto, true);
                        //document.body.appendChild(takeButton);

                        //var saveButton = document.createElement('button');
                        var saveButton = document.getElementById('btnSave');
                        //saveButton.id = 'btnSave';
                        //saveButton.innerText = 'Save Picture';
                        saveButton.disabled = true;
                        saveButton.addEventListener('click', savePhoto, true);
                        //document.body.appendChild(saveButton);

                    }

                    function takePhoto() {

                        // set our canvas to the same size as our video
                        photo.width = video.width;
                        photo.height = video.height;

                        var context = photo.getContext('2d');
                        context.drawImage(video, 0, 0, photo.width, photo.height);

                        // allow us to save
                        var saveButton = document.getElementById('btnSave');
                        saveButton.disabled = false;

                        var data = photo.toDataURL("image/png");

                        data = data.replace("image/png", "");

                        document.getElementById("<%= eventdata.ClientID %>").value = data;

                    }

                    function savePhoto() {
                        //                        var data = photo.toDataURL("image/png");

                        //                        data = data.replace("image/png", "image/octet-stream");

                        //                        document.getElementById("<%= eventdata.ClientID %>").value = data;
                        //                        document.location.href = data;

                        SendBack();
                    }

                    return {
                        init: function () {

                            changeStatus('Please accept the permissions dialog.', true);

                            video.width = 320;
                            video.height = 240;

                            document.body.appendChild(video);
                            document.body.appendChild(photo);

                            navigator.getUserMedia || (navigator.getUserMedia = navigator.mozGetUserMedia || navigator.webkitGetUserMedia || navigator.msGetUserMedia);

                            play();
                            setupPhotoBooth();

                        } ()

                    }


                })();

            function SendBack() {
                var btn = document.getElementById("<%= btnSavePicture.ClientID %>");
                btn.click();
            }

    </script>

    </div>

Otros consejos

If you're targeting the newer browsers (those that support the WebRTC getUserMedia method), then Photobooth.js may be an option for you.

Quoted from WebMonkey: 'The latest WebRTC hotness to catch our eye is developer Wolfram Hempel’s Photobooth.js, a JavaScript library for working with a device’s camera.'

http://www.webmonkey.com/2012/12/add-an-html5-webcam-to-your-site-with-photobooth-js/

And the lib itself:

http://wolframhempel.com/2012/12/02/photobooth-js/

Hope it works for you!

asp.net is a server side technology and has no ability to connect to a client side webcam. Your browser is highly sand-boxed and is unlikely to allow access to a user's webcam.

Consider building a Flash or Silverlight component to access the webcam.

The exception to this is that on many mobile platforms, the browser can access the camera and picture store via the <input type="file" /> tag. This has been the case for a while on Android and is now an option with Safari v6. I don't know of any desktop browsers that allow direct access to an attached webcam.

An award workaround would be for the user to take pictures and then access them via file upload.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top