Codeigniter 上的网络摄像头 js 集成

Webcam js integration on Codeigniter

我尝试研究并阅读了很多关于这个问题的文章

我已经红了一些,但讨论似乎还没有解决方案就结束了

所以问题是我无法在我的 CI 应用程序上显示摄像头,但是当我只复制确切的代码并将其粘贴到我的本地主机而不使用任何框架工作时它就可以工作了。

这是我的 CI 代码

    <script type="text/javascript" src="<?=base_url('assets/js/cam/webcam.js')?>"></script>
        <script language="JavaScript">
                document.write( webcam.get_html(440, 240) );
        </script>
        <form>
        <br />

            <input type=button value="Configure settings" onClick="webcam.configure()" class="shiva">
            &nbsp;&nbsp;
            <input type=button value="snap" onClick="take_snapshot()" class="shiva">
        </form>


<script  type="text/javascript">
    webcam.set_api_url( "<?=base_url('assets/js/cam/handleimage.php')?>");
        webcam.set_quality( 90 ); // JPEG quality (1 - 100)
        webcam.set_shutter_sound( true, "<?=base_url('assets/js/cam/shutter.mp3')?>" ); // play shutter click sound
        webcam.set_hook( 'onComplete', 'my_completion_handler' );
        function take_snapshot(){
            // take snapshot and upload to server
            document.getElementById('img').innerHTML = '<h1>Uploading...</h1>';

            webcam.snap();
        }

        function my_completion_handler(msg) {
            // extract URL out of PHP output
            if (msg.match(/(http\:\/\/\S+)/)) {
                // show JPEG image in page

                document.getElementById('img').innerHTML ='<h3>Upload Successfuly done</h3>'+msg;

                document.getElementById('img').innerHTML ="<img src="+msg+" class=\"img\">";


                // reset camera for another shot
                webcam.reset();
            }
            else {alert("Error occured we are trying to fix now: " + msg); }
        }
    </script>

我使用了这个演示包,如果我只是将它解压到本地主机,它运行良好。 https://app.box.com/s/nkhgrj73fvutaj6dfspf

但是当我尝试将其集成到 CI 时,相机 windows 显示空白。当我尝试使用网络摄像头 js 函数时,例如 webcam.configure() .. 它说电影尚未加载

朱尼

您无法通过简单的复制粘贴在 codeigniter 上实现它。错误 It says the Movie is not loaded yet 背后的原因是 webcam.swf 未加载。实际上 flash 文件是网络摄像头功能背后的重要内容。

创建新控制器application\controllers\camera.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Camera extends CI_Controller {

        public function index(){
            $this->load->helper('url');
            $this->load->view("camera");

        }


        public function saveImage(){

            /*write your own code to save to your database*/        
            $newname = "some_name.png"; 
            file_put_contents( $newname, file_get_contents('php://input') );

        }
    }

?>

创建新视图application\views\camera.php

<style type="text/css">
    body {
        margin: 0;
        padding: 0;
    }
    
    .img {
        background: #ffffff;
        padding: 12px;
        border: 1px solid #999999;
    }
    
    .shiva {
        -moz-user-select: none;
        background: #2A49A5;
        border: 1px solid #082783;
        box-shadow: 0 1px #4C6BC7 inset;
        color: white;
        padding: 3px 5px;
        text-decoration: none;
        text-shadow: 0 -1px 0 #082783;
        font: 12px Verdana, sans-serif;
    }
</style>
<html>

<body style="background-color:#dfe3ee;">
    <div id="outer" style="margin:0px; width:100%; height:90px;background-color:#3B5998;">
    </div>
    <div id="main" style="height:800px; width:100%">
        <div id="content" style="float:left; width:500px; margin-left:50px; margin-top:20px;" align="center">

            <script type="text/javascript" src="<?php echo base_url();?>assets/js/cam/webcam.js"></script>
            <script language="JavaScript">
                document.write(webcam.get_html(440, 240));
            </script>
            <form>
                <br />
                <input type=button value="Configure settings" onClick="webcam.configure()" class="shiva"> &nbsp;&nbsp;
                <input type=button value="snap" onClick="take_snapshot()" class="shiva">
            </form>
        </div>

        <script type="text/javascript">
            webcam.set_api_url('<?php echo base_url();?>camera/saveImage');
            webcam.set_quality(90);
            webcam.set_shutter_sound(true);
            webcam.set_hook('onComplete', 'my_completion_handler');

            function take_snapshot() {
                document.getElementById('img').innerHTML = '<h1>Uploading...</h1>';
                webcam.snap();
            }

            function my_completion_handler(msg) {
                if (msg.match(/(http\:\/\/\S+)/)) {
                    document.getElementById('img').innerHTML = '<h3>Upload Successfuly done</h3>' + msg;

                    document.getElementById('img').innerHTML = "<img src=" + msg + " class=\"img\">";
                    webcam.reset();
                } else {
                    alert("Error occured we are trying to fix now: " + msg);
                }
            }
        </script>

        <div id="img" style=" height:800px; width:500px; float:left; margin-left:40px; margin-top:20px;">
        </div>
    </div>
