/** * Created by toutou on 2013/8/28. */ import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; public class WaterMarkUtils { //定義水印文字樣式 private static final String MARK_TEXT = "請叫我頭頭哥"; private static final String FONT_NAME = "微軟雅黑"; private static final int FONT_STYLE = Font.BOLD; private static final int FONT_SIZE = 20; private static final Color FONT_COLOR = Color.gray; private static final String PATH = "D:\\Project"; private static final float ALPHA = 0.3F; //添加多條文字水印 public static String moreTextWaterMark(String myFile,String imageFileName) { InputStream is =null; OutputStream os =null; int X = 636; int Y = 763; File logossss = new File(myFile); try { Image image = ImageIO.read(logossss); //計算原始圖片寬度長度 int width = image.getWidth(null); int height = image.getHeight(null); //創建圖片緩存對象 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //創建java繪圖工具對象 Graphics2D graphics2d = bufferedImage.createGraphics(); //參數主要是,原圖,座標,寬高 graphics2d.drawImage(image, 0, 0, width, height, null); graphics2d.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE)); graphics2d.setColor(FONT_COLOR); //使用繪圖工具將水印繪製到圖片上 //計算文字水印寬高值 int waterWidth = FONT_SIZE*getTextLength(MARK_TEXT); int waterHeight = FONT_SIZE; //水印透明設置 graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA)); graphics2d.rotate(Math.toRadians(30), bufferedImage.getWidth()/2, bufferedImage.getHeight()/2); int x = -width/2; int y = -height/2; while(x < width*1.5){ y = -height/2; while(y < height*1.5){ graphics2d.drawString(MARK_TEXT, x, y); y+=waterHeight+100; } x+=waterWidth+100; } graphics2d.dispose(); os = new FileOutputStream(PATH+"/"+imageFileName); //創建圖像編碼工具類 JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os); //使用圖像編碼工具類,輸出緩存圖像到目標文件 en.encode(bufferedImage); if(is!=null){ is.close(); } if(os!=null){ os.close(); } } catch (IOException e) { e.printStackTrace(); } return "success"; } //計算水印文本長度 //1、中文長度即文本長度 2、英文長度爲文本長度二分之一 public static int getTextLength(String text){ //水印文字長度 int length = text.length(); for (int i = 0; i < text.length(); i++) { String s =String.valueOf(text.charAt(i)); if (s.getBytes().length>1) { length++; } } length = length%2==0?length/2:length/2+1; return length; } public static void main(String[] args) { File file = new File(PATH); File[] tempList = file.listFiles(); for (int i = 0; i < tempList.length; i++) { // 文件 if (tempList[i].isFile()) { String fileFullName = tempList[i].getName(); // 獲取文件名(不包含後綴名) String fileName = fileFullName.substring(0,fileFullName.lastIndexOf(".")); // 獲取後綴名 String prefix = fileFullName.substring(fileFullName.lastIndexOf(".")+1); String newFileName = fileName + "_1." + prefix; System.out.println(fileFullName); System.out.println(newFileName); moreTextWaterMark(tempList[i].toString(),newFileName); } // 文件夾 if (tempList[i].isDirectory()) { } } // e.g. moreTextWaterMark("原始文件路徑","新生成的水印圖片文件名"); System.out.println("成功"); } }
效果圖如下: