java

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

java.imageIo给图片添加水印的实现代码


发布日期:2022年03月01日
 
java.imageIo给图片添加水印的实现代码
最近项目在做一个商城项目 项目上的图片要添加水印①添加图片水印;②:添加文字水印;一下提供下个方法希望大家可以用得着复制代码 代码如下:
package comblogsimage;
import javaawtAlphaComposite;
import javaawtColor;
import javaawtFont;
import javaawtGraphicsD;
import javaawtImage;
import javaawtRenderingHints;
import javaawtimageBufferedImage;
import javaioFile;
import javaioFileOutputStream;
import javaioInputStream;
import javaioOutputStream;
import javaximageioImageIO;
import javaxswingImageIcon;
/**
* 图片水印
*/
public class ImageUtil {
/**
* @param args
*/
public static void main(String[] args) {
String srcImgPath = "e:/png";
String iconPath = "e:logojpg";
String targerPath = "e:/jpg";
// 给图片添加水印
ImageUtilwaterMarkImageByIcon(iconPath srcImgPath targerPath
f);
// 给图片添加水印水印旋转
// ImageMarkLogoByIconmarkImageByIcon(iconPath srcImgPath
// targerPath );
}
/**
* 给图片添加水印可设置水印图片旋转角度
*
* @param iconPath
* 水印图片路径
* @param srcImgPath
* 源图片路径
* @param targerPath
* 目标图片路径
* @param degree
* 水印图片旋转角度
* @param width
* 宽度(与左相比)
* @param height
* 高度(与顶相比)
* @param clarity
* 透明度(小于的数)越接近越透明
*/
public static void waterMarkImageByIcon(String iconPath String srcImgPath
String targerPath Integer degree Integer width Integer height
float clarity) {
OutputStream os = null;
try {
Image srcImg = ImageIOread(new File(srcImgPath));
Systemoutprintln("width:" + srcImggetWidth(null));
Systemoutprintln("height:" + srcImggetHeight(null));
BufferedImage buffImg = new BufferedImage(srcImggetWidth(null)
srcImggetHeight(null) BufferedImageTYPE_INT_RGB);
// 得到画笔对象
// Graphics g= buffImggetGraphics();
GraphicsD g = buffImgcreateGraphics();
// 设置对线段的锯齿状边缘处理
gsetRenderingHint(RenderingHintsKEY_INTERPOLATION
RenderingHintsVALUE_INTERPOLATION_BILINEAR);
gdrawImage(
srcImggetScaledInstance(srcImggetWidth(null)
srcImggetHeight(null) ImageSCALE_SMOOTH)
null);
if (null != degree) {
// 设置水印旋转
grotate(MathtoRadians(degree)
(double) buffImggetWidth() /
(double) buffImggetHeight() / );
}
// 水印图象的路径 水印一般为gif或者png的这样可设置透明度
ImageIcon imgIcon = new ImageIcon(iconPath);
// 得到Image对象
Image img = imgIcongetImage();
float alpha = clarity; // 透明度
gsetComposite(AlphaCompositegetInstance(AlphaCompositeSRC_ATOP
alpha));
// 表示水印图片的位置
gdrawImage(img width height null);
gsetComposite(AlphaCompositegetInstance(AlphaCompositeSRC_OVER));
gdispose();
os = new FileOutputStream(targerPath);
// 生成图片
ImageIOwrite(buffImg "JPG" os);
Systemoutprintln("添加水印图片完成!");
} catch (Exception e) {
eprintStackTrace();
} finally {
try {
if (null != os)
osclose();
} catch (Exception e) {
eprintStackTrace();
}
}
}
/**
* 给图片添加水印可设置水印图片旋转角度
*
* @param logoText
* 水印文字
* @param srcImgPath
* 源图片路径
* @param targerPath
* 目标图片路径
* @param degree
* 水印图片旋转角度
* @param width
* 宽度(与左相比)
* @param height
* 高度(与顶相比)
* @param clarity
* 透明度(小于的数)越接近越透明
*/
public static void waterMarkByText(String logoText String srcImgPath
String targerPath Integer degree Integer width Integer height
Float clarity) {
// 主图片的路径
InputStream is = null;
OutputStream os = null;
try {
Image srcImg = ImageIOread(new File(srcImgPath));
BufferedImage buffImg = new BufferedImage(srcImggetWidth(null)
srcImggetHeight(null) BufferedImageTYPE_INT_RGB);
// 得到画笔对象
// Graphics g= buffImggetGraphics();
GraphicsD g = buffImgcreateGraphics();
// 设置对线段的锯齿状边缘处理
gsetRenderingHint(RenderingHintsKEY_INTERPOLATION
RenderingHintsVALUE_INTERPOLATION_BILINEAR);
gdrawImage(
srcImggetScaledInstance(srcImggetWidth(null)
srcImggetHeight(null) ImageSCALE_SMOOTH)
null);
if (null != degree) {
// 设置水印旋转
grotate(MathtoRadians(degree)
(double) buffImggetWidth() /
(double) buffImggetHeight() / );
}
// 设置颜色
gsetColor(Colorred);
// 设置 Font
gsetFont(new Font("宋体" FontBOLD ));
float alpha = clarity;
gsetComposite(AlphaCompositegetInstance(AlphaCompositeSRC_ATOP
alpha));
// 第一参数>设置的内容后面两个参数>文字在图片上的坐标位置(xy)
gdrawString(logoText width height);
gdispose();
os = new FileOutputStream(targerPath);
// 生成图片
ImageIOwrite(buffImg "JPG" os);
Systemoutprintln("添加水印文字完成!");
} catch (Exception e) {
eprintStackTrace();
} finally {
try {
if (null != is)
isclose();
} catch (Exception e) {
eprintStackTrace();
}
try {
if (null != os)
osclose();
} catch (Exception e) {
eprintStackTrace();
}
}
}
}


还有一个图片缩放代码:

复制代码 代码如下:


/**
* 图片缩放(图片等比例缩放为指定大小空白部分以白色填充)
*
* @param srcPath
* 源图片路径
* @param destPath
* 缩放后图片路径
*/
public static void zoomImage(String srcPath String destPath int destHeight int destWidth) {
try {
BufferedImage srcBufferedImage = ImageIOread(new File(srcPath));
int imgWidth = destWidth;
int imgHeight = destHeight;
int srcWidth = srcBufferedImagegetWidth();
int srcHeight = srcBufferedImagegetHeight();
if (srcHeight >= srcWidth) {
imgWidth = (int) Mathround(((destHeight * / srcHeight) * srcWidth));
} else {
imgHeight = (int) Mathround(((destWidth * / srcWidth) * srcHeight));
}
BufferedImage destBufferedImage = new BufferedImage(destWidth destHeight BufferedImageTYPE_INT_RGB);
GraphicsD graphicsD = destBufferedImagecreateGraphics();
graphicsDsetBackground(ColorWHITE);
graphicsDclearRect( destWidth destHeight);
  graphicsDdrawImage(srcBufferedImagegetScaledInstance(imgWidth  imgHeight ImageSCALE_SMOOTH) (destWidth / ) (imgWidth / )  (destHeight / ) (imgHeight / ) null);
graphicsDdispose();
ImageIOwrite(destBufferedImage "JPEG" new File(destPath));
} catch (IOException e) {
eprintStackTrace();
}
}

               

上一篇:jsp Servlet中脚本元素、指令和预定义变量解析

下一篇:java 字符串相减(很简单的一个方法)