java

位置:IT落伍者 >> java >> 浏览文章

Java图片处理 文字水印 图片水印 缩放 补白


发布日期:2020年08月27日
 
Java图片处理 文字水印 图片水印 缩放 补白

package util;

import javaawtAlphaComposite;

import javaawtColor;

import javaawtFont;

import javaawtGraphicsD;

import javaawtImage;

import javaawtgeomAffineTransform;

import javaawtimageAffineTransformOp;

import javaawtimageBufferedImage;

import javaioFile;

import javaioIOException;

import javaximageioImageIO;

/**

* @author Eric Xu

*

*/

public final class ImageUtils {

/**

* 图片水印

* @param pressImg 水印图片

* @param targetImg 目标图片

* @param x 修正值 默认在中间

* @param y 修正值 默认在中间

* @param alpha 透明度

*/

public final static void pressImage(String pressImg String targetImg int x int y float alpha) {

try {

File img = new File(targetImg);

Image src = ImageIOread(img);

int wideth = srcgetWidth(null);

int height = srcgetHeight(null);

BufferedImage image = new BufferedImage(wideth height BufferedImageTYPE_INT_RGB);

GraphicsD g = imagecreateGraphics();

gdrawImage(src wideth height null);

//水印文件

Image src_biao = ImageIOread(new File(pressImg));

int wideth_biao = src_biaogetWidth(null);

int height_biao = src_biaogetHeight(null);

gsetComposite(AlphaCompositegetInstance(AlphaCompositeSRC_ATOP alpha));

gdrawImage(src_biao (wideth wideth_biao) / (height height_biao) / wideth_biao height_biao null);

//水印文件结束

gdispose();

ImageIOwrite((BufferedImage) image jpg img);

} catch (Exception e) {

eprintStackTrace();

}

}

/**

* 文字水印

* @param pressText 水印文字

* @param targetImg 目标图片

* @param fontName 字体名称

* @param fontStyle 字体样式

* @param color 字体颜色

* @param fontSize 字体大小

* @param x 修正值

* @param y 修正值

* @param alpha 透明度

*/

public static void pressText(String pressText String targetImg String fontName int fontStyle Color color int fontSize int x int y float alpha) {

try {

File img = new File(targetImg);

Image src = ImageIOread(img);

int width = srcgetWidth(null);

int height = srcgetHeight(null);

BufferedImage image = new BufferedImage(width height BufferedImageTYPE_INT_RGB);

GraphicsD g = imagecreateGraphics();

gdrawImage(src width height null);

gsetColor(color);

gsetFont(new Font(fontName fontStyle fontSize));

gsetComposite(AlphaCompositegetInstance(AlphaCompositeSRC_ATOP alpha));

gdrawString(pressText (width (getLength(pressText) * fontSize)) / + x (height fontSize) / + y);

gdispose();

ImageIOwrite((BufferedImage) image jpg img);

} catch (Exception e) {

eprintStackTrace();

}

}

/**

* 缩放

* @param filePath 图片路径

* @param height 高度

* @param width 宽度

* @param bb 比例不对时是否需要补白

*/

public static void resize(String filePath int height int width boolean bb) {

try {

double ratio = ; //缩放比例

File f = new File(filePath);

BufferedImage bi = ImageIOread(f);

Image itemp = bigetScaledInstance(width height biSCALE_SMOOTH);

//计算比例

if ((bigetHeight() > height) || (bigetWidth() > width)) {

if (bigetHeight() > bigetWidth()) {

ratio = (new Integer(height))doubleValue() / bigetHeight();

} else {

ratio = (new Integer(width))doubleValue() / bigetWidth();

}

AffineTransformOp op = new AffineTransformOp(AffineTransformgetScaleInstance(ratio ratio) null);

itemp = opfilter(bi null);

}

if (bb) {

BufferedImage image = new BufferedImage(width height BufferedImageTYPE_INT_RGB);

GraphicsD g = imagecreateGraphics();

gsetColor(Colorwhite);

gfillRect( width height);

if (width == itempgetWidth(null))

gdrawImage(itemp (height itempgetHeight(null)) / itempgetWidth(null) itempgetHeight(null) Colorwhite null);

else

gdrawImage(itemp (width itempgetWidth(null)) / itempgetWidth(null) itempgetHeight(null) Colorwhite null);

gdispose();

itemp = image;

}

ImageIOwrite((BufferedImage) itemp jpg f);

} catch (IOException e) {

eprintStackTrace();

}

}

public static void main(String[] args) throws IOException {

pressImage(D:\\shuiyinpng D:\\testjpg f);

pressText(我是文字水印 D:\\testjpg 微软雅黑 Colorwhite f);

resize(D:\\testjpg true);

}

public static int getLength(String text) {

int length = ;

for (int i = ; i < textlength(); i++) {

if (new String(textcharAt(i) + )getBytes()length > ) {

length += ;

} else {

length += ;

}

}

return length / ;

}

}

               

上一篇:Java设置JS脚本中的变量

下一篇:Java很神奇 用swing制作欢迎屏幕