1.前台jsp页面代码
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
video cap control demo
------------------------------------------------------------------
2.js文件 test1.js
String.prototype.replaceAll = function(s1, s2) {
return this.replace(new RegExp(s1, "gm"), s2);
}
$(function() {
$('#btn_hidden_btns').click(function() {
document.getElementById('WebVideoCap1').hiddenControllButtons();
document.getElementById('WebVideoCap1').autofill(636, false);
}), $('#btn_show_btns').click(function() {
document.getElementById('WebVideoCap1').showControllButtons();
document.getElementById('WebVideoCap1').autofill(636, true);
}), $('#btn_start').click(function() {
document.getElementById('WebVideoCap1').start();
}), $('#btn_stop').click(function() {
document.getElementById('WebVideoCap1').stop();
}), $('#btn_cap_only').click(function() {
document.getElementById('WebVideoCap1').cap();
}), $('#btn_cap').click(function() {
document.getElementById('WebVideoCap1').cap();
document.getElementById('base64_jpeg').value = document
.getElementById('WebVideoCap1').jpegBase64Data;
document.getElementById('base64_bmp').value = document
.getElementById('WebVideoCap1').bmpBase64Data;
document.getElementById("picData").value = document
.getElementById('WebVideoCap1').jpegBase64Data;
ajax_post();
}), $('#btn_submit_only').click(function() {
document.getElementById('WebVideoCap1').cap();
document.getElementById('base64_jpeg').value = document
.getElementById('WebVideoCap1').jpegBase64Data;
document.getElementById('base64_bmp').value = document
.getElementById('WebVideoCap1').bmpBase64Data;
document.getElementById("picData").value = document
.getElementById('WebVideoCap1').jpegBase64Data;
alert(document.getElementById("picData").value.length);
document.forms[0].submit();
}), $('#btn_getdata_only').click(function() {
document.getElementById('base64_jpeg').value = document
.getElementById('WebVideoCap1').jpegBase64Data;
document.getElementById('base64_bmp').value = document
.getElementById('WebVideoCap1').bmpBase64Data;
document.getElementById("picData").value = document
.getElementById('WebVideoCap1').jpegBase64Data;
alert(document.getElementById("picData").value.length);
}), $('#btn_clear').click(function() {
document.getElementById('WebVideoCap1').clear();
}), $('#btn_submit').click(function() {
document.getElementById('WebVideoCap1').cap();
document.getElementById('base64_jpeg').value = document
.getElementById('WebVideoCap1').jpegBase64Data;
document.getElementById('base64_bmp').value = document
.getElementById('WebVideoCap1').bmpBase64Data;
document.getElementById("picData").value = document
.getElementById('WebVideoCap1').jpegBase64Data;
alert(document.getElementById("picData").value.length);
document.forms[0].submit();
});
});
function ajax_post() {
var base64_data = document.getElementById('WebVideoCap1').jpegBase64Data;
$.ajax({
url : 'http://localhost:8080/VideoCap/servlet/VideoCap4Ajax',
type : 'POST',
dataType : 'jason',
data : {
picData : "'" + base64_data + "'"
},
timeout : 1000,
success : callbackfun
});
}
function callbackfun(data) {
var obj = eval('(' + data + ')');
if ('ok' == obj.savestatus) {
alert('照片采集成功!');
}
}
3.java代码
-------------------------------------------------------------------
package com.demo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sun.misc.BASE64Decoder;
/**
* Servlet implementation class VideoCap
*/
public class VideoCap extends HttpServlet {
private static final long serialVersionUID = 1L;
private String savePath;
/**
* @see HttpServlet#HttpServlet()
*/
public VideoCap() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
savePath=config.getServletContext().getRealPath("/")+"//pics//";
File tmp_path=new File(savePath);
tmp_path.mkdirs();
System.out.println("照片数据保存路径:"+savePath);
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pic_base_64_data=request.getParameter("picData");
//System.out.println("图片数据:"+pic_base_64_data);
BASE64Decoder decode=new BASE64Decoder();
byte[] datas=decode.decodeBuffer(pic_base_64_data);
String filename=String.valueOf(System.currentTimeMillis())+".jpg";
File file=new File(this.savePath+filename);
OutputStream fos=new FileOutputStream(file);
System.out.println("照片文件名称:"+filename);
fos.write(datas);
fos.close();
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.print("
+"//pics//"+filename+")
");
out.flush();
out.close();
}
}
文章转自:http://peihexian.iteye.com/blog/676246