一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

java实现电脑端扫描二维码

时间:2018-10-19 编辑:猪哥 来源:一聚教程网

本文实例为大家分享了java实现电脑端扫描二维码的具体代码,供大家参考,具体内容如下

说明:js调去电脑摄像头拍照,然后获取图片base64位编码,再将base64为编码转为bolb,通过定时异步上传到后台,在后台对图片文件进行解码,返回解码结果到页面,然后页面重新加载结果(url)

第一种方式引入js


第二种方式引入js




后台java代码maven引入jar包

 
   com.github.binarywang
   qrcode-utils
   1.1
 
 
 
   com.google.zxing
   core
   3.3.3
 

后台代码处理方式:

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));
     final Map hints = new HashMap();
     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);
%>


  
    
    
    webcam
    
 
    
    
 
    
    
 
  
 
  
 
    

第二种处理方式:

<%@ 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);
%>



QRCODE





 

 
 

 

热门栏目