zoukankan      html  css  js  c++  java
  • java二维码之利用谷歌的zxing生成二维码,解析二维码

     生成二维码

    @RequestMapping("/123")
    public void test(HttpServletRequest request,HttpServletResponse response){
    // TODO Auto-generated method stub
    int width=300;//宽
    int height=300;//高
    String format="png";
    String content="http://www.baidu.com";
    HashMap map = new HashMap<>();
    map.put(EncodeHintType.CHARACTER_SET, "utf-8");
    map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
    try {
    BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,map);
    Path file = new File(request.getContextPath()+"/code").toPath();//二维码存放路径
    MatrixToImageWriter.writeToPath(bitMatrix, format, file); //ImageIO.write(bufferedImage, "png", outputStream);
    } catch (Exception e) {
    }
    }

    解析二维码

    /**
     * 解析二维码
     * @param args
     * @throws Exception
     */
        public static void main(String[] args) throws Exception {
            MultiFormatReader formatReader = new MultiFormatReader();
            File file = new File("D:/code/img.png");//要解析的二维码图片
            BufferedImage bufferedImage = ImageIO.read(file);
            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
            
            Map map = new HashMap<>();
            map.put(EncodeHintType.CHARACTER_SET, "utf-8");
            Result result = formatReader.decode(binaryBitmap,map);
            System.out.println(result.getText());//内容
            System.err.println(new Date(result.getTimestamp()));//时间
            System.out.println(result.getBarcodeFormat());//二维码格式
        }
  • 相关阅读:
    四十一.redis主从复制 RDB/AOF持久化 数据类型
    四十.创建Redis集群 管理集群
    三十九.NoSQL概述 部署Redis服务 、 部署LNMP+Redis
    三十八. 分库分表概述 配置mycat
    520D
    442C
    LA4788
    LA3276
    LA4122
    zoj3478
  • 原文地址:https://www.cnblogs.com/syscn/p/5794353.html
Copyright © 2011-2022 走看看