草庐IT

php - 在 php 中通过网络摄像头上传图像

coder 2024-04-21 原文

<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
    <input type="text" name="myname" id="myname">
    <input type="submit" name="send" id="send">
</form>

<script type="text/javascript" src="webcam.js"></script>
<script language="JavaScript">
        document.write( webcam.get_html(320, 240) );
</script>
<form>
        <input type=button value="Configure..." onClick="webcam.configure()">
        &nbsp;&nbsp;
        <input type=button name="img" value="Take Snapshot" onClick="take_snapshot()">
    </form>

<script language="JavaScript">
        document.write( webcam.get_html(320, 240) );
</script>

<script language="JavaScript">
    webcam.set_api_url( 'test.php' );
        webcam.set_quality( 90 ); // JPEG quality (1 - 100)
        webcam.set_shutter_sound( true ); // play shutter click sound
        webcam.set_hook( 'onComplete', 'my_completion_handler' );

        function take_snapshot(){
            // take snapshot and upload to server

            document.getElementById('upload_results').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('upload_results').innerHTML ='<h1>Upload Successful!</h1>';
                // reset camera for another shot
                webcam.reset();
            }
            else {alert("PHP Error: " + msg);
        }
    </script>
<div id="upload_results" style="background-color:#eee;"></div>

我正在使用 PHP 语言工作。我正在尝试从网络摄像头上传图像并想将其保存在我的 PC 上。网络摄像头工作正常;它正在打开网络摄像头,但我点击了 take_snapshot,它什么也没做。

我在理解 JavaScript 部分以及单击 take_snapshot 按钮时发生的情况时遇到了问题。

我的摄像头.js

/* JPEGCam v1.0.9 */
/* Webcam library for capturing JPEG images and submitting to a server */
/* Copyright (c) 2008 - 2009 Joseph Huckaby <jhuckaby@goldcartridge.com> */
/* Licensed under the GNU Lesser Public License */
/* http://www.gnu.org/licenses/lgpl.html */

/* Usage:
    <script language="JavaScript">
        document.write( webcam.get_html(320, 240) );
        webcam.set_api_url( 'test.php' );
        webcam.set_hook( 'onComplete', 'my_callback_function' );
        function my_callback_function(response) {
            alert("Success! PHP returned: " + response);
        }
    </script>
    <a href="javascript:void(webcam.snap())">Take Snapshot</a>
*/

