java在圖片上寫入文字-IDNo身份證生成方法

一、IDCardGenerate.java(接口)--//接口的定義,接口裏只包含常量和抽象方法.java

public interface IDCardGenerate {
	//public static final String outPath = "D:/gen/idcard.png";
	//public static final String outDir = "D:/gen/";
	public static final String outPath = ".\\idcard.png";//這個是臨時文件名
	public static final String outDir = ".\\";
	public static final String outSuf = ".jpg";
	public static final int canvasWidth = 480;
	public static final int canvasHeight = 320;
	public static final int imgWidth = 270;
	public static final int imgHeigth = 428;
	public static final int xStart = 60;
	public static final int yStart = 85;
	public static final int yStep = 32;
	public static final int xStep = 90;
	public static final int charSpace = 10;
	public static final int paramXStart = 110;
	public static final int nationSpace = 140;
	public static final int monthSpace = 120;
	public static final int daySpace = 60;
	public static final int idNoSpace = 80;
	public static final int charLineSpace = 25;
	
	
	public void paint();
}

 

二、IDCardInfo.javacanvas

public class IDCardInfo {
	private String name = "";
	private String sex = "";
	private String nation = "";
	private String bornYear = "";
	private String bornMonth = "";
	private String bornDay = "";
	private String addr = "";
	private String idNo = "";
	private String expDate = "";
	private String issuingAuthority = "";
	private String addr1 = "";
	private String addr2 = "";
	
	
	
	public String getAddr1() {
		addr1 = addr.length() > 11 ? 
				addr.substring(0,11) : "";
		return addr1;
	}
	
	public String getAddr2() {
		addr2 = addr.length() > 11 ? 
				addr.substring(11) : "";
		return addr2;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getNation() {
		return nation;
	}
	public void setNation(String nation) {
		this.nation = nation;
	}
	public String getBornYear() {
		return bornYear;
	}
	public void setBornYear(String bornYear) {
		this.bornYear = bornYear;
	}
	public String getBornMonth() {
		return bornMonth;
	}
	public void setBornMonth(String bornMonth) {
		this.bornMonth = bornMonth;
	}
	public String getBornDay() {
		return bornDay;
	}
	public void setBornDay(String bornDay) {
		this.bornDay = bornDay;
	}
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	public String getIdNo() {
		return idNo;
	}
	public void setIdNo(String idNo) {
		this.idNo = idNo;
	}
	public String getExpDate() {
		return expDate;
	}
	public void setExpDate(String expDate) {
		this.expDate = expDate;
	}
	public String getIssuingAuthority() {
		return issuingAuthority;
	}
	public void setIssuingAuthority(String issuingAuthority) {
		this.issuingAuthority = issuingAuthority;
	}
}

 

三、IDCardGeneratorFront.javaide

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;



public class IDCardGeneratorFront implements IDCardGenerate {
	private IDCardInfo idCard = null;
	
	@SuppressWarnings("unused")
	private IDCardGeneratorFront(){
	}
	
	public IDCardGeneratorFront(IDCardInfo idCard) throws Exception{
		if(null == idCard){
			throw new Exception("IDCardinfo object can not be null!");
		}
		this.idCard = idCard;
	}
	
