java水印_Java水印图片处理

1 importjava.awt.AlphaComposite;2 importjava.awt.Color;3 importjava.awt.Font;4 importjava.awt.Graphics2D;5 importjava.awt.Image;6 importjava.awt.geom.AffineTransform;7 importjava.awt.image.AffineTransformOp;8 importjava.awt.image.BufferedImage;9 importjava.io.File;10 importjava.io.IOException;11

12 importjavax.imageio.ImageIO;13

14 /**

15 * 图片工具类, 图片水印,文字水印,缩放,补白等16 *17 *@authorCarl He18 */

19 public final classImageUtils {20 /**图片格式:JPG*/

21 private static final String PICTRUE_FORMATE_JPG = "jpg";22

23 privateImageUtils() {24 }25

26 /**

27 * 添加图片水印28 *29 *@paramtargetImg30 * 目标图片路径,如:C://myPictrue//1.jpg31 *@paramwaterImg32 * 水印图片路径,如:C://myPictrue//logo.png33 *@paramx34 * 水印图片距离目标图片左侧的偏移量,如果x<0, 则在正中间35 *@paramy36 * 水印图片距离目标图片上侧的偏移量,如果y<0, 则在正中间37 *@paramalpha38 * 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明)39 */

40 public final static voidpressImage(String targetImg, String waterImg,41 int x, int y, floatalpha) {42 try{43 File file = newFile(targetImg);44 Image image =ImageIO.read(file);45 int width = image.getWidth(null);46 int height = image.getHeight(null);47 BufferedImage bufferedImage = newBufferedImage(width, height,48 BufferedImage.TYPE_INT_RGB);49 Graphics2D g =bufferedImage.createGraphics();50 g.drawImage(image, 0, 0, width, height, null);51

52 Image waterImage = ImageIO.read(new File(waterImg)); //水印文件

53 int width_1 = waterImage.getWidth(null);54 int height_1 = waterImage.getHeight(null);55 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,56 alpha));57

58 int widthDiff = width -width_1;59 int heightDiff = height -height_1;60 if (x < 0) {61 x = widthDiff / 2;62 } else if (x >widthDiff) {63 x =widthDiff;64 }65 if (y < 0) {66 y = heightDiff / 2;67 } else if (y >heightDiff) {68 y =heightDiff;69 }70 g.drawImage(waterImage, x, y, width_1, height_1, null); //水印文件结束

71 g.dispose();72 ImageIO.write(bufferedImage, PICTRUE_FORMATE_JPG, file);73 } catch(IOException e) {74 e.printStackTrace();75 }76 }77

78 /**

79 * 添加文字水印80 *81 *@paramtargetImg82 * 目标图片路径,如:C://myPictrue//1.jpg83 *@parampressText84 * 水印文字, 如:中国证券网85 *@paramfontName86 * 字体名称, 如:宋体87 *@paramfontStyle88 * 字体样式,如:粗体和斜体(Font.BOLD|Font.ITALIC)89 *@paramfontSize90 * 字体大小,单位为像素91 *@paramcolor92 * 字体颜色93 *@paramx94 * 水印文字距离目标图片左侧的偏移量,如果x<0, 则在正中间95 *@paramy96 * 水印文字距离目标图片上侧的偏移量,如果y<0, 则在正中间97 *@paramalpha98 * 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明)99 */

100 public static voidpressText(String targetImg, String pressText,101 String fontName, int fontStyle, int fontSize, Color color, intx,102 int y, floatalpha) {103 try{104 File file = newFile(targetImg);105

106 Image image =ImageIO.read(file);107 int width = image.getWidth(null);108 int height = image.getHeight(null);109 BufferedImage bufferedImage = newBufferedImage(width, height,110 BufferedImage.TYPE_INT_RGB);111 Graphics2D g =bufferedImage.createGraphics();112 g.drawImage(image, 0, 0, width, height, null);113 g.setFont(newFont(fontName, fontStyle, fontSize));114 g.setColor(color);115 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,116 alpha));117

118 int width_1 = fontSize *getLength(pressText);119 int height_1 =fontSize;120 int widthDiff = width -width_1;121 int heightDiff = height -height_1;122 if (x < 0) {123 x = widthDiff / 2;124 } else if (x >widthDiff) {125 x =widthDiff;126 }127 if (y < 0) {128 y = heightDiff / 2;129 } else if (y >heightDiff) {130 y =heightDiff;131 }132

133 g.drawString(pressText, x, y +height_1);134 g.dispose();135 ImageIO.write(bufferedImage, PICTRUE_FORMATE_JPG, file);136 } catch(Exception e) {137 e.printStackTrace();138 }139 }140

141 /**

142 * 获取字符长度,一个汉字作为 1 个字符, 一个英文字母作为 0.5 个字符143 *144 *@paramtext145 *@return字符长度,如:text="中国",返回 2;text="test",返回 2;text="中国ABC",返回 4.146 */

147 public static intgetLength(String text) {148 int textLength =text.length();149 int length =textLength;150 for (int i = 0; i < textLength; i++) {151 if (String.valueOf(text.charAt(i)).getBytes().length > 1) {152 length++;153 }154 }155 return (length % 2 == 0) ? length / 2 : length / 2 + 1;156 }157

158 /**

159 * 图片缩放160 *161 *@paramfilePath162 * 图片路径163 *@paramheight164 * 高度165 *@paramwidth166 * 宽度167 *@parambb168 * 比例不对时是否需要补白169 */

170 public static void resize(String filePath, int height, int width, booleanbb) {171 try{172 double ratio = 0; //缩放比例

173 File f = newFile(filePath);174 BufferedImage bi =ImageIO.read(f);175 Image itemp =bi.getScaledInstance(width, height,176 BufferedImage.SCALE_SMOOTH);177 //计算比例

178 if ((bi.getHeight() > height) || (bi.getWidth() >width)) {179 if (bi.getHeight() >bi.getWidth()) {180 ratio = (newInteger(height)).doubleValue()181 /bi.getHeight();182 } else{183 ratio = (new Integer(width)).doubleValue() /bi.getWidth();184 }185 AffineTransformOp op = newAffineTransformOp(186 AffineTransform.getScaleInstance(ratio, ratio), null);187 itemp = op.filter(bi, null);188 }189 if(bb) {190 BufferedImage image = newBufferedImage(width, height,191 BufferedImage.TYPE_INT_RGB);192 Graphics2D g =image.createGraphics();193 g.setColor(Color.white);194 g.fillRect(0, 0, width, height);195 if (width == itemp.getWidth(null))196 g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,197 itemp.getWidth(null), itemp.getHeight(null),198 Color.white, null);199 else

200 g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,201 itemp.getWidth(null), itemp.getHeight(null),202 Color.white, null);203 g.dispose();204 itemp =image;205 }206 ImageIO.write((BufferedImage) itemp, "jpg", f);207 } catch(IOException e) {208 e.printStackTrace();209 }210 }211

212 public static void main(String[] args) throwsIOException {213 pressImage("C://pic//jpg", "C://pic//test.gif", 5000, 5000, 0f);214 pressText("C://pic//jpg", "旺仔之印", "宋体", Font.BOLD | Font.ITALIC, 20,215 Color.BLACK, 0, 0, 8f);216 resize("C://pic//4.jpg", 1000, 500, true);217 }218 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值