package com.dy.common.util; 
 | 
  
 | 
import com.google.zxing.BarcodeFormat; 
 | 
import com.google.zxing.EncodeHintType; 
 | 
import com.google.zxing.client.j2se.MatrixToImageWriter; 
 | 
import com.google.zxing.common.BitMatrix; 
 | 
import com.google.zxing.qrcode.QRCodeWriter; 
 | 
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; 
 | 
  
 | 
import javax.imageio.ImageIO; 
 | 
import java.awt.*; 
 | 
import java.awt.image.BufferedImage; 
 | 
import java.io.File; 
 | 
import java.io.IOException; 
 | 
import java.util.HashMap; 
 | 
import java.util.Map; 
 | 
  
 | 
public class QrCodeTest2 { 
 | 
  
 | 
    public static void main(String[] args) throws Exception { 
 | 
        String imgpath = "D:\\demo\\"; 
 | 
        createq("1002001", "确定" , imgpath + "test2.jpg"); 
 | 
    } 
 | 
  
 | 
    private static void createq(String code, String name, String imgpath) throws Exception{ 
 | 
        //二维码大小 
 | 
        int size = 300; 
 | 
  
 | 
        //设置二维码参数 
 | 
        Map hints  = new HashMap(); 
 | 
        //设置UTF-8, 防止中文乱码 
 | 
        hints .put(EncodeHintType.CHARACTER_SET, "UTF-8"); 
 | 
        // 设置二维码的容错性 
 | 
        hints .put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); 
 | 
        //留白 默认4 
 | 
        hints .put(EncodeHintType.MARGIN, 1); 
 | 
        // 码版本,取值为 1 到 40 ,根据二维码内容决定最低版本 
 | 
        //param.put(EncodeHintType.QR_VERSION, 3); 
 | 
        //创建码位图 
 | 
        BitMatrix bitMatrix = new QRCodeWriter().encode(code, BarcodeFormat.QR_CODE, size, size, hints ); 
 | 
  
 | 
        //将生成码位图转换成BufferedImage 
 | 
        BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix); 
 | 
  
 | 
        int fontSize = 30 ; 
 | 
        //设置画布内边距 
 | 
        int padding = 5; 
 | 
        //画布大小 
 | 
        int combinedWidth = size + padding * 2 ; 
 | 
        int combinedHeight = size + padding * 2  + fontSize; 
 | 
  
 | 
        //创建空白BufferedImage 
 | 
        BufferedImage combined = new BufferedImage(combinedWidth, combinedHeight, BufferedImage.TYPE_INT_RGB); 
 | 
        //从BufferedImage对象中获取画布(Graphics2D)对象 
 | 
        Graphics2D graphics = combined.createGraphics(); 
 | 
        //设置渲染提示为启用文本抗锯齿 
 | 
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 
 | 
        //默认开启绘图抗锯齿 
 | 
        //graphics.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,RenderingHints.VALUE_STROKE_DEFAULT); 
 | 
        //设置空白画布整体背景颜色 
 | 
        GraphicsUtils.drawBackground(graphics,0, 0, combinedWidth, combinedHeight,Color.WHITE); 
 | 
  
 | 
        int linepadding = padding - 1 ; 
 | 
        //绘制矩形 
 | 
        GraphicsUtils.drawRectangle(graphics, linepadding, linepadding, 
 | 
                combinedWidth - linepadding * 2, 
 | 
                combinedHeight - linepadding * 2, Color.BLACK); 
 | 
  
 | 
        //二维码插入画布 
 | 
        GraphicsUtils.drawImage(graphics, qrImage, padding, padding); 
 | 
  
 | 
        //绘制头部字体 
 | 
        Font titleFont = new Font("宋体", Font.BOLD, fontSize); 
 | 
        GraphicsUtils.drawFont(graphics,titleFont,Color.BLUE); 
 | 
        FontMetrics fontMetrics = graphics.getFontMetrics(); 
 | 
        int fontWidth = fontMetrics.stringWidth(name); 
 | 
        int xName = ((combinedWidth - fontWidth) / 2); 
 | 
        int yName = combinedHeight - padding - fontSize/2 ; 
 | 
       // int yName = combinedHeight - padding - fontWidth/2 ; 
 | 
        GraphicsUtils.drawText(graphics, name, xName, yName, titleFont, Color.BLUE); 
 | 
  
 | 
        graphics.dispose(); 
 | 
  
 | 
        try { 
 | 
            ImageIO.write(combined, "jpg",new File(imgpath) ); 
 | 
        } catch (IOException e) { 
 | 
            e.printStackTrace(); 
 | 
        } 
 | 
  
 | 
    } 
 | 
} 
 |