	@Override
	public void paint(){
		BufferedImage bi = new BufferedImage(canvasWidth, canvasHeight, 
				BufferedImage.TYPE_INT_ARGB);//構造一個類型爲預約義圖像類型之一的 BufferedImage
		
		Graphics2D g2d = getTemplateFront(bi);
		Font font = new Font("華文細黑",Font.BOLD,16);//可不定義
		g2d.setPaint(Color.black);//給寫入的我的信息文本設置爲黑色
		//g2d.setFont(font);
		int ypStep = 0;
		int xpStep = 0;
		//姓名參數
		g2d.drawString(idCard.getName(), paramXStart, yStart);
		//性別參數
		ypStep = yStart + yStep;
		g2d.drawString(idCard.getSex(), paramXStart, ypStep);
		
		//民族參數
		xpStep = xStart + nationSpace;
		g2d.drawString(idCard.getNation(), xpStep, ypStep);
		
		//出生參數
		//幄1�7
		ypStep += yStep;
		g2d.drawString(idCard.getBornYear(), paramXStart, ypStep);
		//朄1�7
		xpStep = xStart + monthSpace;
		g2d.drawString(idCard.getBornMonth(), xpStep, ypStep);
		//旄1�7
		xpStep += daySpace;
		g2d.drawString(idCard.getBornDay(), xpStep, ypStep);
		
		//住址參數
		ypStep += yStep;
		g2d.drawString(idCard.getAddr1(), paramXStart, ypStep);
		//地址換行處理
		g2d.drawString(idCard.getAddr2(), paramXStart, ypStep + charLineSpace);
		
		
		//身份證號
		ypStep += idNoSpace;
		xpStep = xpStep - daySpace;
		//font = new Font(Font.MONOSPACED,Font.BOLD,20);
		//g2d.setFont(font);
		g2d.drawString(idCard.getIdNo(), xpStep, ypStep + 10);
		
		g2d.dispose();
		try {
			ImageIO.write(bi, "PNG", new File(outPath));// 使用支持給定格式的任意 ImageWriter 將一個圖像寫入 File
			File imgFile = new File(outPath);//經過將給定路徑名字符串轉換爲抽象路徑名來建立一個新 File 實例。
			String imgName = String.valueOf(System.currentTimeMillis());
			imgFile.renameTo(new File(outDir + imgName + outSuf));//從新命名此抽象路徑名錶示的文件。
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			
		} 
		
	}	
	private Graphics2D getTemplateFront(BufferedImage bi) {//該方法是用來獲取模板樣式
		
		Graphics2D g2d = bi.createGraphics();
		int ypStep = 0;
		int xpStep = 0;
		Font font = new Font("黑體",Font.BOLD,14);
		
		//設置背景艄1�7
	  	g2d.setBackground(Color.white);//白色背景
	  	g2d.clearRect(0, 0, canvasWidth, canvasWidth);
	    //g2d.fillRect(0, 0, canvasWidth, canvasWidth);
	    
	    
	    //繪製圓角矩形
	    g2d.setPaint(Color.black);//矩形邊框顏色
		RoundRectangle2D rr = new RoundRectangle2D.Double(30,30,428,270,20,20);//設置圓角
		g2d.draw(rr);//要呈現的輪廓
		
		//姓名
		g2d.setPaint(Color.BLUE);//這是爲模板字體設置爲藍色
		//g2d.setFont(font);//調試用的
		//姓名
		g2d.drawString("姓名", xStart, yStart);//首字符的基線位於用戶空間的1�7 (x, y) 位置處1�7
		
		ypStep = yStart + yStep;
		//性 別
		g2d.drawString("性 別", xStart, ypStep);
		//pStep += yStep;
		//民 族
		xpStep =xStart + xStep;
		g2d.drawString("民 族",xpStep , ypStep);
		ypStep += yStep;
		//出 生
		g2d.drawString("出 生", xStart, ypStep);
		//ypStep += yStep;
		xpStep = xStart + xStep;
		//年
		g2d.drawString("年", xpStep, ypStep);
		//月
		xpStep += xStart;
		g2d.drawString("月", xpStep, ypStep);
		//日
		xpStep += xStart;
		g2d.drawString("日", xpStep, ypStep);
		//住 址
		ypStep += yStep;
		g2d.drawString("住 址", xStart, ypStep);
		ypStep += idNoSpace;
		//號碼
		font = new Font("黑體",Font.PLAIN,16);//這裏設置顏色有什麼用?
		g2d.setFont(font);
		g2d.drawString("公民身份號碼", xStart, ypStep + 10);
		//設置頭像
		try {
			//Image img = ImageIO.read(new File("D:/gen/qie2.png"));
			Image img = ImageIO.read(new File(".\\qie2.png"));
			g2d.drawImage(img, 300,70,null);
		} catch (IOException e) {	
		}
		
		
		return g2d;
//		g2d.dispose();
//		
//		try {
//			ImageIO.write(bi, "PNG", new File(outPath));
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally{
//			
//		}  
	}
}

四、TestGenCard.java(程序入口)字體

public class TestGenCard {
	public static void main(String[] args) {
		IDCardInfo idCard = new IDCardInfo();
		idCard.setName("趙一鵬");
		idCard.setSex("男");
		idCard.setNation("漢");
		idCard.setBornYear("1999");
		idCard.setBornMonth("8");
		idCard.setBornDay("8");
		idCard.setAddr("陝西省渭南市大荔縣黃河鎮黃家溝二組");
		idCard.setIdNo("622426199908080018");
		try {
			IDCardGeneratorFront idCardFront = new IDCardGeneratorFront(idCard);
			idCardFront.paint();
			System.out.println("complete!!!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

執行方法:進入當前java文件目錄,打開命令窗口,一、執行javac TestGenCard.java進行編譯(若出現亂碼,則執行javac -encoding utf-8 TestGenCard.java),二、java TestGenCard執行java文件運行成功ui

相關文章
相關標籤/搜索