0%

分享一个图片工具类

分享一个图片工具类,

图片切割,图片水印,。。。。。

直接上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
package com.lrs.util;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.ImageIcon;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
*
* @author tyro
*
*/
@SuppressWarnings("restriction")
public class ImageUtils {

/** */
/**
* 把图片印刷到图片上
*
* @param pressImg
* -- 水印文件
* @param targetImg
* -- 目标文件
* @param drection
* --水印位置, <0.5 -- 左上角, =0.5 -- 中间 >0.5 -- 右下角
* @param alpha
* --透明度
*/
public final static void pressImage(String pressImg, String targetImg, float drection, float alpha) {
try {
// 目标文件
float Alpha = alpha;
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
// 水印文件
File _filebiao = new File(pressImg);
Image src_biao = ImageIO.read(_filebiao);
int wideth_biao = src_biao.getWidth(null);
int height_biao = src_biao.getHeight(null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, Alpha));
if (drection < 0.5) {
g.drawImage(src_biao, 50, 50, wideth_biao, height_biao, null);
} else if (drection == 0.5) {
g.drawImage(src_biao, wideth / 2, height / 2, null);
} else {
g.drawImage(src_biao, wideth - wideth_biao - 50, height - height_biao - 50, null);
}
// 水印文件结束
g.dispose();
FileOutputStream out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 图片水印,保留原图生成新的图片水印
*
* @param pressImg
* @param targetImg
* @param newPath
* @param drection
* @param alpha
*/
public final static void pressImage(String pressImg, String targetImg, String newPath, float drection,
float alpha) {
try {
OutputStream os = null;
// 目标文件
float Alpha = alpha;
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
// 水印文件
File _filebiao = new File(pressImg);
Image src_biao = ImageIO.read(_filebiao);
int wideth_biao = src_biao.getWidth(null);
int height_biao = src_biao.getHeight(null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, Alpha));
if (drection < 0.5) {
g.drawImage(src_biao, 50, 50, wideth_biao, height_biao, null);
} else if (drection == 0.5) {
g.drawImage(src_biao, wideth / 2, height / 2, null);
} else {
g.drawImage(src_biao, wideth - wideth_biao - 50, height - height_biao - 50, null);
}
// 水印文件结束
g.dispose();
os = new FileOutputStream(newPath);
// 生成图片
ImageIO.write(image, "JPG", os);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}

/** */
/**
* 打印文字水印图片
*
* @param pressText
* --文字
* @param targetImg
* -- 目标图片
* @param fontName
* -- 字体名
* @param fontStyle
* -- 字体样式
* @param color
* -- 字体颜色
* @param fontSize
* -- 字体大小
* @param x
* -- 偏移量
* @param y
*/

public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color,
int fontSize, float drection, float alpha) {
try {
System.out.println(pressText.length());
float Alpha = alpha;
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();

g.drawImage(src, 0, 0, wideth, height, null);
g.setColor(color);
g.setFont(new Font(fontName, fontStyle, fontSize));
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, Alpha));
if (drection < 0.5) {
g.drawString(pressText, fontSize, fontSize);
} else if (drection == 0.5) {
g.drawString(pressText, wideth / 2, height / 2);
} else {
// (中文)和 (文字的大小)是1:1的关系,字符是2:1的关系
g.drawString(pressText, wideth - (getTextLength(pressText) * fontSize) - fontSize, height - fontSize);
}
g.dispose();
FileOutputStream out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
System.out.println(e);
}
}

/**
* 给图片添加图片水印、可设置水印的旋转角度
*
* @param filePath
* 原图片地址
* @param newPath
* 新图片地址
* @param logoPath
* 水印图片地址
* @param degree
* 旋转的度数(-180到180的整数)
* @param alpha
* 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
*/
public static void rotateImage(String filePath, String newPath, String logoPath, int degree, String alpha) {
OutputStream os = null;
try {
if (!"".equals(filePath)) {
Image srcImg = ImageIO.read(new File(filePath));
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null),
BufferedImage.TYPE_INT_RGB);

// 得到画笔对象
Graphics2D g = buffImg.createGraphics();

// 设置对线段锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH),
0, 0, null);

// 设置水印图片旋转
g.rotate(Math.toRadians(degree), 0, 0);

// 水印图片的路径,水印一般格式是gif,png,这种图片可以设置透明度
ImageIcon imgIcon = new ImageIcon(logoPath);

