c语言sscanf函数的用法是什么
269
2023-07-19
java实现电脑端扫描二维码
本文实例为大家分享了java实现电脑端扫描二维码的具体代码,供大家参考,具体内容如下
说明:js调去电脑摄像头拍照,然后获取图片base64位编码,再将base64为编码转为bolb,通过定时异步上传到后台,在后台对图片文件进行解码,返回解码结果到页面,然后页面重新加载结果(url)
第一种方式引入js
第二种方式引入js
后台java代码maven引入jar包
后台代码处理方式:
public class EwmDescode {
/**
* 解析二维码
*
* @param input
* 二维码输入流
*/
public static final String parse(InputStream input) throws Exception {
Reader reader = null;
BufferedImage image;
try {
image = ImageIO.read(input);
if (image == null) {
throw new Exception("cannot read image from inputstream.");
}
final LuminanceSource source = new BufferedImageLuminanceSource(image);
final BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
finTTSHMEFal Map
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
// 解码设置编码方式为:utf-8,
reader = new MultiFormatReader();
return reader.decode(bitmap, hints).getText();
} catch (IOException e) {
e.printStackTrace();
throw new Exception("parse QR code error: ", e);
} catch (ReaderException e) {
e.printStackTrace();
throw new Exception("parse QR code error: ", e);
}
}
/**
* 解析二维码
*
* @param url
* 二维码url
*/
public static final String parse(URL url) throws Exception {
InputStream in = null;
try {
in = url.openStream();
return parse(in);
} catch (IOException e) {
e.printStackTrace();
throw new Exception("parse QR code error: ", e);
} finally {
IOUtils.closeQuietly(in);
}
}
/**
* 解析二维码
*
* @param file
* 二维码图片文件
*/
public static final String parse(File file) throws Exception {
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(file));
return parse(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new Exception("parse QR code error: ", e);
} finally {
IOUtils.closeQuietly(in);
}
}
/**
* 解析二维码
*
* @param filePath
* 二维码图片文件路径
*/
public static final String parse(String filePath) throws Exception {
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(filePath));
return parse(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new Exception("parse QR code error: ", e);
} finally {
IOUtils.closeQuietly(in);
}
}
}
@RequestMapping("/decodeEwm")
@ResponseBody
public String decodeEwm(MultipartFile ewmImg){
String parse = null;
try {
parse = EwmDescode.parse(ewmImg.getInputStream());
} catch (Exception e) {
//e.printStackTrace();
}
String msg = "no";
if(StringUtils.isNotBlank(parse)){
return parse;
}
return msg;
}
前台jsp代码:
第一种处理方式:
<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/resources/";
String urlPath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
request.setAttribute("path", path);
request.setAttribute("basePath", basePath);
request.setAttribute("urlPath", urlPath);
%>
第二种处理方式:
<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/resources/";
String urlPath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
request.setAttribute("path", path);
request.setAttribute("basePath", basePath);
request.setAttribute("urlPath", urlPath);
%>
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~