package cumt.zry.two;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
public class Watermark2{
public Watermark2(){super();};
/**
* 在源图片上设置水印文字
*/
public void WordsToImage(String srcImagePath,float alpha,
String font,int fontStyle,int fontSize,Color color,
String inputWords,int x,int y,String imageFormat,String toPath) throws IOException{
FileOutputStream fos=null;
try {
//读取图片
BufferedImage image = ImageIO.read(new File(srcImagePath));
//创建java2D对象
Graphics2D g2d=image.createGraphics();
//用源图像填充背景
g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null, null);
//!!!!
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
//为 Graphics2D 上下文设置 Composite。 Composite 用于所有绘制方法中,如 drawImage、
//drawString、draw 和 fill。 它指定新的像素如何在呈现过程中与图形设备上的现有像素组合。
g2d.setComposite(ac);
//设置文字字体名称、样式、大小
g2d.setFont(new Font(font, fontStyle, fontSize));
g2d.setColor(color);//设置字体颜色
g2d.drawString(inputWords, x, y); //输入水印文字及其起始x、y坐标
g2d.dispose();
//将水印后的图片写入toPath路径中
fos=new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, fos);
}
//文件操作错误抛出
catch (Exception e) {
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
/**
* 在源图像上设置图片水印
*/
public void ImageToImage(String srcImagePath,String appendImagePath,
float alpha,int x,int y,int width,int height,
String imageFormat,String toPath) throws IOException{
FileOutputStream fos = null;
try {
//读图
BufferedImage image = ImageIO.read(new File(srcImagePath));
//创建java2D对象
Graphics2D g2d=image.createGraphics();
//用源图像填充背景
g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null, null);
//关键地方
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
g2d.setComposite(ac);
BufferedImage appendImage = ImageIO.read(new File(appendImagePath));
g2d.drawImage(appendImage, x, y, width, height, null, null);
g2d.dispose();
fos=new FileOutputStream(toPath);
ImageIO.write(image, imageFormat, fos);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fos!=null){
fos.close();
}
}
}
public static void main(String[] args) throws Exception
{
Watermark2 imageObj = new Watermark2();
//源图片路径
String srcImagePath = "F:/27.jpg";
//水印图片路径
String appendImagePath = "F:/logo.jpg";
// ---- 宋体 普通字体 77号字 红色 透明度0.4"
float alpha = 0.4F;
String font = "宋体";
int fontStyle = Font.PLAIN;
int fontSize = 77;
Color color = Color.RED;
String inputWords = "图片上设置水印文字";
int x = 1700;
int y = 77;
String imageFormat = "jpg";
//水印文字后的存储路径
String wToPath = "F:/31.png";
//水印图片后的存储路径
String IToPath = "F:/7.png" ;
imageObj.WordsToImage(srcImagePath, alpha, font, fontStyle,
fontSize, color, inputWords, x, y, imageFormat, wToPath);
imageObj.ImageToImage(srcImagePath, appendImagePath, alpha,
x, y, 300, 200, imageFormat, IToPath);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有