1. 程式人生 > >java 文字生成圖片

java 文字生成圖片

直接上程式碼:

import java.awt.*;   
import java.awt.image.*;   
import java.awt.font.*;   
import java.awt.geom.*; 
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

import javax.imageio.ImageIO;

/**
 * 建立文字圖片
 * @author yuki_ho
 *
 */
public class FontImage {
	 // 預設格式
	 private static final String FORMAT_NAME = "JPG";
	 // 預設 寬度
	 private static final int WIDTH = 100;
	 // 預設 高度
	  private static final int HEIGHT =100;
	       
	  /**
	   * 建立圖片
	   * @param content 內容
	   * @param font  字型
	   * @param width 寬
	   * @param height 高
	   * @return
	   */
	 private static BufferedImage createImage(String content,Font font,Integer width,Integer height){  
	        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);   
	        Graphics2D g2 = (Graphics2D)bi.getGraphics();   
	        g2.setBackground(Color.WHITE);   
	        g2.clearRect(0, 0, width, height);   
	        g2.setPaint(Color.BLACK);   
	           
	        FontRenderContext context = g2.getFontRenderContext();   
	        Rectangle2D bounds = font.getStringBounds(content, context);   
	        double x = (width - bounds.getWidth()) / 2;   
	        double y = (height - bounds.getHeight()) / 2;   
	        double ascent = -bounds.getY();   
	        double baseY = y + ascent;   
	           
	        g2.drawString(content, (int)x, (int)baseY);   
	        
	        return bi;
	 }
	 
	 /**
	  * 獲取 圖片 
	  * @param content 內容
	  * @param font  字型
	  * @param width 寬
	  * @param height 高
	  * @return
	  */
	 public static BufferedImage getImage(String content,Font font,Integer width,Integer height){
		width=width==null?WIDTH:width;
		height=height==null?HEIGHT:height;
		if(null==font)
			font = new Font("Serif", Font.BOLD, 11);   
		 return createImage(content, font, width, height);
	 }
	 
	 /**
	  * 獲取 圖片
	  * @param content 內容
	  * @param width 寬
	  * @param height 高
	  * @return
	  */
	 public static BufferedImage getImage(String content,Integer width,Integer height){

		 return getImage(content, null,width, height);
	 }
	 
	 /**
	  * 獲取圖片
	  * @param content 內容
	  * @return
	  */
	 public static BufferedImage getImage(String content){

		 return getImage(content, null, null);
	 }
	 
	 /**
	  *  獲取圖片
	  * @param content 內容
	  * @param font 字型
	  * @param width 寬
	  * @param height 高
	  * @param destPath 輸出路徑
	  * @throws IOException 
	  */
	 public static void getImage(String content,Font font ,Integer width,Integer height,String destPath) throws IOException{
	     mkdirs(destPath);
	     String file = UUID.randomUUID().toString()+".jpg";
		 ImageIO.write(getImage(content,font,width,height),FORMAT_NAME, new File(destPath+"/"+file));  
	 }
	
	 /**
	  * 獲取圖片
	  * @param content 內容
	  * @param font 字型
	  * @param width 寬
	  * @param height 高
	  * @param output 輸出流
	  * @throws IOException
	  */
	 public static void getImage(String content,Font font,Integer width,Integer height, OutputStream output) throws IOException{
		 ImageIO.write(getImage(content,font,width,height), FORMAT_NAME, output);  
	 }
	 
	 /**
	  * 獲取圖片
	  * @param content 內容
	  * @param width 寬
	  * @param height 高
	  * @param destPath 輸出路徑
	  * @throws IOException
	  */
	 public static void getImage(String content,Integer width,Integer height,String destPath) throws IOException{
		getImage(content, null, width, height, destPath);
	 }
	
	 /**
	  * 獲取圖片
	  * @param content 內容
	  * @param width 寬
	  * @param height 高
	  * @param output 輸出流
	  * @throws IOException
	  */
	 public static void getImage(String content,Integer width,Integer height, OutputStream output) throws IOException{
		getImage(content, null, width, height, output);
	 }
	 

	   /**
	    * 建立 目錄
	    * @param destPath
	    */
	   public static void mkdirs(String destPath) {
	        File file =new File(destPath);   
	        //當資料夾不存在時,mkdirs會自動建立多層目錄,區別於mkdir.(mkdir如果父目錄不存在則會丟擲異常)
	        if (!file.exists() && !file.isDirectory()) {
	            file.mkdirs();
	        }
	    }
	 
	 public static void main(String[] args) throws Exception {
		 getImage("MAS-123456", 100, 100, "d:/test");

	}
}

可以配合二維碼 一起使用:地址