BufferedImageUtil.java
2.45 KB
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
package com.server.utils;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
/**
* Created by stone on 2017/4/27.
*/
public class BufferedImageUtil {
/**
* 图片设置圆角
* @param srcImage
* @param radius
* @param border
* @param padding
* @return
* @throws IOException
*/
public static BufferedImage setRadius(BufferedImage srcImage, int radius, int border, int padding) throws IOException {
int width = srcImage.getWidth();
int height = srcImage.getHeight();
int canvasWidth = width + padding * 2;
int canvasHeight = height + padding * 2;
BufferedImage image = new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D gs = image.createGraphics();
gs.setComposite(AlphaComposite.Src);
gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
gs.setColor(Color.WHITE);
gs.fill(new RoundRectangle2D.Float(0, 0, canvasWidth, canvasHeight, radius, radius));
gs.setComposite(AlphaComposite.SrcAtop);
gs.drawImage(setClip(srcImage, radius), padding, padding, null);
if(border !=0){
gs.setColor(Color.GRAY);
gs.setStroke(new BasicStroke(border));
gs.drawRoundRect(padding, padding, canvasWidth - 2 * padding, canvasHeight - 2 * padding, radius, radius);
}
gs.dispose();
return image;
}
/**
* 图片设置圆角
* @param srcImage
* @return
* @throws IOException
*/
public static BufferedImage setRadius(BufferedImage srcImage) throws IOException{
int radius = srcImage.getWidth();
return setRadius(srcImage, radius, 2, 5);
}
/**
* 图片切圆角
* @param srcImage
* @param radius
* @return
*/
public static BufferedImage setClip(BufferedImage srcImage, int radius){
int width = srcImage.getWidth();
int height = srcImage.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D gs = image.createGraphics();
gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
gs.setClip(new RoundRectangle2D.Double(0, 0, width, height, radius, radius));
gs.drawImage(srcImage, 0, 0, null);
gs.dispose();
return image;
}
}