【Java】------- Java实现多个二维码图片生成并存入Excel文件中然后下载(可以添加图标,和文字)

1.Java excel 生成多个二维码(java  springboot框架 )

javaexcel生成二维码(可以添加图标和文字).zip_Java生成二维码写入excel-Java文档类资源-CSDN下载1.使用技术:Java,SpringBoot;2.资源内容:实现生成二维码存放在Excel中并进行Java生成二维码写入excel更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/qq_38366657/11664701

(1) 第一步: pom.xml 配置文件配置参数如下:

        
		
			com.google.zxing
			core
			${zxing.version}
			compile
			true
		

(2)第二步: 创建 qcode.java 类(主要代码)

package com.imx.qrcode;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.yd.edb.entity.IMXResponse;
import com.yd.edb.ticket.Ticket;
//import org.apache.poi.common.usermodel.fonts.FontInfo;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.ClientAnchor;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;

import java.util.HashMap;


/**
 * Created by liyoubing on 2019-09-03.
 */
public class qrcode {

    /**
     * 根据自定义的内容和图片名称生成二维码图片
     * 参考:https://blog.csdn.net/amberwangfeng/article/details/78657889
     */
    public static IMXResponse BAPI_qrcode_sc(Ticket ticket,String data) {
        IMXResponse ret=new IMXResponse();
        try {


            FileOutputStream fileOut = null;

            String content = "";
            boolean flag = false;
            int width = 300;
            int height = 380;
            String format = "png";
            String text = "";
            int font = 10; //字体大小
            int fontStyle = 1; //字体风格

            //定义二维码的参数
            HashMap hashMap = new HashMap();
            hashMap.put(EncodeHintType.CHARACTER_SET, "utf-8");
            hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
            hashMap.put(EncodeHintType.MARGIN, 4);

            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet1 = wb.createSheet("二维码");
            sheet1.setColumnWidth(0, 45*256);

            sheet1.setDefaultRowHeight((short)(24*20/32*400));
            //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
            HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();

            try {
                JSONArray job=JSONObject.parseArray(data);
                if(job.size()>0){
                    BufferedImage image=null;
                    for(int i=0;i

第三步: 添加图标的类: LoginConfig.java

package com.imx.qrcode;

import java.awt.*;

/**
 * Created by liyoubing on 2019-09-03.
 */
public class LogoConfig {
    // logo默认边框颜色
    public static final Color DEFAULT_BORDERCOLOR = Color.WHITE;
    // logo默认边框宽度
    public static final int DEFAULT_BORDER = 2;
    // logo大小默认为二维码照片的1/6
    public static final int DEFAULT_LOGOPART = 6;

    private final int border = DEFAULT_BORDER;
    private final Color borderColor;
    private final int logoPart;

    /**
     * Creates a default config with on color {@link #BLACK} and off color
     * {@link #WHITE}, generating normal black-on-white barcodes.
     */
    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;
    }

}

3. 二维码生成类:MatrixToImageWriter.java

package com.imx.qrcode;

 import java.awt.image.BufferedImage;

 import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;

 import javax.imageio.ImageIO;

 import java.util.Hashtable;

 import com.google.zxing.common.BitMatrix;
 import com.google.zxing.BarcodeFormat;
 import com.google.zxing.EncodeHintType;
 import com.google.zxing.MultiFormatWriter;

 /**
 * 二维码的生成需要借助MatrixToImageWriter类,该类是由Google提供的,可以将该类直接拷贝到源码中使用
 */
public class MatrixToImageWriter {
     private static final int BLACK = 0xFF000000;
     private static final int WHITE = 0xFFFFFFFF;

     private MatrixToImageWriter() {
     }

     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 - 50; y++) {
                 image.setRGB(x, y, matrix.get(x, y + 50) ? BLACK : WHITE);
             }
         }
         return image;
     }


     public static BufferedImage writeToFile(BitMatrix matrix, String format)
             throws IOException {
         BufferedImage image = toBufferedImage(matrix);
         return image;
     }


     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 void main(String[] args) throws Exception {
//        String text = "http://www.baidu.com"; // 二维码内容
//        int width = 300; // 二维码图片宽度
//        int height = 300; // 二维码图片高度
//        String format = "jpg";// 二维码的图片格式
//
//        Hashtable hints = new Hashtable();
//        hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 内容所使用字符集编码
//
//        BitMatrix bitMatrix = new MultiFormatWriter().encode(text,
//                BarcodeFormat.QR_CODE, width, height, hints);
//        // 生成二维码
//        File outputFile = new File("d:" + File.separator + "new.jpg");
//        MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
//    }
}

4. 实例效果图:

【Java】------- Java实现多个二维码图片生成并存入Excel文件中然后下载(可以添加图标,和文字)_第1张图片

你可能感兴趣的:(#,Java,Java,excel,生成多个二维码(可以添加图标,和文字))