// 得到Image对象
Image img = imgIcon.getImage();

// 原图片的宽高
int width = srcImg.getWidth(null);
int height = srcImg.getHeight(null);

// 水印图片的宽高
int width1 = img.getWidth(null);
int height1 = img.getHeight(null);

// 透明度
if (alpha == null || alpha.equals("")) {
alpha = "1";
}
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, Float.parseFloat(alpha)));

int x = -width / 2;
int y = -height / 2;
while (x < width * 2) {
y = -height / 2;
while (y < height * 1.5) {
g.drawImage(img, x, y, null);
y += height1 + 300;
}
x += width1 + 300;
}
// 表示水印图片的位置
// g.drawImage(img, (width - width1) / 2, (height - height1) /
// 2, width1, height1, null);

g.dispose();

os = new FileOutputStream(newPath);

// 生成图片
ImageIO.write(buffImg, "JPG", os);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != os) {
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

/**
* 获取文本长度
*
* @param text
* @return
*/
public static int getTextLength(String text) {
int length = text.length();
for (int i = 0; i < text.length(); i++) {
String s = String.valueOf(text.charAt(i));
if (s.getBytes().length > 1) {
length++;
}
}
length = length % 2 == 0 ? length / 2 : length / 2 + 1;// 是以中文的长度为标准获取
return length;
}

/**
* 从HTML源码中提取图片路径,最后以一个 String 类型的 List 返回,如果不包含任何图片,则返回一个 size=0 的List
* 需要注意的是,此方法只会提取以下格式的图片:.jpg|.bmp|.eps|.gif|.mif|.miff|.png|.tif|.tiff|.svg|.wmf|.jpe|.jpeg|.dib|.ico|.tga|.cut|.pic
*
* @param htmlCode
* HTML源码
* @return <img>标签 src 属性指向的图片地址的List集合
* @author Carl He
*/
public static List<String> getImageSrc(String htmlCode) {
List<String> imageSrcList = new ArrayList<String>();
Pattern p = Pattern.compile(
"<img\\b[^>]*\\bsrc\\b\\s*=\\s*('|\")?([^'\"\n\r\f>]+(\\.jpg|\\.bmp|\\.eps|\\.gif|\\.mif|\\.miff|\\.png|\\.tif|\\.tiff|\\.svg|\\.wmf|\\.jpe|\\.jpeg|\\.dib|\\.ico|\\.tga|\\.cut|\\.pic)\\b)[^>]*>",
Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(htmlCode);
String quote = null;
String src = null;
while (m.find()) {
quote = m.group(1);
src = (quote == null || quote.trim().length() == 0) ? m.group(2).split("\\s+")[0] : m.group(2);
imageSrcList.add(src);
}
return imageSrcList;
}

/**
* 图像切割(按指定起点坐标和宽高切割)
*
* @param srcImageFile
* 源图像地址
* @param result
* 切片后的图像地址
* @param x
* 目标切片起点坐标X
* @param y
* 目标切片起点坐标Y
* @param width
* 目标切片宽度
* @param height
* 目标切片高度
*/
public final static void cut(String srcImageFile, String result, int x, int y, int width, int height) {
try {
// 读取源图像
BufferedImage bi = ImageIO.read(new File(srcImageFile));
int srcWidth = bi.getHeight(); // 源图宽度
int srcHeight = bi.getWidth(); // 源图高度
if (srcWidth > 0 && srcHeight > 0) {
Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT);
// 四个参数分别为图像起点坐标和宽高
// 即: CropImageFilter(int x,int y,int width,int height)
ImageFilter cropFilter = new CropImageFilter(x, y, width, height);
Image img = Toolkit.getDefaultToolkit()
.createImage(new FilteredImageSource(image.getSource(), cropFilter));
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, width, height, null); // 绘制切割后的图
g.dispose();
// 输出为文件
ImageIO.write(tag, "png", new File(result));
}
} catch (Exception e) {
e.printStackTrace();
}
}

