java实现图片圆角处理、背景透明化

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

java实现图⽚圆⾓处理、背景透明化
⽬录
java 图⽚圆⾓处理、背景透明化
java 的图⽚处理解析
java 图⽚圆⾓处理、背景透明化
/**图⽚圆⾓处理,背景透明化
* @param srcImageFile 原图⽚
* @param result 处理后图⽚
* @param type 图⽚格式
* @param cornerRadius 720为圆⾓
*/
public void makeRoundedCorner(File srcImageFile, File result, String type, int cornerRadius) {
try {
BufferedImage bi1 = ImageIO.read(srcImageFile);
// 根据需要是否使⽤ BufferedImage.TYPE_INT_ARGB
BufferedImage image = new BufferedImage(bi1.getWidth(), bi1.getHeight(),
BufferedImage.TYPE_INT_ARGB);
Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, bi1.getWidth(), bi1
.getHeight());
Graphics2D g2 = image.createGraphics();
image = g2.getDeviceConfiguration().createCompatibleImage(bi1.getWidth(), bi1.getHeight(), Transparency.TRANSLUCENT); g2 = image.createGraphics();
g2.setComposite(AlphaComposite.Clear);
g2.fill(new Rectangle(image.getWidth(), image.getHeight()));
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f));
g2.setClip(shape);
// 使⽤ setRenderingHint 设置抗锯齿
g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.fillRoundRect(0, 0,bi1.getWidth(), bi1.getHeight(), cornerRadius, cornerRadius);
g2.setComposite(AlphaComposite.SrcIn);
g2.drawImage(bi1, 0, 0, bi1.getWidth(), bi1.getHeight(), null);
g2.dispose();
ImageIO.write(image, type, result);
} catch (Exception e) {
// TODO: handle exception
}
}
java 的图⽚处理解析
直接上效果图,现在有的需求就是把⽤户的头像,跟昵称嵌⼊到这个背景图中。

第⼀步,把头像切成圆⾓,背景透明的图⽚。

第⼆部,把第⼀步⽣成的图⽚,当成⽔印放到坐标的左边的红箭头的地⽅
第三部,创建⽂字⽔印,然后放⼊到右边的红箭头地⽅。

效果图如下:
由于需要thumbnailator组件⽀持先导⼊maven
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId> <version>0.4.8</version>
</dependency>
直接上代码:
package com.image;
import java.awt.AlphaComposite; import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Transparency;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Position;
public class ImageDo {
public static void main(String[] args) throws IOException {
//⾸先获取
makeRoundedCorner("C:/Users/luojie/Desktop/0.jpg", "C:/Users/luojie/Desktop/2.png", "png", 170);
//后续⽔印在背景图⽚的的x轴y轴的坐标
Position ab= new Position() {
@Override
public Point calculate(int enclosingWidth, int enclosingHeight, int width, int height, int insetLeft,
int insetRight, int insetTop, int insetBottom) {
// TODO Auto-generated method stub
return new Point(89, 53);
}
};
//把⽣成的圆形图⽚变换成宽⾼142x142的图⽚
Thumbnails.of("C:/Users/luojie/Desktop/2.png").size(142, 142).toFile(
"C:/Users/luojie/Desktop/2_142x142.png");
//把⽣成的圆形图⽚,当⽔印贴到背景图中,ab为圆形图⽚应该到背景图的x轴y轴的坐标
Thumbnails.of("C:/Users/luojie/Desktop/cmbg.png").size(1280, 1024).watermark(ab,
ImageIO.read(new File("C:/Users/luojie/Desktop/2_142x142.png")), 1f)
.outputQuality(0.8f).toFile("C:/Users/luojie/Desktop/image_watermark_bottom_right.jpg");
//给⽂字⽔印
pressText("C:/Users/luojie/Desktop/image_watermark_bottom_right.jpg", "WEIXINYONGHU", "Comic Sans MS", Font.BOLD, 30, Color.BLACK,275, 65, 1f); }
/**
* 添加⽂字⽔印
* @param targetImg ⽬标图⽚路径,如:C://myPictrue//1.jpg
* @param pressText ⽔印⽂字,如:中国证券⽹
* @param fontName 字体名称,如:宋体
* @param fontStyle 字体样式,如:粗体和斜体(Font.BOLD|Font.ITALIC)
* @param fontSize 字体⼤⼩,单位为像素
* @param color 字体颜⾊
* @param x ⽔印⽂字距离⽬标图⽚左侧的偏移量,如果x<0, 则在正中间
* @param y ⽔印⽂字距离⽬标图⽚上侧的偏移量,如果y<0, 则在正中间
* @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明)
*/
public static void pressText(String targetImg,String pressText,String fontName,int fontStyle,int fontSize,Color color,int x,int y,float alpha){
try {
File file = new File(targetImg);
Image image = ImageIO.read(file);
int width = image.getWidth(null);
int height = image.getHeight(null);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(image,0,0, width, height, null);
g.setFont(new Font(fontName, fontStyle, fontSize));
g.setColor(color);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
int width_wi = fontSize*getTextLength(pressText);
int height_wi = fontSize;
int widthDiff = width-width_wi;
int heightDiff = height-height_wi;
if(x<0){
x = widthDiff/2;
}else if(x>widthDiff){
x=widthDiff;
}
if(y<0){
y = heightDiff/2;
}else if(y>heightDiff){
y = heightDiff;
}
g.drawString(pressText, x, y+height_wi);//⽔印⽂件
g.dispose();
ImageIO.write(bufferedImage, "JPEG", file);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 计算⽂字像素长度
* @param text
* @return
*/
private static int getTextLength(String text){
int textLength = text.length();
int length = textLength;
for (int i = 0; i < textLength; i++) {
int wordLength = String.valueOf(text.charAt(i)).getBytes().length;
if(wordLength > 1){
length+=(wordLength-1);
}
}
return length%2==0 ? length/2:length/2+1;
}
/*
* 圆⾓处理
* @param BufferedImage
* @param cornerRadius
* */
public static String makeRoundedCorner(String srcImageFile, String result, String type, int cornerRadius) { try {
BufferedImage image = ImageIO.read(new File(srcImageFile));
// int w = image.getWidth();
// int h = image.getHeight();
int w = image.getWidth();
int h = image.getHeight();
BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = output.createGraphics();
output = g2.getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT);
g2.dispose();
g2 = output.createGraphics();
//这⾥绘画圆⾓矩形
// g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // g2.fillRoundRect(0, 0,w, h, cornerRadius, cornerRadius);
// g2.setComposite(AlphaComposite.SrcIn);
//这⾥绘画原型图
Ellipse2D.Double shape = new Ellipse2D.Double(0, 0,w, h);
g2.setClip(shape);
g2.drawImage(image, 0, 0, w, h, null);
g2.dispose();
ImageIO.write(output, type, new File(result));
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

相关文档
最新文档