使用 PHP 和 JavaScript 从网络摄像头捕获图像并保存在文件夹中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30389392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Capture image from webcam and save in folder using PHP and JavaScript
提问by user4927626
Kindly checkout code given below I want to save image in folder in JPG format using PHP and AJAX. Kindly help me I am not able to do it.
请签出下面给出的代码我想使用 PHP 和 AJAX 以 JPG 格式将图像保存在文件夹中。请帮助我,我无法做到。
function base64_toimage()
{
$('#image').attr("src","data:image/png;base64,"+$.scriptcam.getFrameAsBase64());
};
function base64_tofield_and_image(b64)
{
$('#formfield').val(b64);
$('#image').attr("src","data:image/png;base64,"+b64);
};
function changeCamera()
{
$.scriptcam.changeCamera($('#cameraNames').val());
}
function onError(errorId,errorMsg) {
$( "#btn1" ).attr( "disabled", true );
$( "#btn2" ).attr( "disabled", true );
alert(errorMsg);
}
function onWebcamReady(cameraNames,camera,microphoneNames,microphone,volume) {
$.each(cameraNames, function(index, text) {
$('#cameraNames').append( $('<option></option>').val(index).html(text) )
});
$('#cameraNames').val(camera);
}
</script>
<br />
<div id="webcam" ></div>
<div style="width:250px;float:left;" ><img src="webcamlogo.png" alt="" style="vertical-align:text-top" />
<select name="cameraNames" size="1" id="cameraNames" style="width:205px;font-size:10px;height:25px;" onChange="changeCamera()"></select></div>
回答by ProllyGeek
Check thison MDN , and it is pretty clear how to create a simple photo booth using WebRTC.
在 MDN 上查看这个,很清楚如何使用WebRTC创建一个简单的照相亭。
The second part of question is how to save the image data to disk :
问题的第二部分是如何将图像数据保存到磁盘:
- if you inspect the generated image you will find something like :
- 如果您检查生成的图像,您会发现如下内容:
< img id = "photo"
alt = "The screen capture will appear in this box."
src = "data:image/png;base64 ...... " >
- you need to get the src attribute of the img using Javascript
- 您需要使用 Javascript 获取 img 的 src 属性
var myImg = document.getElementById("yourImgId").src;
- then use php to save your file :
- 然后使用 php 保存您的文件:
$data = myImg;
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
file_put_contents('path_to_your_directory/tmp/image.png', $data);
Please note that this page will a phpfile .
请注意,此页面将是一个php文件。
php code can be inserted directly using <? ?>
.
可以直接使用 .php 代码插入<? ?>
。
If you want to do this via jquery , just post your Imgdata tothe remote php file.
如果您想通过 jquery 执行此操作,只需将您的 Imgdata 发布到远程 php 文件。