| package com.dy.common.util; | 
|   | 
| import com.google.zxing.BarcodeFormat; | 
| import com.google.zxing.EncodeHintType; | 
| import com.google.zxing.WriterException; | 
| 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 QrCodeTest1 { | 
|   | 
|     public static void main(String[] args) throws Exception { | 
|   | 
|         String[] text = {"12345678;超声波水表;民旺道10号;大禹研究院;020-12345678;106.872615383,11.466578703"}; | 
|   | 
|         String imgpath = "D:\\demo\\"; | 
|   | 
|         for (int i = 0; i < text.length; i++) { | 
|   | 
|             String[] s = text[i].split(";"); | 
|   | 
|             String[] lines = {"设备编号:" + s[0], "设备类型:" + s[1], "道路名称:" + s[2], "管护单位:" + s[3], "报修电话:" + s[4]}; | 
|             String[] line = {s[0], s[1], s[2], s[3], s[4]}; | 
|   | 
|             createq(lines, line, imgpath + "test.jpeg"); | 
|         } | 
|     } | 
|   | 
|     static void createq(String[] lines, String[] line, String imgpath) { | 
|   | 
|         String qrCodeText = line[0]; | 
|   | 
|         //二维码大小 | 
|         int size = 400; | 
|   | 
|         //画布大小 | 
|         int combinedWidth = size + 800; | 
|         int combinedHeight = size + 400 ; | 
|   | 
|         //标题高度 | 
|         int topsize = 160; | 
|   | 
|         //设置标题y坐标 | 
|         int yTitle = 110; | 
|   | 
|         //设置画布内边距 | 
|         int padding = 5; | 
|   | 
|         //设置标题内容 | 
|         String title = "大禹集团"; | 
|   | 
|         //设置图片dpi | 
|         int dpi = 300; | 
|   | 
|         Color blue = new Color(75, 125, 178); | 
|   | 
|         //设置二维码参数 | 
|         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 ,根据二维码内容决定最低版本 | 
|         hints.put(EncodeHintType.QR_VERSION, 3); | 
|   | 
|         //创建码位图 | 
|         BitMatrix bitMatrix = null; | 
|         try { | 
|             bitMatrix = new QRCodeWriter().encode(qrCodeText, BarcodeFormat.QR_CODE, size, size, hints); | 
|         } catch (WriterException e) { | 
|             e.printStackTrace(); | 
|         } | 
|   | 
|         //将生成码位图转换成BufferedImage | 
|         BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix); | 
|   | 
|         //创建空白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); | 
|   | 
|         //绘制表格画线 | 
|         //绘制线在内边距内,不影响其它内容,-1操作 | 
|         int linepadding = padding - 1 ; | 
|         //绘制矩形 | 
|         GraphicsUtils.drawRectangle(graphics,linepadding,linepadding,combinedWidth-linepadding*2,combinedHeight-linepadding*2,Color.BLACK); | 
|         //绘制内三横线 | 
|         GraphicsUtils.drawLine(graphics,linepadding,padding+topsize+1,combinedWidth-linepadding,padding+topsize+1,Color.BLACK); | 
|         GraphicsUtils.drawLine(graphics,linepadding,padding+topsize+size+2,combinedWidth-linepadding,padding+topsize+size+2,Color.BLACK); | 
|         GraphicsUtils.drawLine(graphics,linepadding,combinedHeight-(combinedHeight-padding*2-topsize-size+2)/2 - padding,combinedWidth-linepadding,combinedHeight-(combinedHeight-padding*2-topsize-size+2)/2 - padding,Color.BLACK); | 
|         //绘制二维码竖线及右边2横线 | 
|         GraphicsUtils.drawLine(graphics,padding+size+1,padding+topsize+1,padding+size+1,padding+topsize+size+1,Color.BLACK); | 
|         GraphicsUtils.drawLine(graphics,padding+size+1,padding+topsize+1+size/3,combinedWidth-linepadding,padding+topsize+1+size/3,Color.BLACK); | 
|         GraphicsUtils.drawLine(graphics,padding+size+1,padding+topsize+1+size/3*2,combinedWidth-linepadding,padding+topsize+1+size/3*2,Color.BLACK); | 
|   | 
|         //该方法用于绘制头部绘制矩形并填充指定颜色 | 
|         GraphicsUtils.drawFillRect(graphics,padding, padding, combinedWidth-(padding*2), topsize,blue); | 
|   | 
|         //二维码插入画布 | 
|         GraphicsUtils.drawImage(graphics,qrImage, padding, topsize+padding+2); | 
|   | 
|         //该方法用于绘制头部绘制矩形并填充指定颜色 | 
|         GraphicsUtils.drawBackground(graphics,padding, padding, combinedWidth-(padding*2), topsize,blue); | 
|   | 
|         //绘制头部字体 | 
|         Font titleFont = new Font("宋体", Font.BOLD, 70); | 
|         GraphicsUtils.drawFont(graphics,titleFont,Color.white); | 
|         FontMetrics fontMetrics = graphics.getFontMetrics(); | 
|         int fontWidth = fontMetrics.stringWidth(title); | 
|         int xTitle = ((combinedWidth - fontWidth) / 2); | 
|         GraphicsUtils.drawText(graphics,title,xTitle, yTitle+padding,titleFont,Color.white); | 
|   | 
|         //绘制非头部字体 | 
|   | 
|         //设置字体及颜色 | 
|         Font downFont = new Font("宋体", Font.BOLD, 50); | 
|         GraphicsUtils.drawFont(graphics,downFont,Color.BLACK); | 
|   | 
|         fontMetrics = graphics.getFontMetrics(); | 
|   | 
|         int fontHeight  = fontMetrics.getHeight(); | 
|   | 
|         for (int i = 0; i < lines.length; i++) { | 
|   | 
|             if (i < 3){ | 
|                 graphics.drawString(lines[i], size+padding+5, topsize+padding+ (size/3) * i + (size/3+fontHeight)/2 ); | 
|             }else if (i == 3){ | 
|                 int test =  combinedHeight-(combinedHeight-padding*2-topsize-size)/4*3 - padding + fontHeight/2 ; | 
|                 graphics.drawString(lines[i], 5+padding, test); | 
|             } | 
|             else if (i == 4){ | 
|                 int test =  combinedHeight-(combinedHeight-padding*2-topsize-size)/4 - padding + fontHeight/2 ; | 
|                 graphics.drawString(lines[i], 5+padding, test); | 
|             } | 
|         } | 
|   | 
|         graphics.dispose(); | 
|   | 
|         try { | 
|             ImageIO.write(combined, "jpeg",new File(imgpath) ); | 
|         } catch (IOException e) { | 
|             e.printStackTrace(); | 
|         } | 
|   | 
|     } | 
|   | 
| } |