// 将 s 进行 BASE64 编码
public static String getBASE64(String s) {
if (s == null)
return null;
return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
}
// 将 BASE64 编码的字符串 s 进行解码
public static String getFromBASE64(String s) {
if (s == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(s);
return new String(b);
} catch (Exception e) {
return null;
}
}
public static int getLength(String text) {
int length = 0;
for (int i = 0; i < text.length(); i++) {
if (new String(text.charAt(i) + "").getBytes().length > 1) {
length += 2;
} else {
length += 1;
}
}
return length / 2;
}
//参数说明 水印图片 目标图片 位置 透明度
public final static void pressImage(String pressImg, String targetImg, int x, int y, float alpha) {
try {
File img = new File(targetImg);
System.out.println(img.length());
Image src = ImageIO.read(img);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
System.out.println(wideth+"---"+height);
BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
// 水印文件
Image src_biao = ImageIO.read(new File(pressImg));
int wideth_biao = src_biao.getWidth(null);
int height_biao = src_biao.getHeight(null);
System.out.println(wideth_biao+"###"+height_biao);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
g.drawImage(src_biao, (wideth - wideth_biao) / 2, (height - height_biao) / 2, wideth_biao, height_biao, null);
// 水印文件结束
g.dispose();
ImageIO.write((BufferedImage) image, "jpg", img);
} catch (Exception e) {
e.printStackTrace();
}
}
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 = ImageIO.read(img);
int width = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(src, 0, 0, width, height, null);
g.setColor(color);
g.setFont(new Font(fontName, fontStyle, fontSize));
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
g.drawString(pressText, (width - (getLength(pressText) * fontSize)) / 2 + x, (height - fontSize) / 2 + y);
g.dispose();
ImageIO.write((BufferedImage) image, "jpg", img);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
pressImage("D://My Documents//My Pictures//v.jpg", "D://My Documents//My Pictures//6.jpg", 0, 0, 0.5f);
//pressText("我是文字水印", "D://My Documents//My Pictures//vladstudio_learningtofly_1152x864.jpg", "黑体", 36, Color.red, 80, 0, 0, 0.1f);
}
分享到:
相关推荐
微信小程序用Canvas给图片加水印,拼接图片,制作名片PHelper-master.zip
Flex_给图片加水印,Flex_给图片加水印。Flex_给图片加水印
Qt编写的给图片加水印的工具,选择图片所在文件夹,即可批量自动给图片加水印,加水印过程中没有用多线程,会卡住界面。但适合自己批量加水印的小工具。
在线图片加水印,canvas加水印,在线给图片加水印、照片上添加文字,通过前端canvas给你的证件加上水印,防止证件被盗。 演示地址:http://watermark.dxcweb.com/
pb给图片加水印的方法 PowerBuilder 9.0代码
可经批量给图片加水印 方便,快捷,易操作
按键精灵手机版IOS,图片加水印源码,可实现给图片加水印。把水印图片加到主图片里面。暂时只支持IOS系统。
delphi实现给图片加水印方法2用ImageEn控件
C#给图片加水印的工具类,新手可以参考,也可以直接拿来用
批量给图片加水印软件
给图片加水印,代码所提供的方法可以和照相功能结合起来想水印相机一样拍照 合成水印照片
应为项目需要,给图片加图片水印,查阅了许多资料都不理想,最后使用ImageEN 5.2.0 FullSource控件解决,效果比较满意。 案例使用delphi7+ImageEN 5.2.0 FullSource制作,里面有ImageEN控件的安装方法。
用ASP用aspjpegwsImage组件给图片加水印
C#写的给图片加水印的实例代码,应该有一定的实用价值
本资源是博文:《 Python程序设计项目案例》— Python给图片加水印(可批量加水印)小程序代码与免安装应用(博文链接地址:https://blog.csdn.net/meenr/article/details/124486259)一文中发布的第一版即V1.1版本...