Accessing Your Webcam in HTML5

The quicktime app can select a connected IOS device as a Camera. But Chrome (for example) does not see it among the selectable cameras… is there a way to access a connected ios device’s screen like quicktime does for ios screen recording to mac?

No, tethered devices showing up is dependent on whether the device registers itself as a camera the browser can detect. One day, maybe :stuck_out_tongue:

Can I use this to record a video and save it on my server?
If not what could I use?

Just noting here that I (foolishly, perhaps) tried to customize the initial code example by using jQuery, only to have it fail over and over and over. For some reason, you cannot simply select the video element like you normally would in jQuery, even if you try to grab it by ID.

Won’t Work: $("#videoElement");

WILL Work: $("#videoElement")[0];

That is so bizarre! When you console.log the first example, what element is printed to the screen

If I don’t use the array notation [0] and then console.log the result, no video shows in the browser (no errors, either), and Chrome displays this in the console:

Yeah! Based on your console output, the actual element itself is referenced by the 0. That’s so strange but glad you were able to make it work.

Hi. I am using angularjs to build a web application.
I will be accessing the webcam through an IP and port over the lan to video stream and take picture.
I am able to do it through the laptop webcam but stuck up in accessing the webcam through IP and port.
Can you pls guide me how to achive this?

Are you able to access the webcam via the IP to see the picture?

For example, see if you can do something like the following:

<img id="mywebcam" border="0" width="1920" height="1080" src="http://192.168.1.1:8080/video">

Hey…sorry to bother you.

I could get it done. Thanks.

But, I have the need to access the webcam in the network through IP and port.

Can it be done?

That’s my question to you - do you have an IP address that gets you access to the webcam? If the answer is “Yes”, then the HTML/CSS/JS side is doable. If the answer is no, then you’ll need to have an intermediary solution to get you that information.

My answer to your question is yes. I have access an IP address to access the webcam which is in the LAN. I am developing a weighbridge system where the user will be sitting in the control room and the camera is setup in the weighbridge console. The user will instruct the driver to move the truck back and forth to get the right image of the container, the vehicle number and take picture to be recorded throug a UI. Let me know how that can be possible please.
TiA

When you paste the IP address in your browser’s address bar, do you see an image?

Kirupa - this was a lifesaver when testing HTML output for my webcam -
There is also a great test page for all Input / Output Audio / video devices in the WebRTC Samples.

1 Like

What to add in this html code:

<html data-kantu="1"><script src="chrome-extension://ljdobmomdgdljniojadhoplhkpialdid/page/prompt.js"></script><script src="chrome-extension://ljdobmomdgdljniojadhoplhkpialdid/page/runScript.js"></script><head>
<meta charset="utf-8">
<meta content="Display Webcam Stream" name="title">
<title>Display Webcam Stream</title>
<style>
    body {
      margin: 30px;
    }

    h1 {
      font-family: sans-serif;
      color: #666;
    }

    #container {
      width: 500px;
      height: 375px;
      border: 10px #333 solid;
    }

    #videoElement {
      width: 500px;
      height: 375px;
      background-color: #666;
    }
    
    button {
      margin-top: 20px;
      font-size: 12px;
      font-weight: bold;
      padding: 5px;
      background-color: white;
      border: 5px solid black;
    }

    button:hover {
      background-color: yellow;
    }

    button:active {
      background-color: yellowgreen;
    }
  </style>
</head>
<body>
<h1>Stop Webcam Stream</h1>
<div id="container">
<video autoplay="true" id="videoElement">
</video>
</div>
<button id="stop">Stop Video</button>
<script>
    var video = document.querySelector("#videoElement");
    var stopVideo = document.querySelector("#stop");

    if (navigator.mediaDevices.getUserMedia) {
      navigator.mediaDevices.getUserMedia({ video: true })
        .then(function (stream) {
          video.srcObject = stream;
        })
        .catch(function (err0r) {
          console.log("Something went wrong!");
        });
    }

    stopVideo.addEventListener("click", stop, false);

    function stop(e) {
      var stream = video.srcObject;
      var tracks = stream.getTracks();

      for (var i = 0; i < tracks.length; i++) {
        var track = tracks[i];
        track.stop();
      }

      video.srcObject = null;
    }
  </script>

</body></html>

to have another button that reactivates the webcam after having stopped it ?
thanks

Hi @System0 - welcome to the forums! You will need to add another button similar to the Stop Video one:

<button id="play">Play Video</button>

In the event handler for this button, you will have this code:

    if (navigator.mediaDevices.getUserMedia) {
      navigator.mediaDevices.getUserMedia({ video: true })
        .then(function (stream) {
          video.srcObject = stream;
        })
        .catch(function (err0r) {
          console.log("Something went wrong!");
        });
    }

:slight_smile:

ok .A new button appears.
but i can only once time reactivate (by button play) the video and not that several times.
I don’t know how to change this code to do that

Are you trying to have a Play and Stop button that someone can click multiple times and have the video start/stop accordingly? Do you want the video to play when the page loads?

Are you trying to have a Play and Stop button that someone can click multiple times and have the video start/stop accordingly?
yes

Do you want the video to play when the page loads?
a) yes
b) no