</body>

</html>

现在将webcam.js,shutter.mp3,webcam.swf复制到assets/js/cam。在 webcam.js

中做 2 处小改动

line number 27 : swf_url: 'webcam.swf'

line number 28 : shutter_url: 'shutter.mp3'

改为

line number 27 : swf_url: 'assets/js/cam/webcam.swf'

line number 28 : shutter_url: 'assets/js/cam/shutter.mp3'

来自 Tintu 先生的回答 添加了一些修改

您无法通过简单的复制粘贴在 codeigniter 上实现它。错误背后的原因 它说电影尚未加载 webcam.swf 未加载。实际上,该 Flash 文件是网络摄像头功能背后的重要内容。

创建新控制器application\controllers\camera.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Camera extends CI_Controller {

        public function index(){
            $this->load->helper('url');
            $this->load->view("camera");

        }


        public function saveImage(){

            /*write your own code to save to your database*/        
            $newname = "some_name.png"; 
            file_put_contents( $newname, file_get_contents('php://input') );

        }
    }

?>

创建新视图application\views\camera.php

<style type="text/css">
    body {
        margin: 0;
        padding: 0;
    }

    .img {
        background: #ffffff;
        padding: 12px;
        border: 1px solid #999999;
    }

    .shiva {
        -moz-user-select: none;
        background: #2A49A5;
        border: 1px solid #082783;
        box-shadow: 0 1px #4C6BC7 inset;
        color: white;
        padding: 3px 5px;
        text-decoration: none;
        text-shadow: 0 -1px 0 #082783;
        font: 12px Verdana, sans-serif;
    }
</style>
<html>

<body style="background-color:#dfe3ee;">
    <div id="outer" style="margin:0px; width:100%; height:90px;background-color:#3B5998;">
    </div>
    <div id="main" style="height:800px; width:100%">
        <div id="content" style="float:left; width:500px; margin-left:50px; margin-top:20px;" align="center">

            <script type="text/javascript" src="<?php echo base_url();?>assets/js/cam/webcam.js"></script>
            <script language="JavaScript">
                document.write(webcam.get_html(440, 240));
            </script>
            <form>
                <br />
                <input type=button value="Configure settings" onClick="webcam.configure()" class="shiva"> &nbsp;&nbsp;
                <input type=button value="snap" onClick="take_snapshot()" class="shiva">
            </form>
        </div>

        <script type="text/javascript">
            webcam.set_api_url('<?php echo base_url();?>camera/saveImage');
            webcam.set_quality(90);
            webcam.set_shutter_sound(true);
            webcam.set_hook('onComplete', 'my_completion_handler');

            function take_snapshot() {
                document.getElementById('img').innerHTML = '<h1>Uploading...</h1>';
                webcam.snap();
            }

            function my_completion_handler(msg) {
                if (msg.match(/(http\:\/\/\S+)/)) {
                    document.getElementById('img').innerHTML = '<h3>Upload Successfuly done</h3>' + msg;

                    document.getElementById('img').innerHTML = "<img src=" + msg + " class=\"img\">";
                    webcam.reset();
                } else {
                    alert("Error occured we are trying to fix now: " + msg);
                }
            }
        </script>

        <div id="img" style=" height:800px; width:500px; float:left; margin-left:40px; margin-top:20px;">
        </div>
    </div>
</body>

</html>

现在将 webcam.js,shutter.mp3,webcam.swf 复制到 assets/js/cam。在 webcam.js

中做 2 处小改动
line number 27 : swf_url: 'webcam.swf'

line number 28 : shutter_url: 'shutter.mp3'

改为

line number 27 : swf_url: '../assets/js/cam/webcam.swf'

line number 28 : shutter_url: '../assets/js/cam/shutter.mp3'

注意:这个 Url 很重要并且会有所不同。根据我的经验,通过 google chrome 检查 .swf url exstince 比通过 firefox 更好。 . 添加'../' 上一级文件夹让你在根应用程序文件夹中访问资产文件夹

干杯

感谢 Tintu Raju 先生