java 摄像头 jsp_jsp调用摄像头拍照,控制摄像头操作

本文介绍了一个基于Web的视频采集系统实现方法,重点讲解了如何使用前端JS和Java后端技术进行视频帧的捕获及JPEG/BMP格式图片的Base64编码,并通过Ajax将数据发送到服务器进行保存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码下载在最下面,使劲的往下拽吧

代码下载在最下面,使劲的往下拽吧

代码下载在最下面,使劲的往下拽吧

--------------------------------------------------------------------

1.前台jsp页面代码

pageEncoding="utf-8"%>

video cap control demo

id="WebVideoCap1" codebase="/cabs/Package.cab#version=1,0,0,50" width="636" height="238">



jpeg格式base64编码数据内容:

bmp格式base64编码数据内容:

------------------------------------------------------------------

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("%22+filename+%22");

out.flush();

out.close();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值