简单健康码教学

生成不同颜色二维码工具类

package com.zhdj.utilts;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.zhdj.pojo.LogoConfig;

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

public class QuickMarkUtil {
    //生成二维码时        的编码表
    private static final String encode = "utf-8";
    //二维码的图片格式
    private static final String formatImg = "JPG";
    //二维码的宽与高
    private static final int quickMarkSize = 300;

    /**
     * 生成二维码
     * @param content   二维码内容
     * @param rgbColor  二维码颜色
     * @throws Exception    主要是IO异常、溢出异常
     */
    public static BufferedImage buildQuickMarkImage(String content, Integer rgbColor) throws Exception {
        //定义集合,存放与二维码有关联的部分数据参数
        HashMap<EncodeHintType, Object> hintsMap = new HashMap<>();
        hintsMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hintsMap.put(EncodeHintType.CHARACTER_SET, encode);
        hintsMap.put(EncodeHintType.MARGIN, 1);
        //使用Google的二维码工具类来生成二维码
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, quickMarkSize, quickMarkSize, hintsMap);
        //把二维码画到图片缓冲区中,理解成画板
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //定义二维码颜色
        if (rgbColor == null) {
            rgbColor = 0x00000;
        }
        //开始画二维码矩阵
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? rgbColor : 0xFFFFFFFF);
            }
        }
//        CreatrQrCode creatrQrCode = new CreatrQrCode(); //LogoConfig中设置Logo的属性
//        addLogo_QRCode(qrcFile, creatrQrCode, creatrQrCode);
        return bufferedImage;
    }
    public static BufferedImage buildQuickMarkImageFile(File content, Integer rgbColor) throws Exception {
        //定义集合,存放与二维码有关联的部分数据参数
        HashMap<EncodeHintType, Object> hintsMap = new HashMap<>();
        hintsMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hintsMap.put(EncodeHintType.CHARACTER_SET, encode);
        hintsMap.put(EncodeHintType.MARGIN, 1);
        //使用Google的二维码工具类来生成二维码
//        BitMatrix bitMatrix =new MultiFormatWriter().
        BitMatrix bitMatrix = new MultiFormatWriter().encode(String.valueOf(content), BarcodeFormat.QR_CODE, quickMarkSize, quickMarkSize, hintsMap);
        //把二维码画到图片缓冲区中,理解成画板
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //定义二维码颜色
        if (rgbColor == null) {
            rgbColor = 0x00000;
        }
        //开始画二维码矩阵
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? rgbColor : 0xFFFFFFFF);
            }
        }
