基於MVC圖片水印的源碼分析

public interface WaterMark {
	
	public static final String FONT_NAME = "華文新魏";
	public static final int FONT_STYLE = Font.CENTER_BASELINE;	//斜體
	public static final int FONT_SIZE = 15;			//文字大小
	public static final Color FONT_COLOR = Color.black;//文字顏色
	

	public static String confirm ="本人已確認以上配置"; // 
	
	public static float ALPHA = 0.6F; //文字水印透明度 
	
	/*
	 * 添加文字水印
	 */
   public boolean addWaterMark(String contractPath,String contractBuildPath,String realname,String type);

}
public class WaterMarkImpl implements WaterMark{

	/*
	 * 添加文字水印
	 */
	public boolean addWaterMark(String contractPath,String contractBuildPath,String realname,String type){

		OutputStream os=null;
		
		//獲取原圖文件
		File img=new File(contractPath);
		System.out.println(img.getAbsolutePath());
		try {
			//1.創建圖片緩存對象
			Image image=ImageIO.read(img);
		    int width=image.getWidth(null);
			int height=image.getHeight(null);
			
			BufferedImage bufImg=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			
			//2.創建Java繪圖工具對象
			Graphics2D g=bufImg.createGraphics();
			
			//3.使用繪圖工具對象將原圖繪製到圖片緩存對象
			g.drawImage(image, 0, 0, width, height,null);
			
			//4.使用繪圖工具對象將水印(文字或圖片)繪製到圖片緩存對象
			
			/*GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
			String[] fontFamilies = ge.getAvailableFontFamilyNames();
			for (String s : fontFamilies) {
					System.out.println(s);
			}*/
			
			//g.setFont(new Font("latarcyrheb-sun16",FONT_STYLE,FONT_SIZE));
			//g.setFont(new Font(FONT_NAME,FONT_STYLE,FONT_SIZE));
			g.setColor(FONT_COLOR);
			//水印透明度的設置
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,ALPHA));
			//開始繪製
			switch(type){
			case "contract":
				
				g.setFont(new Font(FONT_NAME,FONT_STYLE,FONT_SIZE));
				
				g.drawString(realname, 300, 160);
				g.drawString(realname,630,965);
				
				//獲取系統時間
				Calendar c=Calendar.getInstance();
				int year=c.get(Calendar.YEAR);
				int day=c.get(Calendar.DATE);
				int month=c.get(Calendar.MONTH)+1;
				g.drawString(year+"", 300, 645);
				g.drawString(month+"", 400, 645);
				g.drawString(day+"", 456, 645);
				
				g.drawString(year+"", 595, 1105);
				g.drawString(month+"", 680,1105 );
				g.drawString(day+"", 760, 1105);
				
				
				break;
			case "config":
				
				g.setFont(new Font(FONT_NAME,FONT_STYLE,24));
				
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy.MM.dd");
				Date date=new Date();
		        
				g.drawString(confirm,(float)(width*0.6),(float)(height*0.8));
				g.drawString(realname,(float)(width*0.62),(float)(height*0.84));
				g.drawString(sdf.format(date),(float)(width*0.69),(float)(height*0.84));
				break;
			}
			//釋放工具
			g.dispose();
			
			//創建文件輸出流,輸出指向最終的文件
			os=new FileOutputStream(contractBuildPath);
			
			//5.創建圖像文件編碼工具類
			//JPEGImageEncoder  en=JPEGCodec.createJPEGEncoder(os);
			
			//6 使用圖像編碼工具類,輸出緩存圖像到目標文件
			//en.encode(bufImg);
			
			ImageIO.write(bufImg, "jpg", os);
			
			
			
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}finally{
			if(os!=null){
				try {
					os.close();	//關閉流
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		}
		return true;	
	}

}



項目演示:點擊打開鏈接

源碼地址:點擊打開鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章