// Everything is under a 'webcam' Namespace
window.webcam = {
    version: '1.0.9',

    // globals
    ie: !!navigator.userAgent.match(/MSIE/),
    protocol: location.protocol.match(/https/i) ? 'https' : 'http',
    callback: null, // user callback for completed uploads
    swf_url: 'webcam.swf', // URI to webcam.swf movie (defaults to cwd)
    shutter_url: 'shutter.mp3', // URI to shutter.mp3 sound
    api_url: '', // URL to upload script
    loaded: false, // true when webcam movie finishes loading
    quality: 90, // JPEG quality (1 - 100)
    shutter_sound: true, // shutter sound effect on/off
    stealth: false, // stealth mode (do not freeze image upon capture)
    hooks: {
        onLoad: null,
        onComplete: null,
        onError: null
    }, // callback hook functions

    set_hook: function(name, callback) {
        // set callback hook
        // supported hooks: onLoad, onComplete, onError
        if (typeof(this.hooks[name]) == 'undefined')
            return alert("Hook type not supported: " + name);

        this.hooks[name] = callback;
    },

    fire_hook: function(name, value) {
        // fire hook callback, passing optional value to it
        if (this.hooks[name]) {
            if (typeof(this.hooks[name]) == 'function') {
                // callback is function reference, call directly
                this.hooks[name](value);
            }
            else if (typeof(this.hooks[name]) == 'array') {
                // callback is PHP-style object instance method
                this.hooks[name][0][this.hooks[name][1]](value);
            }
            else if (window[this.hooks[name]]) {
                // callback is global function name
                window[ this.hooks[name] ](value);
            }
            return true;
        }
        return false; // no hook defined
    },

    set_api_url: function(url) {
        // set location of upload API script
        this.api_url = url;
    },

    set_swf_url: function(url) {
        // set location of SWF movie (defaults to webcam.swf in cwd)
        this.swf_url = url;
    },

    get_html: function(width, height, server_width, server_height) {
        // Return HTML for embedding webcam capture movie
        // Specify pixel width and height (640x480, 320x240, etc.)
        // Server width and height are optional, and default to movie width/height
        if (!server_width) server_width = width;
        if (!server_height) server_height = height;

        var html = '';
        var flashvars = 'shutter_enabled=' + (this.shutter_sound ? 1 : 0) + 
            '&shutter_url=' + escape(this.shutter_url) + 
            '&width=' + width + 
            '&height=' + height + 
            '&server_width=' + server_width + 
            '&server_height=' + server_height;

        if (this.ie) {
            html += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="'+this.protocol+'://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+width+'" height="'+height+'" id="webcam_movie" align="middle"><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="false" /><param name="movie" value="'+this.swf_url+'" /><param name="loop" value="false" /><param name="menu" value="false" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><param name="flashvars" value="'+flashvars+'"/></object>';
        }
        else {
            html += '<embed id="webcam_movie" src="'+this.swf_url+'" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="'+width+'" height="'+height+'" name="webcam_movie" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="'+flashvars+'" />';
        }

        this.loaded = false;
        return html;
    },

    get_movie: function() {
        // get reference to movie object/embed in DOM
        if (!this.loaded) return alert("ERROR: Movie is not loaded yet");
        var movie = document.getElementById('webcam_movie');
        if (!movie) alert("ERROR: Cannot locate movie 'webcam_movie' in DOM");
        return movie;
    },

    set_stealth: function(stealth) {
        // set or disable stealth mode
        this.stealth = stealth;
    },

    snap: function(url, callback, stealth) {
        // take snapshot and send to server
        // specify fully-qualified URL to server API script
        // and callback function (string or function object)
        if (callback) this.set_hook('onComplete', callback);
        if (url) this.set_api_url(url);
        if (typeof(stealth) != 'undefined') this.set_stealth( stealth );

        this.get_movie()._snap( this.api_url, this.quality, this.shutter_sound ? 1 : 0, this.stealth ? 1 : 0 );
    },

    freeze: function() {
        // freeze webcam image (capture but do not upload)
        this.get_movie()._snap('', this.quality, this.shutter_sound ? 1 : 0, 0 );
    },

    upload: function(url, callback) {
        // upload image to server after taking snapshot
        // specify fully-qualified URL to server API script
        // and callback function (string or function object)
        if (callback) this.set_hook('onComplete', callback);
        if (url) this.set_api_url(url);

        this.get_movie()._upload( this.api_url );
    },

    reset: function() {
        // reset movie after taking snapshot
        this.get_movie()._reset();
    },

    configure: function(panel) {
        // open flash configuration panel -- specify tab name:
        // "camera", "privacy", "default", "localStorage", "microphone", "settingsManager"
        if (!panel) panel = "camera";
        this.get_movie()._configure(panel);
    },

    set_quality: function(new_quality) {
        // set the JPEG quality (1 - 100)
        // default is 90
        this.quality = new_quality;
    },

    set_shutter_sound: function(enabled, url) {
        // enable or disable the shutter sound effect
        // defaults to enabled
        this.shutter_sound = enabled;
        this.shutter_url = url ? url : 'shutter.mp3';
    },

    flash_notify: function(type, msg) {
        // receive notification from flash about event
        switch (type) {
            case 'flashLoadComplete':
                // movie loaded successfully
                this.loaded = true;
                this.fire_hook('onLoad');
                break;

            case 'error':
                // HTTP POST error most likely
                if (!this.fire_hook('onError', msg)) {
                    alert("JPEGCam Flash Error: " + msg);
                }
                break;

            case 'success':
                // upload complete, execute user callback function
                // and pass raw API script results to function
                this.fire_hook('onComplete', msg.toString());
                break;

            default:
                // catch-all, just in case
                alert("jpegcam flash_notify: " + type + ": " + msg);
                break;
        }
    }
};

最佳答案

为了运行 PHP 脚本,您必须使用 PHP 设置的 Web 服务器。 一个常见的设置是 Linux/Apache/MySQL/PHP,也称为 LAMP .没有这种设置的 PHP 脚本将无法运行,只会在 Web 浏览器中显示为文本。

关于php - 在 php 中通过网络摄像头上传图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18568491/

