package com.hmw.picMark;
2
3
import java.awt.AlphaComposite;
4
import java.awt.Color;
5
import java.awt.Font;
6
import java.awt.Graphics2D;
7
import java.awt.Image;
8
import java.awt.geom.AffineTransform;
9
import java.awt.image.AffineTransformOp;
10
import java.awt.image.BufferedImage;
11
import java.io.File;
12
import java.io.IOException;
13
14
import javax.imageio.ImageIO;
15
16
/**
17
* 图片工具类, 图片水印,文字水印,缩放,补白等
18
*
@author
Carl He
19
*/
20
public
final
class ImageUtils {
21
/**
图片格式:JPG
*/
22
private
static
final String PICTRUE_FORMATE_JPG = "jpg";
23
24
private ImageUtils(){}
25
/**
26
* 添加图片水印
27
*
@param
targetImg 目标图片路径,如:C://myPictrue//1.jpg
28
*
@param
waterImg 水印图片路径,如:C://myPictrue//logo.png
29
*
@param
x 水印图片距离目标图片左侧的偏移量,如果x<0, 则在正中间
30
*
@param
y 水印图片距离目标图片上侧的偏移量,如果y<0, 则在正中间
31
*
@param
alpha 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明)
32
*/
33
public
final
static
void pressImage(String targetImg, String waterImg,
int x,
int y,
float alpha) {
34
try {
35 File file =
new File(targetImg);
36 Image image = ImageIO.read(file);
37
int width = image.getWidth(
null);
38
int height = image.getHeight(
null);
39 BufferedImage bufferedImage =
new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
40 Graphics2D g = bufferedImage.createGraphics();
41 g.drawImage(image, 0, 0, width, height,
null);
42
43 Image waterImage = ImageIO.read(
new File(waterImg));
//
水印文件
44
int width_1 = waterImage.getWidth(
null);
45
int height_1 = waterImage.getHeight(
null);
46 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
47
48
int widthDiff = width - width_1;
49
int heightDiff = height - height_1;
50
if(x < 0){
51 x = widthDiff / 2;
52 }
else
if(x > widthDiff){
53 x = widthDiff;
54 }
55
if(y < 0){
56 y = heightDiff / 2;
57 }
else
if(y > heightDiff){
58 y = heightDiff;
59 }
60 g.drawImage(waterImage, x, y, width_1, height_1,
null);
//
水印文件结束
61 g.dispose();
62 ImageIO.write(bufferedImage, PICTRUE_FORMATE_JPG, file);
63 }
catch (IOException e) {
64 e.printStackTrace();
65 }
66 }
67
68
/**
69
* 添加文字水印
70
*
@param
targetImg 目标图片路径,如:C://myPictrue//1.jpg
71
*
@param
pressText 水印文字, 如:中国证券网
72
*
@param
fontName 字体名称, 如:宋体
73
*
@param
fontStyle 字体样式,如:粗体和斜体(Font.BOLD|Font.ITALIC)
74
*
@param
fontSize 字体大小,单位为像素
75
*
@param
color 字体颜色
76
*
@param
x 水印文字距离目标图片左侧的偏移量,如果x<0, 则在正中间
77
*
@param
y 水印文字距离目标图片上侧的偏移量,如果y<0, 则在正中间
78
*
@param
alpha 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明)
79
*/
80
public
static
void pressText(String targetImg, String pressText, String fontName,
int fontStyle,
int fontSize, Color color,
int x,
int y,
float alpha) {
81
try {
82 File file =
new File(targetImg);
83
84 Image image = ImageIO.read(file);
85
int width = image.getWidth(
null);
86
int height = image.getHeight(
null);
87 BufferedImage bufferedImage =
new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
88 Graphics2D g = bufferedImage.createGraphics();
89 g.drawImage(image, 0, 0, width, height,
null);
90 g.setFont(
new Font(fontName, fontStyle, fontSize));
91 g.setColor(color);
92 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
93
94
int width_1 = fontSize * getLength(pressText);
95
int height_1 = fontSize;
96
int widthDiff = width - width_1;
97
int heightDiff = height - height_1;
98
if(x < 0){
99 x = widthDiff / 2;
100 }
else
if(x > widthDiff){
101 x = widthDiff;
102 }
103
if(y < 0){
104 y = heightDiff / 2;
105 }
else
if(y > heightDiff){
106 y = heightDiff;
107 }
108
109 g.drawString(pressText, x, y + height_1);
110 g.dispose();
111 ImageIO.write(bufferedImage, PICTRUE_FORMATE_JPG, file);
112 }
catch (Exception e) {
113 e.printStackTrace();
114 }
115 }
116
117
/**
118
* 获取字符长度,一个汉字作为 1 个字符, 一个英文字母作为 0.5 个字符
119
*
@param
text
120
*
@return
字符长度,如:text="中国",返回 2;text="test",返回 2;text="中国ABC",返回 4.
121
*/
122
public
static
int getLength(String text) {
123
int textLength = text.length();
124
int length = textLength;
125
for (
int i = 0; i < textLength; i++) {
126
if (String.valueOf(text.charAt(i)).getBytes().length > 1) {
127 length++;
128 }
129 }
130
return (length % 2 == 0) ? length / 2 : length / 2 + 1;
131 }
132
133
/**
134
* 图片缩放
135
*
@param
filePath 图片路径
136
*
@param
height 高度
137
*
@param
width 宽度
138
*
@param
bb 比例不对时是否需要补白
139
*/
140
public
static
void resize(String filePath,
int height,
int width,
boolean bb) {
141
try {
142
double ratio = 0;
//
缩放比例
143 File f =
new File(filePath);
144 BufferedImage bi = ImageIO.read(f);
145 Image itemp = bi.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);
146
//
计算比例
147
if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
148
if (bi.getHeight() > bi.getWidth()) {
149 ratio = (
new Integer(height)).doubleValue() / bi.getHeight();
150 }
else {
151 ratio = (
new Integer(width)).doubleValue() / bi.getWidth();
152 }
153 AffineTransformOp op =
new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio),
null);
154 itemp = op.filter(bi,
null);
155 }
156
if (bb) {
157 BufferedImage image =
new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
158 Graphics2D g = image.createGraphics();
159 g.setColor(Color.white);
160 g.fillRect(0, 0, width, height);
161
if (width == itemp.getWidth(
null))
162 g.drawImage(itemp, 0, (height - itemp.getHeight(
null)) / 2, itemp.getWidth(
null), itemp.getHeight(
null), Color.white,
null);
163
else
164 g.drawImage(itemp, (width - itemp.getWidth(
null)) / 2, 0, itemp.getWidth(
null), itemp.getHeight(
null), Color.white,
null);
165 g.dispose();
166 itemp = image;
167 }
168 ImageIO.write((BufferedImage) itemp, "jpg", f);
169 }
catch (IOException e) {
170 e.printStackTrace();
171 }
172 }
173
174
public
static
void main(String[] args)
throws IOException {
175 pressImage("C://pic//jpg", "C://pic//test.gif", 5000, 5000, 0f);
176 pressText("C://pic//jpg", "旺仔之印", "宋体", Font.BOLD|Font.ITALIC, 20, Color.BLACK, 0, 0, 8f);
177 resize("C://pic//4.jpg", 1000, 500,
true);
178 }
179 }
java图片处理(文字水印、图片水印、缩放、补白)
最新推荐文章于 2021-02-16 15:24:23 发布