Java图片处理 (文字水印、图片水印、缩放、补白)代码实例
程序员文章站
2024-03-05 14:05:06
package com.hmw.picmark;
import java.awt.alphacomposite;
import java.awt.color...
package com.hmw.picmark; import java.awt.alphacomposite; import java.awt.color; import java.awt.font; import java.awt.graphics2d; import java.awt.image; import java.awt.geom.affinetransform; import java.awt.image.affinetransformop; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; /** * 图片工具类, 图片水印,文字水印,缩放,补白等 * @author carl he */ public final class imageutils { /**图片格式:jpg*/ private static final string pictrue_formate_jpg = "jpg"; private imageutils(){} /** * 添加图片水印 * @param targetimg 目标图片路径,如:c://mypictrue//1.jpg * @param waterimg 水印图片路径,如:c://mypictrue//logo.png * @param x 水印图片距离目标图片左侧的偏移量,如果x<0, 则在正中间 * @param y 水印图片距离目标图片上侧的偏移量,如果y<0, 则在正中间 * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明) */ public final static void pressimage(string targetimg, string waterimg, int x, int y, float alpha) { try { file file = new file(targetimg); image image = imageio.read(file); int width = image.getwidth(null); int height = image.getheight(null); bufferedimage bufferedimage = new bufferedimage(width, height, bufferedimage.type_int_rgb); graphics2d g = bufferedimage.creategraphics(); g.drawimage(image, 0, 0, width, height, null); image waterimage = imageio.read(new file(waterimg)); // 水印文件 int width_1 = waterimage.getwidth(null); int height_1 = waterimage.getheight(null); g.setcomposite(alphacomposite.getinstance(alphacomposite.src_atop, alpha)); int widthdiff = width - width_1; int heightdiff = height - height_1; if(x < 0){ x = widthdiff / 2; }else if(x > widthdiff){ x = widthdiff; } if(y < 0){ y = heightdiff / 2; }else if(y > heightdiff){ y = heightdiff; } g.drawimage(waterimage, x, y, width_1, height_1, null); // 水印文件结束 g.dispose(); imageio.write(bufferedimage, pictrue_formate_jpg, file); } catch (ioexception e) { e.printstacktrace(); } } /** * 添加文字水印 * @param targetimg 目标图片路径,如:c://mypictrue//1.jpg * @param presstext 水印文字, 如:中国证券网 * @param fontname 字体名称, 如:宋体 * @param fontstyle 字体样式,如:粗体和斜体(font.bold|font.italic) * @param fontsize 字体大小,单位为像素 * @param color 字体颜色 * @param x 水印文字距离目标图片左侧的偏移量,如果x<0, 则在正中间 * @param y 水印文字距离目标图片上侧的偏移量,如果y<0, 则在正中间 * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明) */ public static void presstext(string targetimg, string presstext, string fontname, int fontstyle, int fontsize, color color, int x, int y, float alpha) { try { file file = new file(targetimg); image image = imageio.read(file); int width = image.getwidth(null); int height = image.getheight(null); bufferedimage bufferedimage = new bufferedimage(width, height, bufferedimage.type_int_rgb); graphics2d g = bufferedimage.creategraphics(); g.drawimage(image, 0, 0, width, height, null); g.setfont(new font(fontname, fontstyle, fontsize)); g.setcolor(color); g.setcomposite(alphacomposite.getinstance(alphacomposite.src_atop, alpha)); int width_1 = fontsize * getlength(presstext); int height_1 = fontsize; int widthdiff = width - width_1; int heightdiff = height - height_1; if(x < 0){ x = widthdiff / 2; }else if(x > widthdiff){ x = widthdiff; } if(y < 0){ y = heightdiff / 2; }else if(y > heightdiff){ y = heightdiff; } g.drawstring(presstext, x, y + height_1); g.dispose(); imageio.write(bufferedimage, pictrue_formate_jpg, file); } catch (exception e) { e.printstacktrace(); } } /** * 获取字符长度,一个汉字作为 1 个字符, 一个英文字母作为 0.5 个字符 * @param text * @return 字符长度,如:text="中国",返回 2;text="test",返回 2;text="中国abc",返回 4. */ public static int getlength(string text) { int textlength = text.length(); int length = textlength; for (int i = 0; i < textlength; i++) { if (string.valueof(text.charat(i)).getbytes().length > 1) { length++; } } return (length % 2 == 0) ? length / 2 : length / 2 + 1; } /** * 图片缩放 * @param filepath 图片路径 * @param height 高度 * @param width 宽度 * @param bb 比例不对时是否需要补白 */ public static void resize(string filepath, int height, int width, boolean bb) { try { double ratio = 0; //缩放比例 file f = new file(filepath); bufferedimage bi = imageio.read(f); image itemp = bi.getscaledinstance(width, height, bufferedimage.scale_smooth); //计算比例 if ((bi.getheight() > height) || (bi.getwidth() > width)) { if (bi.getheight() > bi.getwidth()) { ratio = (new integer(height)).doublevalue() / bi.getheight(); } else { ratio = (new integer(width)).doublevalue() / bi.getwidth(); } affinetransformop op = new affinetransformop(affinetransform.getscaleinstance(ratio, ratio), null); itemp = op.filter(bi, null); } if (bb) { bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb); graphics2d g = image.creategraphics(); g.setcolor(color.white); g.fillrect(0, 0, width, height); if (width == itemp.getwidth(null)) g.drawimage(itemp, 0, (height - itemp.getheight(null)) / 2, itemp.getwidth(null), itemp.getheight(null), color.white, null); else g.drawimage(itemp, (width - itemp.getwidth(null)) / 2, 0, itemp.getwidth(null), itemp.getheight(null), color.white, null); g.dispose(); itemp = image; } imageio.write((bufferedimage) itemp, "jpg", f); } catch (ioexception e) { e.printstacktrace(); } } public static void main(string[] args) throws ioexception { pressimage("c://pic//jpg", "c://pic//test.gif", 5000, 5000, 0f); presstext("c://pic//jpg", "旺仔之印", "宋体", font.bold|font.italic, 20, color.black, 0, 0, 8f); resize("c://pic//4.jpg", 1000, 500, true); } }