以下是本人實現的一個Java水印圖片生成公共類:
package com.sunfairs.util; import java.awt.AlphaComposite; import java.awt.Font; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.imageio.ImageIO; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class WaterMarkerImageUtil { /** * 保存JPEG圖片 * @param image 二進制圖片對象 * @param imageFile 待保存的文件名稱 * @param quality 待保存的圖片質量 * @throws FileNotFoundException * @throws IOException */ public static void saveJPEGImage(BufferedImage image,File imageFile, int quality) throws FileNotFoundException,IOException{ BufferedOutputStream output = null; output = new BufferedOutputStream( new FileOutputStream(imageFile)); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image); param.setQuality(quality/100F, false); encoder.setJPEGEncodeParam(param); encoder.encode(image); if(output != null){ output.close(); } } /** * 給圖片加上水印文字 * @param imageFile 待處理的圖片 * @param mark 待添加的水印文字 * @throws FileNotFoundException * @throws IOException */ public static void waterMarkImage(File imageFile,String mark) throws FileNotFoundException,IOException{ //讀取圖片 BufferedInputStream input = new BufferedInputStream( new FileInputStream(imageFile)); BufferedImage image = ImageIO.read(input); //給圖片加水印效果 Graphics2D g2d = (Graphics2D) image.getGraphics(); int width = image.getWidth(); int height = image.getHeight(); Font font = new Font("Sans",Font.BOLD,24); Rectangle2D rectangle = font.getStringBounds(mark, g2d.getFontRenderContext()); if(width < rectangle.getWidth() + 6 || height < rectangle.getHeight() + 20) return; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float)0.8)); int x = width - (int)rectangle.getWidth() - 5; int y = height - 10; g2d.setFont(font); g2d.drawString(mark, x, y); //保存圖片 String fileName = imageFile.getName(); int dotPosition = fileName.lastIndexOf("."); String newFile = fileName.substring(0, dotPosition) + "_waterMark" + fileName.substring(dotPosition); saveJPEGImage(image, new File(newFile), 100); } public static void main(String[] args) { try { File file = new File("E://聖火傳遞3.jpg"); waterMarkImage(file,"聖火傳遞3"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
前言 最近想學點數據分析的知識,於是想到先用爬蟲爬點數據下來,後面能夠利用數據做些分析處理。由於之前沒有做過爬蟲的相關項目,調查後瞭解到除了主流Python外,Java爬取數據也是挺方便的,可以利用Webmagic框架進行爬取。
public static String getDate() { Date currentTime = new Date(); SimpleDateFormat formatter = new Simpl
以下是本人用Java實現的一個根據漢字生成區位碼的公共類: package com.sunfairs.util; import java.io.UnsupportedEncodingException; import java.util
在我們的Shiro配置文件中,我的配置文件叫:shiroConfig.java 將我下面縮寫的東西添加到spring裏面進行注入,註解就生效了。 /** * @description:開啓代碼權限註解支持 */ @Bean
最近項目有個需求,涉及發送郵件,發件人用的的阿里的企業郵箱,遇到一些坑: 1.剛開始一直找授權碼/服務密碼這個東西,後來問阿里那邊的客服才知道,原來阿里企業郵箱是沒有像QQ郵箱,網易郵箱那樣有授權碼的,所以登錄密碼就是服務密碼; 2.一開
場景 某一天,筆者在寫計算機組成原理實驗報告的時候,發現需要手動在表格中添加64條數據,想到這些數據都是有規律的,每個表格中大多數字段都是一樣的,而且不停地複製粘貼純屬浪費時間,於是就想到用程序來生成這份實驗報告。 使用poi-
大家好,很久不見,小弟最近閉關修煉iPhone中,所以很長時間沒更新博文(順便在寫某物的C++版,另外某物0.3.2版與WP7版已構建完成,不久就會發布)。這次回來,先換個與某物無關的話題,以目前用戶量最大的NScripter(簡稱NS,
2011-05-27 留言: 目前svn中有一個0.3的小補丁(LGame-Android-Core-0.3(revise).jar),修正了在初始化設置Mode.Fill時的自適屏低效問題。另外增加了Max(將遊戲View大小設置爲手機
2011-02-01 http://loon-simple.googlecode.com/files/LGame-0.2.95%28Graphics%20Optimization%29.7z 農曆新年前做一個額外更新,此文件中附帶