html5實現攝像頭拍照並使用java進行照片保存

html5調用攝像頭拍照使用的是photobooth_min.js,這個比較強大還可以直接裁剪,API網址:http://wolframhempel.github.io/photobooth-js/#isSupported

demo jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>測試HTML5相機</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript" src="<%=path %>/js/jquery-1.7.2.min.js"></script>
	<script type="text/javascript" src="<%=path %>/js/photobooth_min.js"></script>
  	<style type="text/css">
	  	*{
			margin: 0;
			padding: 0;
			list-style-type: none;
			font: 11px/16px 'Helvetica Neue', Helvetica, Arial, sans-serif;
			color: #444;
		}
	</style>
  </head>
  
  <body>
   <div id="example" style="width: 600px; height: 360px;">
   </div>
   <div id="gallery"></div>
   <script type="text/javascript">
	  	$(document).ready(function() { 
		  	$('#example').photobooth().on("image",function( event, dataUrl ){
		  		$( "#gallery" ).empty();
		  		$( "#gallery" ).append( '<img src="' + dataUrl + '" >');
		  	});
	  	}); 
  	</script>
  </body>
</html>

jsp中將img的base64直接post到servlet,然後進行保存,servlet代碼:

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.codec.binary.Base64;

import com.aobang.util.ToUTF8;

@WebServlet("/servlet/test")
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		JSONObject jsonRes = new JSONObject();
		try {
			// data:image/png;base64,前綴需要去掉
			String base64Code = 這裏是base64圖片的值;  
	        if(null == base64Code || "".equals(base64Code))  
	        {  
	            System.out.println("null===========");
	        } 
	        String filePath = request.getSession().getServletContext().getRealPath("");
	        String tempSavePath = filePath + File.separator + "test";
	        File logosavedir = new File(tempSavePath);// path1爲存放的路徑  
	        if (!logosavedir.exists()) {// 如果不存在文件夾,則自動生成  
	            logosavedir.mkdirs();  
	        }  
	        String fileName = tempSavePath + File.separator + new Date().getTime()+".jpg";  
            byte[] bytes = Base64.decodeBase64(new String(base64Code).getBytes());  
              
            ByteArrayInputStream in = new ByteArrayInputStream(bytes);  
            byte[] buffer = new byte[1024];  
            FileOutputStream o = new FileOutputStream(fileName);  
            int byteread = 0;  
            int bytesum = 0; 
            while ((byteread = in.read(buffer)) != -1) {  
            	 bytesum += byteread;  
                o.write(buffer, 0, byteread); // 文件寫操作  
                o.flush();  
            }  
            o.close();  
		} catch (Exception e) {
			e.printStackTrace();
		}
		out.print(jsonRes.toString());
		out.flush();
		out.close();
	}

}

代碼文件上傳到了我的資源裏“html5實現攝像頭拍照並使用java進行照片保存“,網址:http://download.csdn.net/detail/maskice/9783060

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章