能够正常使用的,经过测试的java+jquery+webcam调用本地摄像头拍照,然后将拍照结果上传数据库blob字段功能的实现(二)

本文介绍了一种使用Java和jQuery结合Webcam实现从本地摄像头拍照,并将拍摄的照片转换为byte数组保存到数据库Blob字段的方法。文中提供了具体的实现代码,包括将图片转换为byte数组的工具类及保存图片的具体步骤。

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

     上一篇解决了java+jquery+webcam调用本地摄像头拍照功能的实现,这一篇主要解决如何把拍照后生产的图片保存到blob字段的问题了,直接贴代码了:

   工具类图片转byte字节数组:

//图片到byte数组
 public static byte[] image2byte(String path){
	byte[] data = null;
	ByteArrayOutputStream output=null;
	FileImageInputStream input = null;
	try {
	  input = new FileImageInputStream(new File(path));
	  output = new ByteArrayOutputStream();
	  byte[] buf = new byte[10240];
	  int numBytesRead = 0;
	  while ((numBytesRead = input.read(buf)) != -1) {
		  output.write(buf, 0, numBytesRead);
	  }
	  data = output.toByteArray();
	}catch (FileNotFoundException e) {
	  e.printStackTrace();
	}catch (IOException ex1) {
	  ex1.printStackTrace();
	}finally{
		try {
			output.close();
			input.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	return data;
 }
  保存图片:
//上传图片
String savePath = request.getRealPath("/") + "//img//001.jpg";
File file_txt = new File(savePath);
if (file_txt.exists()) {
	byte[] bytes  = ImageTools.image2byte(savePath);
	//System.out.println(bytes.length);
	if(null!=bytes && bytes.length>0){
		entity.setPic(bytes);
		code =***Service.insert***(entity).getExcuteCode(); 
	} 
}else{
	code = new ExcuteCode("-99", "保存失败:不存在照片文件!");
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值