JAVA技术交流QQ群:170933152  

 公开啦,看看有没有帮助,分享吧


处理前的三张图片: 

 java图像处理001---图像合成_java实现雨滴头像效果_可以放在地图上使用_自动生成雨滴头像_2d   java图像处理001---图像合成_java实现雨滴头像效果_可以放在地图上使用_自动生成雨滴头像_spring_02  java图像处理001---图像合成_java实现雨滴头像效果_可以放在地图上使用_自动生成雨滴头像_java_03

实现了下面这种效果: 

 java图像处理001---图像合成_java实现雨滴头像效果_可以放在地图上使用_自动生成雨滴头像_java_04

雨滴图像,42*67  头像图片 可以是任意的,到时候自己找就可以

package com.credream.image.scimage;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

import static com.credream.image.scimage.ImgUtil.watermarkImg;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ScImageApplicationTests {

@Test
public void contextLoads() {
}

public static void main(String[] args) throws Exception{
String baseImgUrl = "E:/testimg/u411.png";
String qrcode = "E:/testimg/gsy.png";

BufferedImage image = ImageIO.read(new File(baseImgUrl));

Graphics2D g=image.createGraphics();//获取这个:Graphics2D 这个可以消除锯齿,还有一些高级功能,普通的Graphics不行
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//这句话用来消除锯齿
g.setColor(Color.white);//给画笔设置颜色
g.drawOval(0,0,39,39);//画圆
g.fillOval(0,0,39,39);//填充圆

BufferedImage writeImage = ImageIO.read(new File(qrcode));
// BufferedImage roundImage=getRenderedImage(writeImage,0,0,39,39);

BufferedImage roundImage=convertCircular(writeImage);//把头像图片转换为圆的头像
g.drawImage(roundImage,0,0,39,39,null);//把圆的头像,画在雨滴头像的这个位置
FileOutputStream out=new FileOutputStream("E:/testimg/new.png");


ImageIO.write(image,"png",out);



//
// List<Double> points = Arrays.asList(0.35D,0.65D,0.66D,0.85D);
// ImgWatermark imgWatermarkVo = new ImgWatermark();
// imgWatermarkVo.getWatermarks().add(new ImgWatermark().new Watermark(qrcode,points));
// try {
// BufferedImage bufferedImage = ImgUtil.watermarkImg(baseImgUrl, imgWatermarkVo);
// OutputStream os = new FileOutputStream("E:/testimg/new.png");
// ImageIO.write(bufferedImage,"jpg",os);
// }catch (Exception e){
// e.printStackTrace();
// }

}

private static BufferedImage getRenderedImage(BufferedImage img, int x, int y, int size, int blur) {

int fix = blur / 2;

BufferedImage result = new BufferedImage(
size, size, BufferedImage.TYPE_INT_ARGB);

Graphics2D g = result.createGraphics();
g.setClip(new RoundRectangle2D.Double(
fix, fix, size-blur, size-blur, size-blur, size-blur));
g.translate(-x, -y);
g.drawImage(img, fix, fix, null);

size -= blur * 2;

// GaussianFilter filter = new GaussianFilter( blur * 2 );
// result = filter.filter(result, result);

g.setClip(new RoundRectangle2D.Double(
blur, blur, size - blur * 2, size - blur * 2, size - blur * 2, size - blur * 2));
g.drawImage(img, fix, fix, null);
g.dispose();
return result;
}


/**
* 缩小Image,此方法返回源图像按给定宽度、高度限制下缩放后的图像
*
* @param inputImage
* @param
* :压缩后宽度
* @param
* :压缩后高度
* @throws java.io.IOException
* return
*/
public static BufferedImage scaleByPercentage(BufferedImage inputImage, int newWidth, int newHeight) throws Exception {
// 获取原始图像透明度类型
int type = inputImage.getColorModel().getTransparency();
int width = inputImage.getWidth();
int height = inputImage.getHeight();
// 开启抗锯齿
RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 使用高质量压缩
renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
BufferedImage img = new BufferedImage(newWidth, newHeight, type);
Graphics2D graphics2d = img.createGraphics();
graphics2d.setRenderingHints(renderingHints);
graphics2d.drawImage(inputImage, 0, 0, newWidth, newHeight, 0, 0, width, height, null);
graphics2d.dispose();
return img;
}


/**
* 传入的图像必须是正方形的 才会 圆形 如果是长方形的比例则会变成椭圆的
*
* @param
*
* @return
* @throws IOException
*/
public static BufferedImage convertCircular(BufferedImage bi1) throws IOException {

// BufferedImage bi1 = ImageIO.read(new File(url));

// 这种是黑色底的
// BufferedImage bi2 = new BufferedImage(bi1.getWidth(), bi1.getHeight(), BufferedImage.TYPE_INT_RGB);

// 透明底的图片
BufferedImage bi2 = new BufferedImage(bi1.getWidth(), bi1.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, bi1.getWidth(), bi1.getHeight());
Graphics2D g2 = bi2.createGraphics();
g2.setClip(shape);
// 使用 setRenderingHint 设置抗锯齿
g2.drawImage(bi1, 0, 0, null);
// 设置颜色
g2.setBackground(Color.green);
g2.dispose();
return bi2;
}
}