Java将字符串生成二维码并下载图片

前言

今天记录一下最近写的下载二维码的代码,实现类似微信样式的二维码。主要是先生成二维码,再用Java的Graphics2D画图添加边框样式和文字,最后下载图片。

具体实现

  1. 添加依赖包
		
		    com.google.zxing
		    core
		    3.3.0
		
		
		    com.google.zxing
		    javase
		    3.3.0
		
  1. 代码引入包
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
  1. 具体方法代码
public void exportQuickResponseCode(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String msg = "好孩子,你还扫这个码";
        String filename = "123.png";
        String path = "D:/1000/" + filename;
        String orgText = "中华人民共和国人民的二维码";
        String goodsNameText = "送你一亿二维码";
        try {
            File file=new File(path);
            OutputStream ous=new FileOutputStream(file);
            Map map =new HashMap();
            //设置编码 EncodeHintType类中可以设置MAX_SIZE, ERROR_CORRECTION,CHARACTER_SET,DATA_MATRIX_SHAPE,AZTEC_LAYERS等参数
            map.put(EncodeHintType.CHARACTER_SET,"UTF-8");
            map.put(EncodeHintType.MARGIN,"2");
            //生成二维码
            BitMatrix bitMatrix = new MultiFormatWriter().encode(msg, BarcodeFormat.QR_CODE,600,600,map);
            MatrixToImageWriter.writeToStream(bitMatrix,"png",ous);
            
            // 绘制图片
            BufferedImage backgroundImage = new BufferedImage(1000, 1100, BufferedImage.TYPE_INT_RGB);
            BufferedImage image = ImageIO.read(file);
            Graphics2D rng=backgroundImage.createGraphics();
            rng.setColor( new Color(30, 171, 58));//设置笔刷颜色
            rng.fillRect(0,0,1000,1100);
            rng.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP));
            rng.drawImage(image,200,250,600,600,null);
            // 绘制文字
            rng.setColor(Color.white);
            rng.setFont(new Font("黑体",Font.BOLD,46));
            rng.drawString(orgText,(1000-13*46)/2,220);
            rng.drawString(goodsNameText,(1000-7*46)/2,900);
            
            rng.dispose();
            image = backgroundImage;
            image.flush();
            ImageIO.write(image, "png", file);
            
            // 以下是下载图片
			// 当文件名不是英文名的时候,最好使用url解码器去编码一下,
			filename=URLEncoder.encode(filename,"UTF-8");
			// 将响应的类型设置为图片
			response.setContentType("image/png");
			response.setHeader("Content-Disposition", "attachment;filename=" + filename);
			FileInputStream fileInputStream = new FileInputStream(file);
			OutputStream out = response.getOutputStream();
			int len = 0;
			byte[] by = new byte[1024*10];
			while ((len = fileInputStream.read(by))>0){
				out.write(by,0,len);
			}
			out.close();
			fileInputStream.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
	}

后记

代码分为三部分,第一部分生成二维码,如果只需要二维码,第一部分就足够了,第二部分是绘制外边绿色边框和文字,第三部分是下载图片。

最终图片
Java将字符串生成二维码并下载图片_第1张图片

你可能感兴趣的:(后端开发,java)