java - 二维码生成

1.QRCode

package com.qr.code;

import com.swetake.util.Qrcode;
import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;
import jp.sourceforge.qrcode.exception.DecodingFailedException;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created by static on 2017/4/24.
 * 二维码生成器
 */
public class QRCode {
    private static final String  CODE_FORMAT = "utf-8";

    /**
     * 生成二维码
     * @param contents  描述文字
     * @param imgPath 生成图片路径
     * @param logoImg 二维码图片logo
     * @throws Exception
     */
    public static void generateQRCodeImg(String contents,String imgPath,String logoImg) throws Exception {
        int width = 140,height = 140;
        try {
            Qrcode qrcode = new Qrcode();
            qrcode.setQrcodeErrorCorrect(FaultTolerant.M.getCode());
            qrcode.setQrcodeEncodeMode('B');
            qrcode.setQrcodeVersion(7);

            BufferedImage bufImg = new BufferedImage(width, height,BufferedImage.TYPE_INT_BGR);

            Graphics2D gs = bufImg.createGraphics();
            gs.setBackground(Color.WHITE);
            gs.clearRect(0, 0, width, height);
            gs.setColor(Color.BLACK);

            int pixOff = 2;//偏移量
            byte[] contentBytes = contents.getBytes(CODE_FORMAT);
            if (contentBytes.length != 0 && contentBytes.length < 120) {
                boolean[][] codeOut = qrcode.calQrcode(contentBytes);
                for (int i = 0; i < codeOut.length; i++) {
                    for (int j = 0; j < codeOut.length; j++) {
                        if (codeOut[j][i]) {
                            gs.fillRect(j * 3 + pixOff, i * 3 + pixOff, 3, 3);
                        }
                    }
                }
            } else {
                throw new Exception("文字内容大小超出限制;(限制小于120)");
            }

            Image img = null;
            if(logoImg!=null&&logoImg!=""&&logoImg.trim().length()>0){
                img = ImageIO.read(new File(logoImg));
            }
            gs.drawImage(img, 45, 45, 50,50, null);
            gs.dispose();
            bufImg.flush();
            File imgFile = new File(imgPath);
            String[] postfix = imgPath.split("\\.");
            ImageIO.write(bufImg,postfix[1], imgFile);
        }catch (Exception ex){
            throw new Exception("生成异常:",ex);
        }
    }

    /**
     * 二维码读取
     * @param imgPath 图片路径
     */
    public static void readQRCodeImg(String imgPath){
        File imageFile = new File(imgPath);

        BufferedImage bufImg;
        String decodedData;
        try {
            bufImg = ImageIO.read(imageFile);

            QRCodeDecoder decoder = new QRCodeDecoder();
            decodedData = new String(decoder.decode(new QRImage(bufImg)), CODE_FORMAT);

            System.out.println(decodedData);
        } catch (IOException e) {
            System.out.println("读取异常: " + e.getMessage());
            e.printStackTrace();
        } catch (DecodingFailedException dfe) {
            System.out.println("解析异常: " + dfe.getMessage());
            dfe.printStackTrace();
        }
    }

    public static class QRImage implements QRCodeImage {
        BufferedImage bufImg;

        public QRImage(BufferedImage bufImg) {
            this.bufImg = bufImg;
        }

        public int getWidth() {
            return bufImg.getWidth();
        }

        public int getHeight() {
            return bufImg.getHeight();
        }

        public int getPixel(int x, int y) {
            return bufImg.getRGB(x, y);
        }

    }

    public static void main(String [] args) throws Exception {
        String contents = "这是一个二维码生成器";
        String imgPath = "2.png";
        String logoImg = "logo.png";
        QRCode.generateQRCodeImg(contents,imgPath,logoImg);

        QRCode.readQRCodeImg(imgPath);
    }
}

2.FaultTolerant

package com.qr.code;

/**
 * Created by static on 2017/4/24.
 * 排错率
 * 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),
 * 排错率越高可存储的信息越少,但对二维码清晰度的要求越小
 */
public enum FaultTolerant {
    L('L',"7%"),

    M('M',"15%"),

    Q('Q',"25%"),

    H('H',"30%");

    private FaultTolerant(char code, String describe) {
        this.code = code;
        this.describe = describe;
    }

    private char code;
    private String describe;

    public char getCode() {
        return code;
    }

    public void setCode(char code) {
        this.code = code;
    }

    public String getDescribe() {
        return describe;
    }

    public void setDescribe(String describe) {
        this.describe = describe;
    }
}



3.运行截图











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值