java实现电脑端扫描二维码

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

创新互联是一家以网络技术公司,为中小企业提供网站维护、网站建设、成都做网站、网站备案、服务器租用、域名与空间、软件开发、成都微信小程序等企业互联网相关业务,是一家有着丰富的互联网运营推广经验的科技公司,有着多年的网站建站经验,致力于帮助中小企业在互联网让打出自已的品牌和口碑,让企业在互联网上打开一个面向全国乃至全球的业务窗口:建站联系电话:18982081108

说明: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





 

 
 

 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。 


分享标题:java实现电脑端扫描二维码
URL标题:http://azwzsj.com/article/pescge.html