zoukankan      html  css  js  c++  java
  • Java实现二维码

    最近突然想写一些博客,所以就陆陆续续的编写一些自我感觉有用的并且大家也可能用到的一些技术代码。方便互相学习交流。

    今天这篇博客,主要是利用Java实现二维码:

    在写代码之前先讲一下整体思路,以方便更好更快捷的实现功能。

    (1).首先要想实现二维码功能需要导入com.google.zxing的核心jar包,我这里导入的是core-3.2.1.jar;

    (2).所谓二维码莫过于把需要的内容放入一张图片中,所以这里首先是创建一张带有参数内容的图片,方法如下:

    private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception {
    Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//容错率:容错率越高,二维码的有效像素点就越多.
    hints.put(EncodeHintType.CHARACTER_SET, CHARSET);//使用小写的编码,大写会出现]Q200026开头内容
    hints.put(EncodeHintType.MARGIN, 1);//margin 边框设置
    BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,
    hints);//二维码矩阵
    int width = bitMatrix.getWidth();
    int height = bitMatrix.getHeight();
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//将二维码矩阵放入图片流中生成图片
    for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
    image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);//填充,可设置颜色 颜色的取值为后6位
    }
    }
    if (imgPath == null || "".equals(imgPath)) {
    return image;
    }
    // 插入图片
    QRCodeUtil.insertImage(image, imgPath, needCompress);
    return image;
    }

    //插入logo 如不需要logo可以不执行此方法

    private static void insertImage(BufferedImage source, String imgPath, boolean needCompress) throws Exception {
    File file = new File(imgPath);
    if (!file.exists()) {
    System.err.println("" + imgPath + "   该文件不存在!");
    return;
    }
    Image src = ImageIO.read(new File(imgPath));
    int width = src.getWidth(null);
    int height = src.getHeight(null);
    if (needCompress) { // 压缩LOGO
    if (width > WIDTH) {
    width = WIDTH;
    }
    if (height > HEIGHT) {
    height = HEIGHT;
    }
    Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
    BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics g = tag.getGraphics();
    g.drawImage(image, 0, 0, null); // 绘制缩小后的图
    g.dispose();
    src = image;
    }
    // 插入LOGO
    Graphics2D graph = source.createGraphics();
    int x = (QRCODE_SIZE - width) / 2;
    int y = (QRCODE_SIZE - height) / 2;
    graph.drawImage(src, x, y, width, height, null);
    Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);// 后面两个参数是设置周边圆角,数值越大圆角越大
    graph.setStroke(new BasicStroke(3f));
    graph.draw(shape);
    graph.dispose();
    }

    (3).至此一张二维码图片生成方法就写完了,是不是很简单,接下来就是调用此方法。

    /**
    * 生成二维码(内嵌LOGO)

    * @param content
    *            内容
    * @param imgPath
    *            LOGO地址
    * @param destPath
    *            存放目录
    * @param needCompress
    *            是否压缩LOGO
    * @throws Exception
    */
    public static void encode(String content, String imgPath, String destPath, boolean needCompress) throws Exception {
    BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);
    mkdirs(destPath);
    String file = new Random().nextInt(99999999) + ".jpg";
    ImageIO.write(image, FORMAT_NAME, new File(destPath + "/" + file));
    }

    /**
    * 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)

    * @author LongJin
    * @date 2013-12-11 上午10:16:36
    * @param destPath
    *            存放目录
    */
    public static void mkdirs(String destPath) {
    File file = new File(destPath);
    // 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
    if (!file.exists() && !file.isDirectory()) {
    file.mkdirs();
    }
    }
    (4).如果不要logo只要在调用时将logo的路径传为null就好。

    (5).至此一个二维码生成工具就完成了,当然有人可能需要解析二维码,所以这里就把二维码解析方法也写出,同时也方便我日后查看。

    /**
    * 解析二维码

    * @param file
    *            二维码图片
    * @return
    * @throws Exception
    */
    public static String decode(File file) throws Exception {
    BufferedImage image;
    image = ImageIO.read(file);
    if (image == null) {
    return null;
    }
    BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    Result result;
    Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
    hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
    result = new MultiFormatReader().decode(bitmap, hints);
    String resultStr = result.getText();
    return resultStr;
    }

  • 相关阅读:
    VS2019正式版 密钥 Visual Studio 2019 破解 激活码 Key
    关于随机数的前世今生
    木兮的纪中集训感想
    浅谈欧洲算法——模拟退火
    你没听过的梅森旋转算法
    二分贪心杂题
    DP专项训练
    实验八 进程间通信
    实验七 信号
    实验六 进程基础
  • 原文地址:https://www.cnblogs.com/longjin-java/p/6380538.html
Copyright © 2011-2022 走看看