业务需求要在图片上添加水印。下面粘出代码供自己和大家分享
packagecom.pro.drawTextOnImg; importjava.awt.Color; importjava.awt.Font; importjava.awt.FontMetrics; importjava.awt.Graphics2D; importjava.awt.Image; importjava.awt.geom.Rectangle2D; importjava.awt.image.BufferedImage; importjava.io.FileNotFoundException; importjava.io.FileOutputStream; importjava.io.IOException; importjavax.imageio.ImageIO; importjavax.swing.ImageIcon; importorg.apache.commons.lang3.StringUtils; public classTestImg { public static voidmain(String[] args) { String filePath = "d:\inimg03.png"; String outPath = "d:\2.jpg"; drawTextInImg(filePath, outPath, new FontText("中国", 1, "#CC2BAC", 40, "黑体")); } public static voiddrawTextInImg(String filePath,String outPath, FontText text) { ImageIcon imgIcon = newImageIcon(filePath); Image img =imgIcon.getImage(); int width = img.getWidth(null); int height = img.getHeight(null); BufferedImage bimage = newBufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g =bimage.createGraphics(); g.setColor(getColor(text.getWm_text_color())); g.setBackground(Color.white); g.drawImage(img, 0, 0, null); Font font = null; if(StringUtils.isEmpty(text.getWm_text_font()) && text.getWm_text_size() != null) { font = newFont(text.getWm_text_font(), Font.BOLD, text.getWm_text_size()); } else{ font = new Font(null, Font.BOLD, 15); } g.setFont(font); FontMetrics metrics = newFontMetrics(font){}; Rectangle2D bounds = metrics.getStringBounds(text.getText(), null); int textWidth = (int) bounds.getWidth(); int textHeight = (int) bounds.getHeight(); int left = 0; int top =textHeight; //九宫格控制位置 if(text.getWm_text_pos()==2){ left = width/2; } if(text.getWm_text_pos()==3){ left = width -textWidth; } if(text.getWm_text_pos()==4){ top = height/2; } if(text.getWm_text_pos()==5){ left = width/2; top = height/2; } if(text.getWm_text_pos()==6){ left = width -textWidth; top = height/2; } if(text.getWm_text_pos()==7){ top = height -textHeight; } if(text.getWm_text_pos()==8){ left = width/2; top = height -textHeight; } if(text.getWm_text_pos()==9){ left = width -textWidth; top = height -textHeight; } g.drawString(text.getText(), left, top); g.dispose(); try{ FileOutputStream out = newFileOutputStream(outPath); ImageIO.write(bimage, "JPEG", out); out.close(); } catch(FileNotFoundException e) { //TODO Auto-generated catch block e.printStackTrace(); } catch(IOException e) { //TODO Auto-generated catch block e.printStackTrace(); } } //color #2395439 public staticColor getColor(String color) { if (color.charAt(0) == '#') { color = color.substring(1); } if (color.length() != 6) { return null; } try{ int r = Integer.parseInt(color.substring(0, 2), 16); int g = Integer.parseInt(color.substring(2, 4), 16); int b = Integer.parseInt(color.substring(4), 16); return newColor(r, g, b); } catch(NumberFormatException nfe) { return null; } } }
下面是FontText里面主要是水印的基本信息
packagecom.pro.drawTextOnImg; public classFontText { privateString text; private intwm_text_pos; privateString wm_text_color; privateInteger wm_text_size; private String wm_text_font;//字体 “黑体,Arial” publicString getText() { returntext; } public voidsetText(String text) { this.text =text; } public intgetWm_text_pos() { returnwm_text_pos; } public void setWm_text_pos(intwm_text_pos) { this.wm_text_pos =wm_text_pos; } publicString getWm_text_color() { returnwm_text_color; } public voidsetWm_text_color(String wm_text_color) { this.wm_text_color =wm_text_color; } publicInteger getWm_text_size() { returnwm_text_size; } public voidsetWm_text_size(Integer wm_text_size) { this.wm_text_size =wm_text_size; } publicString getWm_text_font() { returnwm_text_font; } public voidsetWm_text_font(String wm_text_font) { this.wm_text_font =wm_text_font; } public FontText(String text, intwm_text_pos, String wm_text_color, Integer wm_text_size, String wm_text_font) { super(); this.text =text; this.wm_text_pos =wm_text_pos; this.wm_text_color =wm_text_color; this.wm_text_size =wm_text_size; this.wm_text_font =wm_text_font; } publicFontText(){} }