public final static void cut(InputStream in, String result, int x, int y, int width, int height) {
try {
// 读取源图像
BufferedImage bi = ImageIO.read(in);
int srcWidth = bi.getHeight(); // 源图宽度
int srcHeight = bi.getWidth(); // 源图高度
if (srcWidth > 0 && srcHeight > 0) {
Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT);
// 四个参数分别为图像起点坐标和宽高
// 即: CropImageFilter(int x,int y,int width,int height)
ImageFilter cropFilter = new CropImageFilter(x, y, width, height);
Image img = Toolkit.getDefaultToolkit()
.createImage(new FilteredImageSource(image.getSource(), cropFilter));
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, width, height, null); // 绘制切割后的图
g.dispose();
// 输出为文件
ImageIO.write(tag, "png", new File(result));
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 图片裁切
*
* @param x1
* 选择区域左上角的x坐标
* @param y1
* 选择区域左上角的y坐标
* @param width
* 选择区域的宽度
* @param height
* 选择区域的高度
* @param sourcePath
* 源图片路径
* @param descpath
* 裁切后图片的保存路径
*/
public static void cut(int x1, int y1, int width, int height, String sourcePath, String descpath) {
FileInputStream is = null;
ImageInputStream iis = null;
try {
is = new FileInputStream(sourcePath);
String fileSuffix = sourcePath.substring(sourcePath.lastIndexOf(".") + 1);
Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(fileSuffix);
ImageReader reader = it.next();
iis = ImageIO.createImageInputStream(is);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(x1, y1, width, height);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
ImageIO.write(bi, fileSuffix, new File(descpath));
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
is = null;
}
if (iis != null) {
try {
iis.close();
} catch (IOException e) {
e.printStackTrace();
}
iis = null;
}
}
}

public static void crop(InputStream input, OutputStream output, int x, int y, int w, int h, boolean isPNG)
throws Exception {
try {
BufferedImage srcImg = ImageIO.read(input);
int tmpWidth = srcImg.getWidth();
int tmpHeight = srcImg.getHeight();
int xx = Math.min(tmpWidth - 1, x);
int yy = Math.min(tmpHeight - 1, y);

int ww = w;
if (xx + w > tmpWidth) {
ww = Math.max(1, tmpWidth - xx);
}
int hh = h;
if (yy + h > tmpHeight) {
hh = Math.max(1, tmpHeight - yy);
}

BufferedImage dest = srcImg.getSubimage(xx, yy, ww, hh);

BufferedImage tag = new BufferedImage(w, h,
isPNG ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB);

tag.getGraphics().drawImage(dest, 0, 0, null);
ImageIO.write(tag, isPNG ? "png" : "jpg", output);
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e);
} finally {
if (input != null) {
input.close();
}
if (output != null) {
output.close();
}
}
}

public static void crop(InputStream input, String result, int x, int y, int w, int h, boolean isPNG)
throws Exception {
try {
BufferedImage srcImg = ImageIO.read(input);
int tmpWidth = srcImg.getWidth();
int tmpHeight = srcImg.getHeight();
int xx = Math.min(tmpWidth - 1, x);
int yy = Math.min(tmpHeight - 1, y);

int ww = w;
if (xx + w > tmpWidth) {
ww = Math.max(1, tmpWidth - xx);
}
int hh = h;
if (yy + h > tmpHeight) {
hh = Math.max(1, tmpHeight - yy);
}

BufferedImage dest = srcImg.getSubimage(xx, yy, ww, hh);

BufferedImage tag = new BufferedImage(w, h,
isPNG ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB);

tag.getGraphics().drawImage(dest, 0, 0, null);
ImageIO.write(tag, isPNG ? "png" : "jpg", new FileOutputStream(new File(result)));
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e);
} finally {
if (input != null) {
input.close();
}
}
}

public static void main(String[] args) throws FileNotFoundException, Exception {
// pressImage("E:\\watermark.png", "E:\\test1.jpg", 1f, 0.5f);
// pressImage("E:\\watermark.png", "E:\\test.jpg", "E:\\test1.jpg", 1f,
// 0.5f);
// pressText("www.tyro.com", "E:\\test1.jpg", "隶书", 36, Color.white, 36,
// 0.5f, 0.5f);
// rotateImage("E:\\test.jpg", "E:\\test2.jpg", "E:\\watermark.png", 30,
// "0.5");
System.out.print("添加成功");
// cut(148, 14, 251, 200, "E:\\lrs_img\\timg1.jpg",
// "E:\\lrs_img\\timg11.png");
crop(new FileInputStream("E:\\lrs_img\\timg1.jpg"), "E:\\lrs_img\\timg121.png", 148, 14, 251, 251, true);
}
}

拿走自己调试,如果可以,就点个赞呗!!!

您的打赏,是我创作的动力!不给钱?那我只能靠想象力充饥了。