本文实例为大家分享了java实现电脑端扫描二维码的具体代码,供大家参考,具体内容如下
说明:js调去电脑摄像头拍的照,然后获取图片base64位编码,再将base64为编码转为bolb,通过定时异步上传到后台,在后台对图片文件进行解码,返回解码结果到页面,然后页面重新加载结果(url)
第一种方式引入js
& lt;脚本type=" text/javascript " src=" https://www.yisu.com/zixun/$ {basePath} js/jquery-1.9.min.js”祝辞& lt;/script> & lt;脚本type=" text/javascript " src=" https://www.yisu.com/zixun/$ {basePath} js/jquery.webcam.min.js”祝辞& lt;/script>
第二种方式引入js
& lt;脚本type=" text/javascript " src=" https://www.yisu.com/zixun/$ {basePath} js/jquery-1.9.min.js”祝辞& lt;/script> & lt; !——这个应该是需要的——比; & lt;脚本type=" text/javascript " src=" https://www.yisu.com/zixun/$ {basePath} js/jquery.qrcode.min.js”祝辞& lt;/script> >之前后台java代码maven引入jar包
& lt; dependency> & lt; groupId> com.github.binarywang & lt; artifactId> qrcode-utils & lt; version> 1.1 & lt;/version> & lt;/dependency> & lt; dependency> & lt; groupId> com.google.zxing & lt; artifactId> core & lt; version> 3.3.3 & lt;/dependency>后台代码处理方式:
公开课EwmDescode {/* * *解析二维码 * * @param输入 *二维码输入流 */公共静态最终字符串解析(InputStream输入){抛出异常 读者读者=零; BufferedImage形象; 尝试{ 形象=ImageIO.read(输入); 如果(图片==null) { 抛出新的异常(“不能从inputstream读取图像。”); } 最后LuminanceSource源=new BufferedImageLuminanceSource(图片); 最后BinaryBitmap位图=new BinaryBitmap(新HybridBinarizer(源)); 最后Map提示=new HashMap (); hints.put (DecodeHintType。CHARACTER_SET、“utf - 8”);//解码设置编码方式为:utf - 8, 读者=new MultiFormatReader (); 提示,返回reader.decode(位图).getText (); }捕捉(IOException e) { e.printStackTrace (); 抛出新的异常(“解析二维码错误:e); }捕捉(ReaderException e) { e.printStackTrace (); 抛出新的异常(“解析二维码错误:e); } }/* * *解析二维码 * * @param url *二维码url */公共静态最终字符串解析URL (URL)抛出异常{ InputStream=零; 尝试{ 在=url.openStream (); 返回解析(的); }捕捉(IOException e) { e.printStackTrace (); 抛出新的异常(“解析二维码错误:e); 最后}{ IOUtils.closeQuietly(的); } }/* * *解析二维码 * * @param文件 *二维码图片文件 */公共静态最终字符串解析文件(文件)抛出异常{ InputStream=零; 尝试{ 在新BufferedInputStream=(新FileInputStream(文件); 返回解析(的); }捕捉(FileNotFoundException e) { e.printStackTrace (); 抛出新的异常(“解析二维码错误:e); 最后}{ IOUtils.closeQuietly(的); } }/* * *解析二维码 * * @param filePath *二维码图片文件路径 */公共静态最终字符串解析(字符串filePath){抛出异常 InputStream=零; 尝试{ 在新BufferedInputStream=(新FileInputStream (filePath)); 返回解析(的); }捕捉(FileNotFoundException e) { e.printStackTrace (); 抛出新的异常(“解析二维码错误:e); 最后}{ IOUtils.closeQuietly(的); } } } @RequestMapping ("/decodeEwm”) @ResponseBody 公共字符串decodeEwm (MultipartFile ewmImg) { 字符串解析=零; 尝试{ 解析=EwmDescode.parse (ewmImg.getInputStream ()); }捕捉(异常e) {//e.printStackTrace (); } 字符串味精=安弧? 如果(StringUtils.isNotBlank(解析)){ 返回解析; } 返回味精; } >之前 前台jsp代码:
第一种处理方式:
% @ & lt;页面contentType=" text/html;charset=utf - 8”%比; & lt; % @ taglib前缀=" c " uri=" http://java.sun.com/jsp/jstl/core " %比; & lt; % 字符串路径=request.getContextPath (); 字符串basePath=request.getScheme () +”://? request.getServerName () +“:”+ request.getServerPort () + +路径“/资源/? 字符串urlPath=request.getScheme () +”://? request.getServerName () +“:”+ request.getServerPort () + +路径“/? 请求。setAttribute(“路径”,路径); 请求。setAttribute (“basePath basePath); 请求。setAttribute (“urlPath urlPath); %比; & lt; !DOCTYPE html> & lt; html> & lt; head> & lt;元charset=皍tf - 8”比; & lt;元http-equiv=癤-UA-Compatible”内容=癐E=边缘”比; & lt; title> webcamjava实现电脑端扫描二维码