zoukankan      html  css  js  c++  java
  • 二维码生成和读取

    public class Test {
        public static void main(String[] strings) {
            Test test = new Test();
            test.CreateQrCodevoid("dsc", "D:/", "12306bypass");
            test.ReadQrCode("D:/12306bypass/1546931302959_423.png");
        }
    
        /**
         * 生成二维码
         * @param content 内容
         * @param filePath 生成的二维码存放的位置
         */
        public String CreateQrCodevoid(String content, String rootPath, String filePath) {
            int width = 300;
            int height = 300;
            String format = "png";
            //定义二维码的参数
            HashMap map = new HashMap();
            //设置编码
            map.put(EncodeHintType.CHARACTER_SET, "utf-8");
            //设置纠错等级
            map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
            map.put(EncodeHintType.MARGIN, 2);
            String fileName = null;
            try {
                //生成二维码
                BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
                //判断文件夹是否存在,不存在则创建
                String fullPath = rootPath+filePath;
                File file1 = new File(fullPath);
                if(!file1 .exists()  && !file1 .isDirectory()){
                    file1 .mkdirs();
                }
                //新的文件名
                fileName = content + System.currentTimeMillis()+"_"+new Random().nextInt(1000)+".png";
                //包含文件名的全路径
                String fileFolder = fullPath + "/" +fileName;
                Path file = new File(fileFolder).toPath();
                MatrixToImageWriter.writeToPath(bitMatrix, format, file);
            } catch (WriterException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return fileName;
        }
    
    
        /**
         * 读取二维码
         * @param path 存放二维码的全路径
         * @return
         */
        public Result ReadQrCode(String path) {
            Result result = null;
            try {
                MultiFormatReader multiFormatReader = new MultiFormatReader();
                File file = new File(path);
                BufferedImage image = ImageIO.read(file);
                //定义二维码参数
                Map hints = new HashMap();
                hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    
                //获取读取二维码结果
                BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
                result = multiFormatReader.decode(binaryBitmap, hints);
    
                System.out.println("读取二维码: " + result.toString());
                System.out.println("二维码格式: " + result.getBarcodeFormat());
                System.out.println("二维码内容: " + result.getText());
            } catch (NotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        }
    
    }
  • 相关阅读:
    Codeforces Beta Round #92 (Div. 2 Only) B. Permutations 模拟
    POJ 3281 Dining 最大流 Dinic算法
    POJ 2441 Arrange the BUlls 状压DP
    URAL 1152 Faise Mirrors 状压DP 简单题
    URAL 1039 Anniversary Party 树形DP 水题
    URAL 1018 Binary Apple Tree 树形DP 好题 经典
    pytorch中的forward前向传播机制
    .data()与.detach()的区别
    Argparse模块
    pytorch代码调试工具
  • 原文地址:https://www.cnblogs.com/daishoucheng/p/10239150.html
Copyright © 2011-2022 走看看