//        CreatrQrCode creatrQrCode = new CreatrQrCode(); //LogoConfig中设置Logo的属性
//        addLogo_QRCode(qrcFile, creatrQrCode, creatrQrCode);
        return bufferedImage;
    }
    public static void addLogo_QRCode(File qrPic, File logoPic,String name){
        LogoConfig logoConfig =new LogoConfig();
        try
        {
            if (!qrPic.isFile() || !logoPic.isFile())
            {
                System.out.print("file not find !");
                System.exit(0);
            }

            /**
             * 读取二维码图片,并构建绘图对象
             */
            BufferedImage image = ImageIO.read(qrPic);
            Graphics2D g = image.createGraphics();

            /**
             * 读取Logo图片
             */
            BufferedImage logo = ImageIO.read(logoPic);

            int widthLogo = image.getWidth()/logoConfig.getLogoPart();
            //    int    heightLogo = image.getHeight()/logoConfig.getLogoPart();
            int    heightLogo = image.getWidth()/logoConfig.getLogoPart(); //保持二维码是正方形的
            // 计算图片放置位置
            int x = (image.getWidth() - widthLogo) / 2;
            int y = (image.getHeight() - heightLogo) / 2 ;


            //开始绘制图片
            g.drawImage(logo, x, y, widthLogo, heightLogo, null);
            g.drawRoundRect(x, y, widthLogo, heightLogo, 10, 10);
            g.setStroke(new BasicStroke(logoConfig.getBorder()));
            g.setColor(logoConfig.getBorderColor());
            g.drawRect(x, y, widthLogo, heightLogo);

            g.dispose();

            ImageIO.write(image, "jpg", new File("D:/javaee_uniapp/graduation/图片管理/healthy/"+name));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
    /**
     * 生成灰色二维码
     */
    public static BufferedImage buildGrayQuickMarkImage(String context) throws Exception {
        return buildQuickMarkImage(context,Color.GRAY);
    }
    /**
     * 生成绿色二维码
     */
    public static BufferedImage buildBlueQuickMarkImage(String context) throws Exception {
        return buildQuickMarkImage(context,Color.BLUE);
    }
    public static BufferedImage buildGreenQuickMarkImage(String context) throws Exception {
        return buildQuickMarkImage(context,Color.GREEN);
    }
    /**
     * 生成黄色二维码
     */
    public static BufferedImage buildYellowQuickMarkImage(String context) throws Exception {
        return buildQuickMarkImage(context,Color.YELLOW);
    }
    public static BufferedImage buildDarkYellowQuickMarkImage(String context) throws Exception {
        return buildQuickMarkImage(context,Color.DARKYELLOW);
    }
    /**
     * 生成红色二维码
     */
    public static BufferedImage buildRedQuickMarkImage(String context) throws Exception {
        return buildQuickMarkImage(context,Color.RED);
    }
    public static BufferedImage buildDarkRedQuickMarkImage(String context) throws Exception {
        return buildQuickMarkImage(context,Color.DARKRED);
    }
    /**
     * 生成纯黑色二维码
     */
    public static BufferedImage  buildBlackQuickMarkImage(String context) throws Exception {
        return buildQuickMarkImage(context,Color.BLACK);
    }
    /**
     * 图片写到本地
     * @param path  保存路径
     * @param image 图片内容
     * @throws IOException  Io异常
     */
    public static void writeToLocal(String path,BufferedImage image) throws IOException {
        File file = new File(path);
        // 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
        ImageIO.write(image, formatImg, new File(path));
    }

    /**
     * RGB颜色值
     */
    public static class Color{
        // 0xFF999 灰码
        // 0xFF8CC152 浅绿码
        // 0x006400  深绿码
        // 0xFFCC3333 浅红码
        // 0x800 深红码
        // 0xFFFFFF00 浅黄码
        // 0xFFDC00 深黄码
        //0x000 黑码
        public static final Integer GRAY=0xFFA9A9A9;
        public static final Integer BLUE=0xFF8CC152;
        public  static  final  Integer GREEN =0x006400;
        public static final Integer YELLOW=0xFFFFFF00;
        public static final Integer RED=0xFFCC3333;
        private  static  final  Integer DARKRED=0x8B0000;
        private  static  final  Integer DARKYELLOW=0xFFDC00;
        private  static  final  Integer  BLACK =0x000;

    }


}

实体类

用于在二维码中间放上喜欢的图片

package com.zhdj.pojo;


import java.awt.*;

public class LogoConfig {

    public static final Color DEFAULT_BORDERCOLOR = Color.WHITE;

    public static final int DEFAULT_BORDER = 2;

    public static final int DEFAULT_LOGOPART = 6;

    private final int border = DEFAULT_BORDER;

    private final Color borderColor;

    private final int logoPart;


    public LogoConfig()


    {
         this(DEFAULT_BORDERCOLOR, DEFAULT_LOGOPART);

    }


        public LogoConfig(Color borderColor, int logoPart)


    {
         this.borderColor = borderColor;
         this.logoPart = logoPart;

    }


    public Color getBorderColor()


    {
         return borderColor;

    }


    public int getBorder()


    {
         return border;

    }



    public int getLogoPart()


    {
         return logoPart;

    }

}

测试

   @Test
    void test1() throws Exception {
        //存放的位置路径
        String path="D:/javaee_uniapp/graduation/图片管理/healthy/";
        //中间图片的路径
        File pathFile=new File("D:/javaee_uniapp/graduation/图片管理/healthy/d80e07a1348db1b344f83e2375c14e8.jpg");
        //二维码生成出来的图片名字
        String gray="灰色";
        String blueQuick="浅绿";
        String blue="深绿";
        String green="浅黄";
        String yellow="深黄";
        String red="浅红";
        String darkRed="深红";

//        LocalDateTime localDateTime=LocalDateTime.now();
        //扫码后出来的文字
        BufferedImage grayQuickMarkImage = QuickMarkUtil.buildGrayQuickMarkImage("http://www.feasy.top/");
        BufferedImage blueQuickMarkImage = QuickMarkUtil.buildBlueQuickMarkImage("http://192.168.8.107:8080/#/pages/index/index");
        BufferedImage GreenQuickMarkImage=QuickMarkUtil.buildGreenQuickMarkImage(blueQuick+"色二维码");
        BufferedImage yellowQuickMarkImage = QuickMarkUtil.buildYellowQuickMarkImage(green+"色二维码");
        BufferedImage DarkYellowQuickMarkImage= QuickMarkUtil.buildDarkYellowQuickMarkImage(yellow+"色健康吗");
        BufferedImage redQuickMarkImage = QuickMarkUtil.buildRedQuickMarkImage(red+"色二维码");
        BufferedImage DarkRedQuickMarkImage=QuickMarkUtil.buildDarkRedQuickMarkImage(darkRed+"色二维码");
       //生成二维码
        QuickMarkUtil.writeToLocal(path+gray+"码.jpg",grayQuickMarkImage);
        QuickMarkUtil.writeToLocal(path+blue+"码.jpg",blueQuickMarkImage);
        QuickMarkUtil.writeToLocal(path+blueQuick+"码.jpg",yellowQuickMarkImage);
        QuickMarkUtil.writeToLocal(path+green+"码.jpg",redQuickMarkImage);
        QuickMarkUtil.writeToLocal(path+yellow+"码.jpg",GreenQuickMarkImage);
        QuickMarkUtil.writeToLocal(path+red+"码.jpg",DarkYellowQuickMarkImage);
        QuickMarkUtil.writeToLocal(path+darkRed+"码.jpg",DarkRedQuickMarkImage);
        //存储二维码
        File grayFile= new File(path+gray+"码.jpg");
        File blueQuickFile=new File(path+blueQuick+"码.jpg");
        File blueFile=new File(path+blue+"码.jpg");
        File greenFile=new File(path+green+"码.jpg");
        File yellowFile=new File(path+yellow+"码.jpg");
        File redFile=new File(path+red+"码.jpg");
        File darkRedFile=new File(path+darkRed+"码.jpg");
        //在二维码中间放图片并且覆盖原来的二维码
        QuickMarkUtil.addLogo_QRCode(grayFile,pathFile,gray);
        QuickMarkUtil.addLogo_QRCode(blueQuickFile,pathFile,blueQuick);
        QuickMarkUtil.addLogo_QRCode(blueFile,pathFile,blue);
        QuickMarkUtil.addLogo_QRCode(greenFile,pathFile,green);
        QuickMarkUtil.addLogo_QRCode(yellowFile,pathFile,yellow);
        QuickMarkUtil.addLogo_QRCode(redFile,pathFile,red);
        QuickMarkUtil.addLogo_QRCode(darkRedFile,pathFile,darkRed);

    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值