您的位置:首页 > Web前端 > JQuery

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

2017-08-04 10:43 1246 查看
     上一篇解决了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", "保存失败:不存在照片文件!");
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java jquery webcam