以下代碼可以生成六行兩列的圖片,代碼改成了可以接受參數可循環的。
package com.xgt.util; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; public class ImgUtil { private static final Logger logger = LoggerFactory.getLogger(ImgUtil.class); public static Integer width = 500; public static Integer height = 500; public static void main(String[] args) { try { ImgUtil test = new ImgUtil(); // test.ImageTset(); } catch (Exception e) { System.out.print(e); } } /** * 注釋勿刪,測試有用 * 生成圖片,合成圖片 * @param fileDirectory * @param realnames * @throws Exception */ public static void ImageTset(String fileDirectory,String[] realnames) throws Exception { /*File _file1 = new File("D:\\QRCodePng\\奧特曼.png"); File _file2 = FontImageUtil.createJpgByFontAndAlign("奧特曼", "center", 32, 150, 50, Color.white, Color.black, new Font(null, Font.BOLD, 32), "D:\\QRCodePng\\奧特曼.jpg"); File _file3 = new File("D:\\QRCodePng\\谷阿莫.png"); File _file4 = FontImageUtil.createJpgByFontAndAlign("谷阿莫", "center", 32, 150, 50, Color.white, Color.black, new Font(null, Font.BOLD, 32), "D:\\QRCodePng\\谷阿莫.jpg"); File _file5 = new File("D:\\QRCodePng\\怪獸.png"); File _file6 = FontImageUtil.createJpgByFontAndAlign("怪獸", "center", 32, 150, 50, Color.white, Color.black, new Font(null, Font.BOLD, 32), "D:\\QRCodePng\\怪獸.jpg"); File _file7 = new File("D:\\QRCodePng\\藍胖.png"); File _file8 = FontImageUtil.createJpgByFontAndAlign("藍胖", "center", 32, 150, 50, Color.white, Color.black, new Font(null, Font.BOLD, 32), "D:\\QRCodePng\\藍胖.jpg"); File _file9 = new File("D:\\QRCodePng\\淑女.png"); File _file10 = FontImageUtil.createJpgByFontAndAlign("淑女", "center", 32, 150, 50, Color.white, Color.black, new Font(null, Font.BOLD, 32), "D:\\QRCodePng\\淑女.jpg"); File _file11 = new File("D:\\QRCodePng\\小草.png"); File _file12 = FontImageUtil.createJpgByFontAndAlign("小草", "center", 32, 150, 50, Color.white, Color.black, new Font(null, Font.BOLD, 32), "D:\\QRCodePng\\小草.jpg"); Image src1 = javax.imageio.ImageIO.read(_file1); Image src2 = javax.imageio.ImageIO.read(_file2); Image src3 = javax.imageio.ImageIO.read(_file3); Image src4 = javax.imageio.ImageIO.read(_file4); Image src5 = javax.imageio.ImageIO.read(_file5); Image src6 = javax.imageio.ImageIO.read(_file6); Image src7 = javax.imageio.ImageIO.read(_file7); Image src8 = javax.imageio.ImageIO.read(_file8); Image src9 = javax.imageio.ImageIO.read(_file9); Image src10 = javax.imageio.ImageIO.read(_file10); Image src11 = javax.imageio.ImageIO.read(_file11); Image src12 = javax.imageio.ImageIO.read(_file12); //獲取圖片的寬度 int width = src1.getWidth(null); //獲取圖片的高度 int height = src1.getHeight(null);*/ //構造一個類型為預定義圖像類型之一的 BufferedImage。 寬度為第一只的寬度,高度為各個圖片高度之和 BufferedImage tag = new BufferedImage(width + width, 4*height, BufferedImage.TYPE_INT_RGB); //創建輸出流 FileOutputStream out = new FileOutputStream("D:\\QRCodePng\\合成圖.png"); //繪制合成圖像 Graphics g = tag.createGraphics(); /** * 此方法最多六行兩列,6張二維碼圖和對應的6張文字圖 * 如需擴展,要重新調整尺寸 * x=0,2,4,6,8... * x對4取余為0存在線性關系,y=x/3 * x對4取余不為0存在線性關系,y=(x-2)/3 * x=1,3,5,7,9... * x對4取余為1存在線性關系,y=(4x+2)/3 * x對4取余不為1存在線性關系,y=x/3 */ for(int i=0;i<realnames.length*2;i++){ if(i%2==0) { File _file = new File(fileDirectory,realnames[i/2]+".png"); Image src = javax.imageio.ImageIO.read(_file); if(i%4==0){ g.drawImage(src, 0, i*height/3, width, height, null); }else { g.drawImage(src, width, (i-2)*height/3, width, height, null); } }else{ File _file = FontImageUtil.createJpgByFontAndAlign(realnames[(i-1)/2], "center", 32, 150, 50, Color.white, Color.black, new Font(null, Font.BOLD, 32), fileDirectory+"\\"+realnames[(i-1)/2]+".jpg"); Image src = javax.imageio.ImageIO.read(_file); if(i%4==1){ g.drawImage(src, 0, (i+2)*height/3, width, height/3, null); }else { g.drawImage(src, width, i*height/3, width, height/3, null); } } } /*g.drawImage(src1, 0, 0, width, height, null); g.drawImage(src2, 0, height, width, height/3, null); g.drawImage(src3, width, 0, width , height, null); g.drawImage(src4, width, height, width, height/3, null); g.drawImage(src5, 0, 4*height/3, width, height, null); g.drawImage(src6, 0, 7*height/3, width, height/3, null); g.drawImage(src7, width, 4*height/3, width, height, null); g.drawImage(src8, width, 7*height/3, width , height/3, null); g.drawImage(src9, 0, 8*height/3, width, height, null); g.drawImage(src10, 0, 11*height/3, width, height/3, null); g.drawImage(src11, width, 8*height/3, width, height, null); g.drawImage(src12, width, 11*height/3, width, height/3, null);*/ // 釋放此圖形的上下文以及它使用的所有系統資源。 g.dispose(); //將繪制的圖像生成至輸出流 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); //關閉輸出流 out.close(); logger.info("合成圖出來了"); } }
工具類FontImageUtil.java
package com.xgt.util; import com.google.zxing.EncodeHintType; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.xgt.dto.ImgQrcodeDto; import com.xgt.entity.BuildLabourer; import com.xgt.service.BuildLabourerService; import net.glxn.qrgen.core.image.ImageType; import net.glxn.qrgen.javase.QRCode; import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.imageio.ImageIO; public class FontImageUtil { private static final Logger logger = LoggerFactory.getLogger(FontImageUtil.class); public static void main(String[] args) throws Exception { //createImage("谷阿莫", new Font("微軟雅黑", Font.PLAIN, 32), new File("d:/a.png"), 150, 50); createJpgByFontAndAlign("生成圖片", "right", 32, 150, 50, Color.white, Color.black, new Font(null, Font.BOLD, 32), "D:/right.jpg"); createJpgByFontAndAlign("生成圖片", "center", 32, 150, 50, Color.white, Color.black, new Font(null, Font.BOLD, 32), "D:/center.jpg"); createJpgByFontAndAlign("生成圖片", "left", 32, 150, 50, Color.white, Color.black, new Font(null, Font.BOLD, 32), "D:/left.jpg"); }/** * 根據提供的文字生成jpg圖片 * * @param s * String 文字 * @param align * 文字位置(left,right,center) * @param y * y坐標 * @param width * 圖片寬度 * @param height * 圖片高度 * @param bgcolor * Color 背景色 * @param fontcolor * Color 字色 * @param font * Font 字體字形字號 * @param jpgname * String jpg圖片名 * @return */ public static File createJpgByFontAndAlign(String s, String align, int y, int width, int height, Color bgcolor, Color fontcolor, Font font, String jpgname) { try { // 寬度 高度 BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = bimage.createGraphics(); g.setColor(bgcolor); // 背景色 g.fillRect(0, 0, width, height); // 畫一個矩形 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 去除鋸齒(當設置的字體過大的時候,會出現鋸齒) g.setColor(fontcolor); // 字的顏色 g.setFont(font); // 字體字形字號 int size = font.getSize(); //文字大小 int x = 5; if(align.equals("left")){ x = 5; } else if(align.equals("right")){ x = width - size * s.length() - 5; } else if(align.equals("center")){ x = (width - size * s.length())/2; } g.drawString(s, x, y); // 在指定坐標除添加文字 g.dispose(); FileOutputStream out = new FileOutputStream(jpgname); // 指定輸出文件 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage); param.setQuality(50f, true); encoder.encode(bimage, param); // 存盤 out.flush(); out.close(); } catch (Exception e) { System.out.println("createJpgByFont Failed!"); e.printStackTrace(); } return new File(jpgname); } }