1.前台jsp頁面代碼
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>video cap control demo</title>
<script type="text/javascript" src="./js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="test1.js"></script>
</head>
<body>
<form action="<%=request.getContextPath()%>/servlet/VideoCap" id="cap_form" method="post">
<input type="hidden" id="picData" name="picData">
<object classid="clsid:34681DB3-58E6-4512-86F2-9477F1A9F3D8"
id="WebVideoCap1" codebase="<%=request.getContextPath()%>/cabs/Package.cab#version=1,0,0,50" width="636" height="238">
<param name="Visible" value="0">
<param name="AutoScroll" value="0">
<param name="AutoSize" value="0">
<param name="AxBorderStyle" value="1">
<param name="Caption" value="WebVideoCap">
<param name="Color" value="4278190095">
<param name="Font" value="宋體">
<param name="KeyPreview" value="0">
<param name="PixelsPerInch" value="96">
<param name="PrintScale" value="1">
<param name="Scaled" value="-1">
<param name="DropTarget" value="0">
<param name="HelpFile" value>
<param name="PopupMode" value="0">
<param name="ScreenSnap" value="0">
<param name="SnapBuffer" value="10">
<param name="DockSite" value="0">
<param name="DoubleBuffered" value="0">
<param name="ParentDoubleBuffered" value="0">
<param name="UseDockManager" value="0">
<param name="Enabled" value="-1">
<param name="AlignWithMargins" value="0">
<param name="ParentCustomHint" value="-1">
</object>
<hr/>
<input type="button" value="隱藏控制按鈕" id="btn_hidden_btns">
<input type="button" value="顯示控制按鈕" id="btn_show_btns">
<input type="button" value="開始捕獲" id="btn_start">
<input type="button" value="停止捕獲" id="btn_stop">
<input type="button" value="照相" id="btn_cap_only">
<input type="button" value="照相併用ajax方式提交" id="btn_cap">
<input type="button" value="照相併用非ajax方式提交" id="btn_submit">
<input type="button" value="不照相直接提交" id="btn_submit_only">
<input type="button" value="只擷取base64資料" id="btn_getdata_only">
<input type="button" value="清除資料" id="btn_clear">
<hr/>
jpeg格式base64編碼資料內容:
<textarea rows="30" cols="50" id="base64_jpeg" name="base64_jpeg"></textarea>
bmp格式base64編碼資料內容:
<textarea rows="30" cols="50" id="base64_bmp" name="base64_bmp"></textarea>
</form>
</body>
</html>
------------------------------------------------------------------
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("<img src='"+request.getContextPath()+"//pics//"+filename+"'/>");
out.flush();
out.close();
}
}
文章轉自:http://peihexian.iteye.com/blog/676246