html5调用摄像头实现拍照

使用input:file标签,去调用系统默认相机,摄像,录音功能,其实是有个capture属性,直接说明需要调用什么功能

<input type="file" accept="image/*" capture="camera">
<input type="file" accept="video/*" capture="camcorder">
<input type="file" accept="audio/*" capture="microphone">

capture表示,可以捕获到系统默认的设备,比如:camera--照相机;camcorder--摄像机;microphone--录音。 accept表示,直接打开系统文件目录。 input:file标签还支持一个multiple属性,表示可以支持多选,如:

<input type="file" accept="image/*" multiple>

加上这个multiple后,capture就没啥用了,因为multiple是专门用来支持多选的。

  具体例子:
  第一步:


  下面是上图的base64编码
  第二步:先建立后台php文件:uploadimg.php
//注意:没有进行安全过滤
$base64=$_POST["img"];	
$img = base64_decode($base64);
$a = file_put_contents('./test.jpg', $img);//返回的是字节数
print_r($a);
	

  第三步:用jQuery ajax - post() 方法,将第一步选择的图片,通过第二步建立的后台文件上传。
  点击上传图片,将显示上传的字节数: 0字节。(注意:我这里删除了第二步的uploadimg.php文件,所以不能上传)。
  上传图片的jQuery代码:
$(document).ready(function(){
  $("#upimg").click(function(){
    txt=$("#j_thumb").val();	
$.post("uploadimg.php",{img:txt},function(result){
$("#span1").html(result);
  });
  });
});