我尝试使用简单的网络摄像头捕获上传,上传不起作用?

时间:2015-02-02 20:11:50

标签: javascript php html5 upload

更新:

这是整个代码,我几乎都复制并粘贴了。

<!DOCTYPE HTML>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script language="JavaScript" type="text/javascript"></script>

<style>

</style>
</head>
<body>
<script>
Webcam.set({
    width: 320,
    height: 240,
    dest_width: 640,
    dest_height: 480,
    image_format: 'jpeg',
    jpeg_quality: 90,
    force_flash: false
});
</script>
<div id="web_camera" style="width:320px; height:240px;"></div>
<div id="cam_result"></div>
 <script type="text/javascript" src="webcam.js"></script>
  <script language="JavaScript">
   document.addEventListener("DOMContentLoaded", function(event) {
        Webcam.set({
            width: 320,
            height: 240,
            image_format: 'jpeg',
            jpeg_quality: 90
        });
        Webcam.attach( '#web_camera' );
        function take_snapshot() {
            // take snapshot and get image data
            Webcam.snap( function(data_uri) {
                // display results in page
                document.getElementById('cam_results').innerHTML =
                    '<h2>Here is your image:</h2>' +
                    '<img src="'+data_uri+'"/>';
                                Webcam.upload( data_uri, 'upload.php', function(code, text) {
                                            // Upload complete!
                                            // 'code' will be the HTTP response code from the server, e.g. 200
                                            // 'text' will be the raw response content
                                });
            } );
        }
   });
  </script>
<a href="javascript:void(take_snapshot())">Take Snapshot</a>

</body>

我正在使用此链接

http://mycodingtricks.com/javascript/webcam-api/

这个看起来好多了但可能是同样的事情

http://www.html5rocks.com/en/tutorials/getusermedia/intro/

我关注的是data_uri,也是网址上传

所以摄像头工作,显示我的脸,无论如何,但我推这个

<a href="javascript:void(take_snapshot())">Take Snapshot</a>

没有任何反应。我看到左下方的小灰框说javascript:void(take_snapshot())我想知道我是否应该放一个参数......

可能有几个问题,我正在使用域映射,文件夹可能指向错误,或者可能是文件权限问题,我用www-data

这是建议的upload.php,或者由第一个链接

给出
<?php
    // be aware of file / directory permissions on your server
    move_uploaded_file($_FILES['webcam']['tmp_name'], '/tabdater/uploads/webcam'.md5(time()).rand(383,1000).'.jpg');
?>

我很感激任何帮助。

2 个答案:

答案 0 :(得分:5)

这应该是一个很好的起点。我希望这会对你有所帮助。

<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Cam Snap</title>
<script type="text/javascript" src="webcam.js"></script>
<script language="JavaScript">
function take_snapshot() {
    Webcam.snap(function(data_uri) {
    document.getElementById('results').innerHTML = '<img id="base64image" src="'+data_uri+'"/><button onclick="SaveSnap();">Save Snap</button>';
});
}
function ShowCam(){
Webcam.set({
width: 320,
height: 240,
image_format: 'jpeg',
jpeg_quality: 100
});
Webcam.attach('#my_camera');
}
function SaveSnap(){
    document.getElementById("loading").innerHTML="Saving, please wait...";
    var file =  document.getElementById("base64image").src;
    var formdata = new FormData();
    formdata.append("base64image", file);
    var ajax = new XMLHttpRequest();
    ajax.addEventListener("load", function(event) { uploadcomplete(event);}, false);
    ajax.open("POST", "upload.php");
    ajax.send(formdata);
}
function uploadcomplete(event){
    document.getElementById("loading").innerHTML="";
    var image_return=event.target.responseText;
    var showup=document.getElementById("uploaded").src=image_return;
}
window.onload= ShowCam;
</script>
<style type="text/css">
.container{display:inline-block;width:320px;}
#Cam{background:rgb(255,255,215);}#Prev{background:rgb(255,255,155);}#Saved{background:rgb(255,255,55);}
</style>
</head>
<body>
<div class="container" id="Cam"><b>Webcam Preview...</b>
    <div id="my_camera"></div><form><input type="button" value="Snap It" onClick="take_snapshot()"></form>
</div>
<div class="container" id="Prev">
    <b>Snap Preview...</b><div id="results"></div>
</div>
<div class="container" id="Saved">
    <b>Saved</b><span id="loading"></span><img id="uploaded" src=""/>
</div>
</body>
</html>

PHP(必须有上传目录) -

<?php
define('UPLOAD_DIR', 'uploads/');
$img = $_POST['base64image'];
$img = str_replace('data:image/jpeg;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>

归功于This Blog的php!

答案 1 :(得分:1)

它不起作用,因为您在take_snapshot()函数中包含了document.addEventListener("DOMContentLoaded", function(event) {函数。

请按照教程...

您的代码应如下所示:

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
<title>Javascript Webcam Demo - <MyCodingTricks/></title>
<link href="http://mycodingtricks.com/demo/style.css" rel="stylesheet"/>
</head>
<body>
	<h3>Demonstrates simple 320x240 capture &amp; display</h3>
	<div id="my_camera"></div>
	<!-- A button for taking snaps -->
	<form>
		<input type=button class="btn btn-success" value="Take Snapshot" onClick="take_snapshot()">
	</form>
	<div id="results" class="well">Your captured image will appear here...</div>
<script src="http://mycodingtricks.com/demo/script.js"></script>

	<!-- First, include the Webcam.js JavaScript Library -->
	<script type="text/javascript" src="webcam.min.js"></script>
	
	<!-- Configure a few settings and attach camera -->
	<script language="JavaScript">
		Webcam.set({
			width: 320,
			height: 240,
			image_format: 'jpeg',
			jpeg_quality: 90
		});
		Webcam.attach( '#my_camera' );
		function take_snapshot() {
			// take snapshot and get image data
			Webcam.snap( function(data_uri) {
				// display results in page
				document.getElementById('results').innerHTML = 
					'<h2>Here is your image:</h2>' + 
					'<img src="'+data_uri+'"/>';
                                Webcam.upload( data_uri, 'upload.php', function(code, text) {
                                            // Upload complete!
                                            // 'code' will be the HTTP response code from the server, e.g. 200
                                            // 'text' will be the raw response content
                                });
			} );
		}
	</script>
</body>
</html>
&#13;
&#13;
&#13;