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());//二维码格式
        }
  • 相关阅读:
    apache安装 解决无services
    需要看的书
    rbac
    IDocHostShowUI
    Eclipse快捷键大全
    Linux中vi显示中文乱码的问题
    如何编译安装Go语言
    ubuntu11.04 下LAMP 开发环境配置
    网络内核sk_buff结构体
    查看所有表名,查询结果加上字符
  • 原文地址:https://www.cnblogs.com/syscn/p/5794353.html
Copyright © 2011-2022 走看看