自定义生成二维码工具类

需要使用的jar

        <!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.0</version>
        </dependency>

前面的几个生成随机数的方法是业务需要,使用的是同余生成器先生成一个10以内的随机数,然后再用这个随机数生成另一个10以内随机数,这是因为同余生成器使用的是模,会生成有规律的数,所以再随机生成一次,至于为什么同余生成器要生成10以内的,是为了下面的生成另一个10以内的随机数是公平产生的。

/**
 * @Author: Ember
 * @Date: 2021/3/11 19:32
 * @Description: 二维码生成工具类
 */
public class QRCodeUtils {
    /**
     * 种子
     */
    private static int SEED = 255;
    /**
     * 线性同余发生器生成的随机数不大于M
     */
    private static final int M = 10;
    /**
     * 生成随机数的最大最小值
     */
    private static final int MAX = 9;
    private static final int MIN = 0;

    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;

    private static final int DEFAULT_HEIGHT = 300;
    private static final int DEFAULT_WEIGHT = 300;

    private static final String CHAR_SET = "utf-8";
    private static final int MARGIN = 1;

    /**
     * 使用线性同余生成随机数
     * @return
     */
    private static int createOneRandom(int seed){
        Random random = new Random();
        //线性同余器需要的系数
        int a = random.nextInt(1024);
        int b = random.nextInt(1024);
        int result = (a * seed + b) % M;
        return result;
    }

    /**
     * 使用线性同余器生成
     * @param seed
     * @return
     */
    private static int createRandom(int seed){
        int result = MIN + seed % (MAX - MIN + 1);
        return result;
    }

    /**
     * 生成16位随机数组合
     * @return
     */
    public static String createQRCode(){
        StringBuffer buffer = new StringBuffer();
        buffer.append("QR");
        int size = 15;
        int seed = SEED;
        for (int i = 0; i < size; i++) {
            buffer.append(createOneRandom(seed));
            //重置seed
            seed = createOneRandom(seed);
        }
        return buffer.toString();
    }

    /**
     * 生成二维码图片
     * @param text
     */
    public static void encode(String text){
        Map<EncodeHintType, Object> params = new HashMap<>(2);
        params.put(EncodeHintType.CHARACTER_SET,CHAR_SET);
        params.put(EncodeHintType.MARGIN,MARGIN);
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE,DEFAULT_WEIGHT,DEFAULT_HEIGHT,params);
            int width = bitMatrix.getWidth();
            int height = bitMatrix.getHeight();
            BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
            //对二维码进行上色
            //嵌套for索引坐标
            for (int i = 0; i < width; i++) {
                for(int j = 0; j < height;j++){
                    //bitMatrix.get(i,j)返回的是判断当前坐标是否是二维码里面那些图案的坐标
                    //对单个坐标设置rgb
                    image.setRGB(i,j,bitMatrix.get(i,j)?BLACK:WHITE);
                }
            }
            ImageIO.write(image,"JPG",new File("D:\\"+text+".jpg"));
        } catch (WriterException | IOException e) {
            e.printStackTrace();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值