有关php - 在 php 中通过网络摄像头上传图像的更多相关文章

  1. Ruby Readline 在向上箭头上使控制台崩溃 - 2

    当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby​​安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少

  2. ruby - 我可以使用 aws-sdk-ruby 在 AWS S3 上使用事务性文件删除/上传吗? - 2

    我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的

  3. ruby - 用 Ruby 编写一个简单的网络服务器 - 2

    我想在Ruby中创建一个用于开发目的的极其简单的Web服务器(不,不想使用现成的解决方案)。代码如下:#!/usr/bin/rubyrequire'socket'server=TCPServer.new('127.0.0.1',8080)whileconnection=server.acceptheaders=[]length=0whileline=connection.getsheaders想法是从命令行运行这个脚本,提供另一个脚本,它将在其标准输入上获取请求,并在其标准输出上返回完整的响应。到目前为止一切顺利,但事实证明这真的很脆弱,因为它在第二个请求上中断并出现错误:/usr/b

  4. ruby-on-rails - 添加回形针新样式不影响旧上传的图像 - 2

    我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司

  5. ruby-on-rails - 有没有办法为 CarrierWave/Fog 设置上传进度指示器? - 2

    我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r

  6. 网络编程套接字 - 2

    网络编程套接字网络编程基础知识理解源`IP`地址和目的`IP`地址理解源MAC地址和目的MAC地址认识端口号理解端口号和进程ID理解源端口号和目的端口号认识`TCP`协议认识`UDP`协议网络字节序socket编程接口`sockaddr``UDP`网络程序服务器端代码逻辑:需要用到的接口服务器端代码`udp`客户端代码逻辑`udp`客户端代码`TCP`网络程序服务器代码逻辑多个版本服务器单进程版本多进程版本多线程版本线程池版本服务器端代码客户端代码逻辑客户端代码TCP协议通讯流程TCP协议的客户端/服务器程序流程三次握手(建立连接)数据传输四次挥手(断开连接)TCP和UDP对比网络编程基础知识

  7. STM32读取串口传感器数据(颗粒物传感器,主动上传) - 2

    文章目录1.开发板选择*用到的资源2.串口通信(个人理解)3.代码分析(注释比较详细)1.主函数2.串口1配置3.串口2配置以及中断函数4.注意问题5.源码链接1.开发板选择我用的是STM32F103RCT6的板子,不过代码大概在F103系列的板子上都可以运行,我试过在野火103的霸道板上也可以,主要看一下串口对应的引脚一不一样就行了,不一样的就更改一下。*用到的资源keil5软件这里用到了两个串口资源,采集数据一个,串口通信一个,板子对应引脚如下:串口1,TX:PA9,RX:PA10串口2,TX:PA2,RX:PA32.串口通信(个人理解)我就从串口采集传感器数据这个过程说一下我自己的理解,

  8. ruby-on-rails - 在 Ruby (on Rails) 中使用 imgur API 获取图像 - 2

    我正在尝试使用Ruby2.0.0和Rails4.0.0提供的API从imgur中提取图像。我已尝试按照Ruby2.0.0文档中列出的各种方式构建http请求,但均无济于事。代码如下:require'net/http'require'net/https'defimgurheaders={"Authorization"=>"Client-ID"+my_client_id}path="/3/gallery/image/#{img_id}.json"uri=URI("https://api.imgur.com"+path)request,data=Net::HTTP::Get.new(path

  9. python ffmpeg 使用 pyav 转换 一组图像 到 视频 - 2

    2022/8/4更新支持加入水印水印必须包含透明图像,并且水印图像大小要等于原图像的大小pythonconvert_image_to_video.py-f30-mwatermark.pngim_dirout.mkv2022/6/21更新让命令行参数更加易用新的命令行使用方法pythonconvert_image_to_video.py-f30im_dirout.mkvFFMPEG命令行转换一组JPG图像到视频时,是将这组图像视为MJPG流。我需要转换一组PNG图像到视频,FFMPEG就不认了。pyav内置了ffmpeg库,不需要系统带有ffmpeg工具因此我使用ffmpeg的python包装p

  10. ruby - 是否有将图像文件转换为 ASCII 艺术的命令行程序或库? - 2

    有这样的事吗?我想在Ruby程序中使用它。 最佳答案 试试这个http://csl.sublevel3.org/jp2a/此外,Imagemagick可能还有一些东西 关于ruby-是否有将图像文件转换为ASCII艺术的命令行程序或库?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/6510445/

随机推荐