Skip to content

Commit d562b2b

Browse files
BYSocketJeffLi1993
authored andcommitted
1. 图片裁剪工具 - 椭圆
#bysocket
1 parent 44a5673 commit d562b2b

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.javacore.img;
2+
3+
import javax.imageio.ImageIO;
4+
import java.awt.*;
5+
import java.awt.geom.Ellipse2D;
6+
import java.awt.image.BufferedImage;
7+
import java.io.File;
8+
import java.io.IOException;
9+
/*
10+
* Copyright [2015] [Jeff Lee]
11+
*
12+
* Licensed under the Apache License, Version 2.0 (the "License");
13+
* you may not use this file except in compliance with the License.
14+
* You may obtain a copy of the License at
15+
*
16+
* http://www.apache.org/licenses/LICENSE-2.0
17+
*
18+
* Unless required by applicable law or agreed to in writing, software
19+
* distributed under the License is distributed on an "AS IS" BASIS,
20+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
* See the License for the specific language governing permissions and
22+
* limitations under the License.
23+
*/
24+
25+
/**
26+
* @author Jeff Lee
27+
* @since 2016-06-28 14:05:26
28+
* 图片裁成椭圆
29+
*/
30+
public class ImgCircleCut {
31+
32+
public static boolean ImgCircleCut(String srcFile, String targetFile) {
33+
try {
34+
// 获取img的BufferedImage对象,可以考虑创建不带透明色的BufferedImage对象:BufferedImage.TYPE_INT_ARGB
35+
BufferedImage srcBi = ImageIO.read(new File(srcFile));
36+
// 创建一个带透明色的BufferedImage对象
37+
BufferedImage targetBi = new BufferedImage(srcBi.getWidth(), srcBi.getHeight(),
38+
BufferedImage.TYPE_INT_ARGB);
39+
// 获取img窗体矩形定义的椭圆
40+
Ellipse2D.Double shape = new Ellipse2D.Double(0, 0,
41+
srcBi.getWidth(), srcBi.getHeight());
42+
// 创建目标图的Graphics2D对象
43+
Graphics2D g2 = targetBi.createGraphics();
44+
// 创建不透明 SRC_OVER 规则的 AlphaComposite 对象
45+
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.9f);
46+
g2.setComposite(ac);
47+
g2.setBackground(new Color(22, 2, 2, 0));
48+
// 是圆形 还是 椭圆 自定义参数
49+
g2.fill3DRect(200, 200, 180, 80, false);
50+
g2.setClip(shape);
51+
g2.drawImage(srcBi, 0, 0, null);
52+
g2.dispose();
53+
ImageIO.write(targetBi, "png", new File(targetFile));
54+
} catch (IOException e) {
55+
e.printStackTrace();
56+
return false;
57+
}
58+
return true;
59+
}
60+
61+
public static void main(String[] args) {
62+
ImgCircleCut("/jee/java-core-learning-example/src/resources/6890948.png","/jee/java-core-learning-example/src/resources/111.png");
63+
}
64+
}

0 commit comments

Comments
 (0)