在二维码上添加图片主题(支持链接跳转)

(1) MatrixToImageWriter二维码创建核心类

package com.yx.yzh.utils;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import org.apache.tomcat.util.codec.binary.Base64;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

/**
 * @className 
 * @description 二维码生成器
 * @author Administrator
 * @date 2018-10-20
 */
public class MatrixToImageWriter {
    //黑色
    private static final int BLACK = 0xFF000000;
    //背景颜色是白色
    private static final int WHITE = 0xFFFFFFFF;
    //二维码默认文字
    private static final String WORDCONTENT = "扫一扫"; 
    private MatrixToImageWriter() {
    }
    
    /**
     * @functionName toBufferedImage
     * @description 构建初始化二维码
     * @param matrix
     * @author yzh
     * @date 2018-10-20
     * @return
     */
    public static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.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, matrix.get(x, y) ? BLACK : WHITE);
            }
        }
        return image;
    }
    
    /**
     * @functionName getQRCodeBufferedImage
     * @description 生成二维码
     * @param bufferedImage 图片
     * @param content 编码内容 :编码内容既可以为文字描述,也可以为链接,如果是链接,那么会自动跳转到相应的网页
     * @param barcodeFormat 编码类型 
     * @param width 图片宽度 
     * @param hints 设置参数 
     * @return
     */  
    public static BufferedImage getQRCodeBufferedImage(String content, BarcodeFormat barcodeFormat, int width){  
        MultiFormatWriter multiFormatWriter = null;
        //图片高度
        int height = 0;
        //图片宽度不超过500,不小于100
        if(width > 500){
            width = 500;
        }else if(width < 100){
            width = 100;
        }
        //字体依赖于图片宽度而动态变化
        height = width+width/10;
        BitMatrix bm = null;
        BufferedImage image = null;
        try{
            multiFormatWriter = new MultiFormatWriter();
            //获得二维码的格式参数 
            Map<EncodeHintType,?> hints = getDecodeHintType();
            //参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数 
            bm = multiFormatWriter.encode(content, barcodeFormat, width, height, hints);
            int w = width;
            int h = height;
            image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            //开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色 
            for (int x = 0; x < w; x++){
                for (int y = 0; y < h; y++){
                    image.setRGB(x, y, bm.get(x, y) ? BLACK : WHITE);
                }
            }
        }catch (WriterException e){
            e.printStackTrace();
        }
        return image;
    }
    
    /**
     * @functionName addLogoToQRCode
     * @description 给二维码图片添加Logo
     * @param logoPic logo图片文件对象
     * @param exportFile 导出带Logo的二维码图片对象
     * @param qRCodePictureType 导出带Logo的二维码图片的后缀
     * @param wordContent 二维码添加的文字内容
     * @param width 图片宽度
     * @date  2018-10-20 
     * @return
     */  
    public static String addLogoToQRCode(BufferedImage bim, File logoPic,File exportFile,String wordContent,int width){
        try{
            //读取二维码图片,并构建绘图对象 
            BufferedImage image = bim;
            //字体大小
            int fontSize = 0; 
            Graphics2D g = image.createGraphics();
            //读取Logo图片 
            BufferedImage logo = ImageIO.read(logoPic);
            //设置logo的大小,本人设置为二维码图片的20%,因为过大会盖掉二维码 
            int widthLogo = logo.getWidth(null)>image.getWidth()*3/10?(image.getWidth()*3/10):logo.getWidth(null),   
            heightLogo = logo.getHeight(null)>image.getHeight()*3/10?(image.getHeight()*3/10):logo.getWidth(null);
            //logo放在中心 
            int x = (image.getWidth() - widthLogo) / 2;  
            int y = (image.getHeight() - heightLogo) / 2;  
            //开始绘制图片  
            g.drawImage(logo, x, y, widthLogo, heightLogo, null);
            g.dispose();
            //图片高度
            int height = 0;
            //图片宽度不超过500,不小于100
            if(width > 500){
                width = 500;
            }else if(width < 100){
                width = 100;
            }
            //字体依赖于图片宽度而动态变化
            height = width + Math.round(width/10);
            fontSize = 12 + Math.round(width/100);
            
            //把商品名称添加上去,商品名称不要太长哦,这里最多支持两行。太长就会自动截取啦  
            if (wordContent != null && !wordContent.equals("")){
                //新的图片,把带logo的二维码下面加上文字 
                BufferedImage outImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
                Graphics2D outg = outImage.createGraphics();
                //画二维码到新的面板  
                outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
                //画文字到新的面板 
                outg.setColor(Color.BLACK);
                int strWidth = outg.getFontMetrics().stringWidth(wordContent);
                    //画文字   
                outg.setFont(new Font("宋体",Font.BOLD,fontSize));//字体、字型、字号 
                if(strWidth*2-width>0){
                    outg.drawString(wordContent,0,image.getHeight()-5);
                }else{
                    outg.drawString(wordContent,(width-strWidth)/2,image.getHeight()-5);
                }
                outg.dispose();
                outImage.flush();
                image = outImage;
            }
            logo.flush();
            image.flush();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            baos.flush();
            //注意这里的png图片格式不能改写成jpg,否则会变成颜色失真的二维码
            ImageIO.write(image, "png", baos);
            //二维码生成的路径,但是实际项目中,我们是把这生成的二维码显示到界面上的,因此下面的折行代码可以注释掉
            //可以看到这个方法最终返回的是这个二维码的imageBase64字符串
            //前端用 <img src="https://img-blog.csdnimg.cn/2022010707321796017.png"/> 其中${imageBase64QRCode}对应二维码的imageBase64字符串 
            ImageIO.write(image,"png", exportFile); 
            //TODO
            String imageBase64QRCode =  Base64.encodeBase64String(baos.toByteArray());
            baos.close();
            return imageBase64QRCode;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * @functionName addLogoToQRCode
     * @description 给二维码图片添加Logo
     * @param logoPic logo图片文件对象
     * @param exportFile 导出带Logo的二维码图片对象
     * @param qRCodePictureType 导出带Logo的二维码图片的后缀
     * @param width 图片宽度
     * @date  2018-10-20 
     * @return
     */  
    public static String addLogoToQRCode(BufferedImage bim, File logoPic,File exportFile,int width){
        try{
            //读取二维码图片,并构建绘图对象 
            BufferedImage image = bim;
            //字体大小
            int fontSize = 0; 
            Graphics2D g = image.createGraphics();
            //读取Logo图片 
            BufferedImage logo = ImageIO.read(logoPic);
            //设置logo的大小,本人设置为二维码图片的20%,因为过大会盖掉二维码 
            int widthLogo = logo.getWidth(null)>image.getWidth()*3/10?(image.getWidth()*3/10):logo.getWidth(null),   
            heightLogo = logo.getHeight(null)>image.getHeight()*3/10?(image.getHeight()*3/10):logo.getWidth(null);
            //logo放在中心 
            int x = (image.getWidth() - widthLogo) / 2;  
            int y = (image.getHeight() - heightLogo) / 2;  
            //开始绘制图片  
            g.drawImage(logo, x, y, widthLogo, heightLogo, null);
            g.dispose();
            
            //图片高度
            int height = 0;
            //图片宽度不超过500,不小于100
            if(width > 500){
                width = 500;
            }else if(width < 100){
                width = 100;
            }
            height = width + Math.round(width/10);
            //字体依赖于图片宽度而动态变化
            fontSize = 12 + Math.round(width/100);
            //新的图片,把带logo的二维码下面加上文字 
            BufferedImage outImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D outg = outImage.createGraphics();
            //画二维码到新的面板  
            outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
            //画文字到新的面板 
            outg.setColor(Color.BLACK);
            outg.setFont(new Font("宋体",Font.BOLD,fontSize));//字体、字型、字号 
            //获得文字的长度
            int strWidth = outg.getFontMetrics().stringWidth(WORDCONTENT);
            
            //画文字   
            outg.drawString(WORDCONTENT,(width-strWidth)/2,image.getHeight()-5);
            outg.dispose();
            outImage.flush();
            image = outImage;
            
            logo.flush();
            image.flush();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            baos.flush();
            ImageIO.write(image,"png", baos);
            //二维码生成的路径,但是实际项目中,我们是把这生成的二维码显示到界面上的,因此下面的折行代码可以注释掉
            //可以看到这个方法最终返回的是这个二维码的imageBase64字符串
            //前端用 <img src="https://img-blog.csdnimg.cn/2022010707321796017.png"/> 其中${imageBase64QRCode}对应二维码的imageBase64字符串 
            ImageIO.write(image,"png", exportFile); 
            //TODO
            String imageBase64QRCode =  Base64.encodeBase64String(baos.toByteArray());
            baos.close();
            return imageBase64QRCode;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
    public static void writeToFile(BitMatrix matrix, String format, File file)throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        if (!ImageIO.write(image, format, file)) {
            throw new IOException("Could not write an image of format "
                    + format + " to " + file);
        }
    }

    public static void writeToStream(BitMatrix matrix, String format,OutputStream stream) throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        if (!ImageIO.write(image, format, stream)) {
            throw new IOException("Could not write an image of format "
                    + format);
        }
    }

    public static BufferedImage writeToFile(BitMatrix matrix, String format)throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        return image;
    }
    
    /**
     * @functionName getDecodeHintType
     * @description 设置二维码的格式参数 
     * @date 2018-10-20
     * @return Map
     */
    public static Map<EncodeHintType, Object> getDecodeHintType(){
        //用于设置QR二维码参数 
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        //设置QR二维码的纠错级别(H为最高级别)具体级别信息 
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        //设置编码方式 
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, 0);
        /*
        hints.put(EncodeHintType.MAX_SIZE, 350);
        hints.put(EncodeHintType.MIN_SIZE, 230);
        */
        return hints;
    }
}

