0%

java 图片操作工具类

文章字数:108,阅读全文大约需要1分钟

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
package com.colin.tool.img;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

/**
* @author colin.cheng
* @date 2022-01-14 19:39
* @since 1.0.0
*/
public class ImageBuilder {

private BufferedImage bufferedImage;
private Graphics graphics;

private Font defaultFont = new Font(null, Font.BOLD, 28);
private Color defaultColor = new Color(120, 120, 120);

public ImageBuilder(int width, int height) {
this(width, height, BufferedImage.TYPE_INT_BGR);
}

public ImageBuilder(File img) throws FileNotFoundException, IOException {
try (final FileInputStream inputStream = new FileInputStream(img)) {
init(ImageIO.read(inputStream));
}
}

public ImageBuilder(BufferedImage bufferedImage) {
init(bufferedImage);
}

public ImageBuilder(int width, int height, int imageType) {
init(new BufferedImage(width, height, imageType));
}

private void init(BufferedImage bufferedImage) {
this.bufferedImage = bufferedImage;
graphics = bufferedImage.getGraphics();
graphics.setColor(this.defaultColor);
graphics.setFont(this.defaultFont);
}

public void drawText(String content, int x, int y) {
graphics.drawString(content, x, y);
}

public void fillRect(int x, int y, int width, int height) {
graphics.fillRect(x, y, width, height);
}

public void drawImg(BufferedImage img, int x, int y, int w, int h) {
graphics.drawImage(img, x, y, w, h, null);
}

public ImageBuilder cut(int x, int y, int w, int h) {
final BufferedImage subImage = bufferedImage.getSubimage(x, y, w, h);
return new ImageBuilder(subImage);
}

public BufferedImage toImage() {
return this.bufferedImage;
}

public boolean writeToFile(File file, String imgType) throws IOException {
return ImageIO.write(this.bufferedImage, imgType, file);
}


// setter

public void setFont(Font font) {
graphics.setFont(font);
}

public void setColor(Color color) {
graphics.setColor(color);
}

public int getWidth() {
return bufferedImage.getWidth();
}

public int getHeight() {
return bufferedImage.getHeight();
}

// static
}
  • 视频截取关键帧
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
package com.colin.tool.img;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
* @author colin.cheng
* @date 2022-01-17
* @since 1.0.0
*/
public class ImageUtil {

public static ImageBuilder getComicStrip(BufferedImage[] imgArray, int outHeight) {
int showW = outHeight, showH = outHeight;
final ImageBuilder imageBuilder = new ImageBuilder(showW * imgArray.length, showH, Color.BLACK);
for (int i = 0; i < imgArray.length; i++) {
BufferedImage image = imgArray[i];
final int height = image.getHeight();
final int width = image.getWidth();
if (height == width) {
imageBuilder.drawImg(image, i * showW, 0, showW, showH);
} else {
if(height > width) {
double n = (double) height / showH;
double w = width / n;
imageBuilder.drawImg(image, (int)(showW * i + (showH - w) / 2), 0, (int)w, showH);
} else {
double n = (double) width / showW;
double h = height / n;
imageBuilder.drawImg(image, showW * i, (int)((showW - h) / 2), showW, (int)h);
}
}
}
return imageBuilder;
}

public static ImageBuilder addComicStripIndex(BufferedImage comicStrip, float transparency, int strokeWidth) {
final int width = comicStrip.getWidth(), height = comicStrip.getHeight(), offset = height;
int length = width / height;
final ImageBuilder imageBuilder = new ImageBuilder(width + offset, height);
BufferedImage banner = ImageBuilder.cut(comicStrip, 0, 0, height, height);
imageBuilder.drawImg(banner, 0, 0, height, height);
imageBuilder.drawImg(comicStrip, offset, 0, width, height);
for (int i = 0; i < length; i++) {
double w = height - strokeWidth, base = width / w, h = height / base;
final int x = height * i + offset + strokeWidth / 2, y = (int)(height - h);
imageBuilder.drawTranslucentImage(comicStrip, x, y, (int)w, (int)h, transparency);
// 画框
if(strokeWidth > 0) {
imageBuilder.setColor(Color.BLACK);
imageBuilder.drawRect(x + (i * (int)h), y, (int)h, (int)h, strokeWidth);
}
// 当前所在的关键帧变清晰
if(transparency < 1) {
BufferedImage frame = ImageBuilder.cut(comicStrip, height * i, 0, height, height);
imageBuilder.drawImg(frame, x + (i * (int)h), y, (int)h, (int)h);
}
}
return imageBuilder;
}
}