dsgw8802 2016-03-31 19:02
浏览 110

如何在PHP中正确显示CCTV的MJPEG快照

I have a IP CCTV camera which support MJPEG. I would like to display image in HTML (from PHP script) together with other data.

I know that I can get a screenshot from the camera using:

http://username:password@<servername>/Streaming/channels/1/picture

however when I am using this simple html code image is not always displayed:

<html>
  <body>
    <IMG id="myImage" SRC='http://username:password@192.168.0.20/Streaming/channels/1/picture'>
  </body>
</html>

On Microsoft Edge, it is not possible even to login to camera. On Firefox works fine. On Chrome camera is logged however image is not displayed.

So the question is how I can get image in HTML or better in PHP to display it on the page. I prefer to use PHP because I want to add more data to page, like temperature etc.

Also will be nice to refresh the image, but this can be done in AJAX later.

  • 写回答

2条回答 默认 最新

  • drpiqlzrh62917192 2016-03-31 19:09
    关注

    Use a second PHP script to retrieve the image and point the <img> tag to that. It's possibly because the page you're displaying the image on uses HTTPS while the camera only supports HTTP. Proxying through PHP will allow it all to appear to come from the same source and PHP will handle the HTTP authentication, which will avoid any of your browser compatibility problems.

    <IMG id="myImage" SRC='currentimage.php'>

    currentimage.php:

    <?php
    $crl = curl_init('http://<servername>/Streaming/channels/1/picture');
    curl_setopt($crl, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
    curl_setopt($crl, CURLOPT_USERPWD, 'username:password');
    curl_setopt($crl, CURLOPT_RETURNTRANSFER, TRUE);
    $contents = curl_exec($crl);
    curl_close($crl);
    
    header("Content-Type: image/jpeg");
    echo $contents;
    ?>
    

    To save on server load and allow it to handle more traffic, you could also cache the last-retrieved image to a file for 5-10 seconds, but that's a separate problem to solve.

    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来