如何使用HTML5实现利用摄像头拍照上传功能
展开全部
前台:
<body>
<div id="contentHolder">
<video id="video" width="320" height="320" autoplay></video>
<button id="picture" style="display:block" >拍照</button>
<canvas style="display:block" id="canvas" width="320" height="320"></canvas>
<button id="sc" style="display:block" >上传</button>
</div>
<script>
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia;
if (navigator.getUserMedia) {
navigator.getUserMedia({ audio: true, video: { width: 320, height: 320 } },
function(stream) {
var video = document.getElementById("video");
video.src = window.URL.createObjectURL(stream);
video.onloadedmetadata = function(e) {
console.log('nihao44eee4aaaaddda');
video.play();
};
},
function(err) {
console.log("The following error occurred: " + err.name);
}
);
} else {
console.log("getUserMedia not supported");
}
var context = document.getElementById("canvas").getContext("2d");
document.getElementById("picture").addEventListener("click", function () {
context.drawImage(video, 0, 0, 320, 320);
});
document.getElementById("sc").addEventListener("click", function () {
var imgData=document.getElementById("canvas").toDataURL("image/png");
var data=imgData.substr(22);
$.post('recorder/target/sc',{'sj':data});
});
</script>
</body>
后台:
@RequestMapping(value = "/sc", method = RequestMethod.POST)
@ResponseBody
public String sc(HttpServletResponse response,HttpServletRequest request,String sj) throws Exception {
GenerateImage(sj);
return "sxt";
}
// base64字符串转化成图片
public static boolean GenerateImage(String imgStr) { // 对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) // 图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
// 生成jpeg图片
String imgFilePath = "F:/test22.png";// 新生成的图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
<body>
<div id="contentHolder">
<video id="video" width="320" height="320" autoplay></video>
<button id="picture" style="display:block" >拍照</button>
<canvas style="display:block" id="canvas" width="320" height="320"></canvas>
<button id="sc" style="display:block" >上传</button>
</div>
<script>
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia;
if (navigator.getUserMedia) {
navigator.getUserMedia({ audio: true, video: { width: 320, height: 320 } },
function(stream) {
var video = document.getElementById("video");
video.src = window.URL.createObjectURL(stream);
video.onloadedmetadata = function(e) {
console.log('nihao44eee4aaaaddda');
video.play();
};
},
function(err) {
console.log("The following error occurred: " + err.name);
}
);
} else {
console.log("getUserMedia not supported");
}
var context = document.getElementById("canvas").getContext("2d");
document.getElementById("picture").addEventListener("click", function () {
context.drawImage(video, 0, 0, 320, 320);
});
document.getElementById("sc").addEventListener("click", function () {
var imgData=document.getElementById("canvas").toDataURL("image/png");
var data=imgData.substr(22);
$.post('recorder/target/sc',{'sj':data});
});
</script>
</body>
后台:
@RequestMapping(value = "/sc", method = RequestMethod.POST)
@ResponseBody
public String sc(HttpServletResponse response,HttpServletRequest request,String sj) throws Exception {
GenerateImage(sj);
return "sxt";
}
// base64字符串转化成图片
public static boolean GenerateImage(String imgStr) { // 对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) // 图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
// 生成jpeg图片
String imgFilePath = "F:/test22.png";// 新生成的图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询