(2)QRCodeUtils二维码工具类

package com.yx.yzh.utils;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;
import javax.servlet.http.HttpServletResponse;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.yx.yzh.enums.QRCodePictureTypeEnum;
//QR (quick response) code
/**
 * @className QRCodeUtils
 * @description 二维码工具类
 * @author yzh
 * @date 2018-10-15
 */
public class QRCodeUtils {
    private Hashtable<EncodeHintType, String> hints;
    /**二维码的图片格式*/
    private String qRCodeFormat;
    /**默认的图片后缀*/
    private static final String DEFAULTQRCODEFORMAT = "jpg";
    /***/
    /**
     * @functionName QRCodeUtils
     * @description 构造器,初始化信息
     * @author yzh
     * @date 2018-10-15
     */
    public QRCodeUtils(){
         hints = new Hashtable<EncodeHintType, String>();
         qRCodeFormat = "";
    }
    
    /**
     * @functionName uploadQRCodeImage
     * @description 生成并上传二维码图片
     * @author yzh
     * @param QRCodeContent 二维码的内容
     * @param QRPicWidth 二维码图片宽度
     * @param QRHeight 二维码图片高度
     * @param qRCodePictureTypeEnum 二维码格式枚举(防止用户手动输入错误的类型)
     * @param fileName 文件名
     * @param outputFilePath 二维码图片输出路经 
     * @date 2018-10-15
     */
    public void uploadQRCodeImage(String QRCodeContent,int QRPicWidth,int QRHeight,String outputFilePath,String fileName,QRCodePictureTypeEnum qRCodePictureTypeEnum){
        /*if(qRCodePictureTypeEnum == QRCodePictureTypeEnum.GIF){
            qRCodeFormat = "gif";
        }else if(qRCodePictureTypeEnum == QRCodePictureTypeEnum.JPEG){
            qRCodeFormat = "jpeg";
        }else if(qRCodePictureTypeEnum == QRCodePictureTypeEnum.JPG){
            qRCodeFormat = "jpg";
        }else if(qRCodePictureTypeEnum == QRCodePictureTypeEnum.PNG){
            qRCodeFormat = "png";
        }*/
        try{
            //内容所使用字符集编码
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            //格式
            BitMatrix bitMatrix = new MultiFormatWriter().encode(QRCodeContent, BarcodeFormat.QR_CODE, QRPicWidth, QRHeight, hints);
            // 生成二维码
            File outputFile = new File(outputFilePath+"\\"+fileName+"."+qRCodePictureTypeEnum.toString().toLowerCase());
            MatrixToImageWriter.writeToFile(bitMatrix, qRCodeFormat, outputFile);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    /**
     * @functionName downLoadQRCodeImage
     * @description 客户端下载二维码图片(不包含logo和文字以及跳转)
     * @author yzh
     * @param response 
     * @param QRCodeContent 二维码的内容
     * @param QRPicWidth 二维码图片宽度
     * @param QRHeight 二维码图片高度
     * @param qRCodePictureTypeEnum 二维码格式枚举(防止用户手动输入错误的类型)
     * @param fileName 文件名
     * @param outputFilePath 二维码图片输出路经 
     * @date 2018-10-15
     */
    public void downLoadQRCodeImage(HttpServletResponse response,String QRCodeContent,int QRPicWidth,int QRHeight,String outputFilePath,String fileName,QRCodePictureTypeEnum qRCodePictureTypeEnum){
        OutputStream stream = null;
        try{
            //1、先上传二维码图片
            this.uploadQRCodeImage(QRCodeContent, QRPicWidth, QRHeight, outputFilePath, fileName, qRCodePictureTypeEnum);
            //2、下载二维码图片
            //获取刚才生成的图片,返回二维码图片流:前端通过访问该接口就可以访问这个图片流,从而获取到图片
            String newfileName = outputFilePath+"\\"+fileName+"."+qRCodeFormat;
            File file = new File(newfileName);
            FileInputStream inputStream = new FileInputStream(file);
            byte[] data = new byte[(int)file.length()];
            inputStream.read(data);
            inputStream.close();
            //response.setContentType("image/jpg");
            //输出流
            stream = response.getOutputStream();
            //写入图片数据
            stream.write(data);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                if(null != stream){
                    stream.flush();
                    stream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * @functionName downLoadLogoQRCodeImage
     * @description 上传带logo的二维码图片(支持图片宽度)
     * @param request
     * @param content 编码内容 :编码内容既可以为文字描述,也可以为链接,如果是链接,那么会自动跳转到相应的网页
     * @param logoPicFilePathAndName logo图片全名
     * @param exportFilePath 导出二维码图片路径
     * @param exportFileName 导出二维码图片名称
     * @param qRCodePictureTypeEnum 图片后缀枚举
     * @param width 图片宽度 
     * @date 2018-10-15
     */
    public void downLoadLogoQRCodeImage(HttpServletResponse response,String content,String logoPicFilePathAndName,String exportFilePath,String exportFileName,int width){
        OutputStream stream = null;
        try{
            //1、先上传二维码图片
            uploadLogoQRCode(content, logoPicFilePathAndName, exportFilePath, exportFileName, width);
            //2、下载二维码图片
            //获取刚才生成的图片,返回二维码图片流:前端通过访问该接口就可以访问这个图片流,从而获取到图片
            String newfileName = exportFilePath+"\\"+exportFileName+"."+DEFAULTQRCODEFORMAT;
            File file = new File(newfileName);
            FileInputStream inputStream = new FileInputStream(file);
            byte[] data = new byte[(int)file.length()];
            inputStream.read(data);
            inputStream.close();
            //response.setContentType("image/jpg");
            //输出流
            stream = response.getOutputStream();
            //写入图片数据
            stream.write(data);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                if(null != stream){
                    stream.flush();
                    stream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    
    /**
     * @functionName downLoadLogoQRCodeImage
     * @description 上传带logo的二维码图片(支持图片后缀选择、宽度)
     * @param request
     * @param content 编码内容 :编码内容既可以为文字描述,也可以为链接,如果是链接,那么会自动跳转到相应的网页
     * @param logoPicFilePathAndName logo图片全名
     * @param exportFilePath 导出二维码图片路径
     * @param exportFileName 导出二维码图片名称
     * @param qRCodePictureTypeEnum 图片后缀枚举
     * @param width 图片宽度 
     * @return
     * @date 2018-10-15
     */
    public void downLoadLogoQRCodeImage(HttpServletResponse response,String content,String logoPicFilePathAndName,String exportFilePath,String exportFileName,QRCodePictureTypeEnum qRCodePictureTypeEnum,int width){
        OutputStream stream = null;
        try{
            //1、先上传二维码图片
            uploadLogoQRCode(content, logoPicFilePathAndName, exportFilePath, exportFileName,qRCodePictureTypeEnum,width);
            //2、下载二维码图片
            //获取刚才生成的图片,返回二维码图片流:前端通过访问该接口就可以访问这个图片流,从而获取到图片
            String newfileName = exportFilePath+"\\"+exportFileName+"."+qRCodePictureTypeEnum;
            File file = new File(newfileName);
            FileInputStream inputStream = new FileInputStream(file);
            byte[] data = new byte[(int)file.length()];
            inputStream.read(data);
            inputStream.close();
            //response.setContentType("image/jpg");
            //输出流
            stream = response.getOutputStream();
            //写入图片数据
            stream.write(data);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                if(null != stream){
                    stream.flush();
                    stream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * @functionName downLoadLogoQRCodeImage
   * @description 上传带logo的二维码图片(支持图片后缀选择、二维码文字描述、宽度)
     * @param request
     * @param content 编码内容 :编码内容既可以为文字描述,也可以为链接,如果是链接,那么会自动跳转到相应的网页
     * @param wordContent 二维码添加的文字内容 
     * @param logoPicFilePathAndName logo图片全名
     * @param exportFilePath 导出二维码图片路径
     * @param exportFileName 导出二维码图片名称
     * @param qRCodePictureTypeEnum 图片后缀枚举
     * @param width 图片宽度 
     * @return
     * @date 2018-10-15
     */
    public void downLoadLogoQRCodeImage(HttpServletResponse response,String content,String wordContent,String logoPicFilePathAndName,String exportFilePath,String exportFileName,QRCodePictureTypeEnum qRCodePictureTypeEnum,int width){
        OutputStream stream = null;
        try{
            //1、先上传二维码图片
            uploadLogoQRCode(content, wordContent,logoPicFilePathAndName, exportFilePath, exportFileName,qRCodePictureTypeEnum,width);
            //2、下载二维码图片
            //获取刚才生成的图片,返回二维码图片流:前端通过访问该接口就可以访问这个图片流,从而获取到图片,枚举的引用可以转换成字符
            String newfileName = exportFilePath+"\\"+exportFileName+"."+qRCodePictureTypeEnum.toString().toLowerCase();
            File file = new File(newfileName);
            FileInputStream inputStream = new FileInputStream(file);
            byte[] data = new byte[(int)file.length()];
            inputStream.read(data);
            inputStream.close();
            //response.setContentType("image/jpg");
            //输出流
            stream = response.getOutputStream();
            //写入图片数据
            stream.write(data);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                if(null != stream){
                    stream.flush();
                    stream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * @functionName uploadLogoQRCode
     * @description 上传带logo的二维码图片(支持图片后缀选择、二维码文字描述、宽度)
     * @param request
     * @param content 编码内容 :编码内容既可以为文字描述,也可以为链接,如果是链接,那么会自动跳转到相应的网页
     * @param wordContent 二维码添加的文字内容 
     * @param logoPicFilePathAndName logo图片全名
     * @param exportFilePath 导出二维码图片路径
     * @param exportFileName 导出二维码图片名称
     * @param qRCodePictureTypeEnum 图片后缀枚举
     * @param width 图片宽度 
     * @return
     */ 
    public static String uploadLogoQRCode(String content,String wordContent,String logoPicFilePathAndName,String exportFilePath,String exportFileName,QRCodePictureTypeEnum qRCodePictureTypeEnum,int width){
        /*String qRCodeFormat = ""; 
        //判断文件类型,枚举的每一项返回枚举类型
        if(qRCodePictureTypeEnum == QRCodePictureTypeEnum.GIF){
            qRCodeFormat = "gif";
        }else if(qRCodePictureTypeEnum == QRCodePictureTypeEnum.JPEG){
            qRCodeFormat = "jpeg";
        }else if(qRCodePictureTypeEnum == QRCodePictureTypeEnum.JPG){
            qRCodeFormat = "jpg";
        }else if(qRCodePictureTypeEnum == QRCodePictureTypeEnum.PNG){
            qRCodeFormat = "png";
        }*/
  
        File exportFilePathObj = new File(exportFilePath);
        if(!exportFilePathObj.exists()){
            exportFilePathObj.mkdirs();
        }
        //获取导出文件对象
        File exportFile = new File(exportFilePath+"\\"+exportFileName+"."+qRCodePictureTypeEnum.toString().toLowerCase());
        try{
            //获取图片流
            BufferedImage bim = MatrixToImageWriter.getQRCodeBufferedImage(content, BarcodeFormat.QR_CODE, width);
            //生成带logo的二维码图片
            return MatrixToImageWriter.addLogoToQRCode(bim, new File(logoPicFilePathAndName), exportFile, wordContent,width);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }  
    
    /**
     * @functionName uploadLogoQRCode
     * @description 上传带logo的二维码图片(支持图片后缀选择、宽度)
     * @param request
     * @param content 编码内容 :编码内容既可以为文字描述,也可以为链接,如果是链接,那么会自动跳转到相应的网页
     * @param logoPicFilePathAndName logo图片全名
     * @param exportFilePath 导出二维码图片路径
     * @param exportFileName 导出二维码图片名称
     * @param qRCodePictureTypeEnum 图片后缀枚举
     * @param width 图片宽度 
     * @return
     */ 
    public static String uploadLogoQRCode(String content,String logoPicFilePathAndName,String exportFilePath,String exportFileName,QRCodePictureTypeEnum qRCodePictureTypeEnum,int width){
        /*String qRCodeFormat = ""; 
        //判断文件类型
        if(qRCodePictureTypeEnum == QRCodePictureTypeEnum.GIF){
            qRCodeFormat = "gif";
        }else if(qRCodePictureTypeEnum == QRCodePictureTypeEnum.JPEG){
            qRCodeFormat = "jpeg";
        }else if(qRCodePictureTypeEnum == QRCodePictureTypeEnum.JPG){
            qRCodeFormat = "jpg";
        }else if(qRCodePictureTypeEnum == QRCodePictureTypeEnum.PNG){
            qRCodeFormat = "png";
        }*/
        
        File exportFilePathObj = new File(exportFilePath);
        if(!exportFilePathObj.exists()){
            exportFilePathObj.mkdirs();
        }
        //获取导出文件对象
        File exportFile = new File(exportFilePath+"\\"+exportFileName+"."+qRCodePictureTypeEnum.toString().toLowerCase());
        try{
            //获取图片流
            BufferedImage bim = MatrixToImageWriter.getQRCodeBufferedImage(content, BarcodeFormat.QR_CODE, width);
            //生成带logo的二维码图片
            return MatrixToImageWriter.addLogoToQRCode(bim, new File(logoPicFilePathAndName),exportFile,width);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }  
    
    /**
     * @functionName uploadLogoQRCode
     * @description 上传带logo的二维码图片 (支持图片宽度)
     * @param request
     * @param content 编码内容 :编码内容既可以为文字描述,也可以为链接,如果是链接,那么会自动跳转到相应的网页
     * @param logoPicFilePathAndName logo图片全名
     * @param exportFilePath 导出二维码图片路径
     * @param exportFileName 导出二维码图片名称
     * @param width 图片宽度 
     * @return
     */ 
    public static String uploadLogoQRCode(String content,String logoPicFilePathAndName,String exportFilePath,String exportFileName,int width){
        File exportFilePathObj = new File(exportFilePath);
        if(!exportFilePathObj.exists()){
            exportFilePathObj.mkdirs();
        }
        //获取导出文件对象
        File exportFile = new File(exportFilePath+"\\"+exportFileName+"."+DEFAULTQRCODEFORMAT);
        try{
            //获取图片流
            BufferedImage bim = MatrixToImageWriter.getQRCodeBufferedImage(content, BarcodeFormat.QR_CODE, width);
            //生成带logo的二维码图片
            return MatrixToImageWriter.addLogoToQRCode(bim, new File(logoPicFilePathAndName),exportFile,width);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
(3)测试

@RequestMapping("/downLoadLogoQRCodeImage")
public void downLoadLogoQRCodeImage(HttpServletResponse response) throws Exception {
String content = "https://www.baidu.com";
String logoPicFilePathAndName = "C:\\Users\\Administrator\\Desktop\\1111.jpg";
int QRPicWidth = 340;
String exportFilePath = "C:\\Users\\Administrator\\Desktop";
String exportFileName = "xxx";
String wordContent = "hello world!";
QRCodeUtils q = new QRCodeUtils();
//枚举的好处就是枚举项的类型还是枚举
q.downLoadLogoQRCodeImage(response, content, wordContent,logoPicFilePathAndName, exportFilePath, exportFileName,QRCodePictureTypeEnum.PNG,QRPicWidth);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值