9宫格 java_java 拼接头像9宫格
java后臺拼接9宮格頭像和添加水印文字,效果:
? ? ?
參考:https://blog.csdn.net/shichen88/article/details/101293541
1、拼接工具類:
package com.bai.blog.utils.files;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 拼接圖片 參考:https://blog.csdn.net/shichen88/article/details/101293541
* FileName: FileSplitUtil
* Create by: 漠天
* Date: 2019/12/23
*/
public class FileSplitUtil {
/**
* 合成圖片
*
* @param imagePaths List* @param message String
* @param outPutPath String
* @throws IOException
* return String
*/
public static String overlapImage(ListimagePaths, String message, String outPutPath) throws IOException {
int withAndHeight = 530;//背景圖片的寬高
int dividerWithAndHeight = 20;//divider分割空白寬高
int sonImageWithAndHeight = 150;//畫的子頭像圖片的寬高
int sonImageRadius= 30;//畫的子頭像的圓角
// 設置背景圖片大小,自己添加替換圖片邏輯
// BufferedImage backgroundImage = resizeImage(566, 230, ImageIO.read(new File(backgroundPath)));
//new一個白色背景板
BufferedImage backgroundImage = new BufferedImage(withAndHeight,withAndHeight,BufferedImage.TYPE_INT_ARGB);
backgroundImage.getGraphics().setColor(Color.white);
backgroundImage.getGraphics().fillRect(0,0,withAndHeight,withAndHeight);
backgroundImage.getGraphics().dispose();
backgroundImage = setClip(backgroundImage,30);
// 初始化有效的圖片BufferedImage列表
ListbufferedImageList = new ArrayList<>();
for(String imagePath:imagePaths){
bufferedImageList.add(setClip(resizeImage(sonImageWithAndHeight, sonImageWithAndHeight, ImageIO.read(new File(imagePath))),sonImageRadius));
}
// 獲取背景畫布
Graphics2D graphics = backgroundImage.createGraphics();
// 在背景圖片上添加文字
graphics.setColor(Color.blue);
graphics.setFont(new Font("宋體", Font.PLAIN, 20));
graphics.drawString(message, sonImageWithAndHeight/3, sonImageWithAndHeight/3);
// 畫出旋轉的文字,在指定位置
Font font = new Font("宋體", Font.PLAIN, 80);
AffineTransform affineTransform = new AffineTransform();
affineTransform.rotate(-Math.toRadians(45), 50, 20);
Font rotatedFont = font.deriveFont(affineTransform);
graphics.setFont(rotatedFont);
graphics.drawString(message, sonImageWithAndHeight, withAndHeight-sonImageWithAndHeight);
// 在背景圖片上添加子圖片
if(bufferedImageList.size()>0){
if(bufferedImageList.size()<2){
graphics.drawImage(bufferedImageList.get(0), dividerWithAndHeight, dividerWithAndHeight, withAndHeight-dividerWithAndHeight*2, withAndHeight-dividerWithAndHeight*2, null);
}else if(bufferedImageList.size()<3){
int newSonWidth = withAndHeight/2-dividerWithAndHeight*2;
int centerPlex =(withAndHeight-newSonWidth)/2;
graphics.drawImage(bufferedImageList.get(0), dividerWithAndHeight+dividerWithAndHeight/2, centerPlex, newSonWidth, newSonWidth, null);
graphics.drawImage(bufferedImageList.get(1), newSonWidth+dividerWithAndHeight*2+dividerWithAndHeight/2, centerPlex, newSonWidth, newSonWidth, null);
}else if(bufferedImageList.size()<10){
for(int i=0;i
2、調用:
ListformatFilePath = new ArrayList<>();
formatFilePath.add("D:/JavaLibFiles/Files/1.png");
formatFilePath.add("D:/JavaLibFiles/Files/2.png");
formatFilePath.add("D:/JavaLibFiles/Files/3.png");
formatFilePath.add("D:/JavaLibFiles/Files/4.png");
formatFilePath.add("D:/JavaLibFiles/Files/5.png");
formatFilePath.add("D:/JavaLibFiles/Files/6.png");
formatFilePath.add("D:/JavaLibFiles/Files/7.png");
String outPutPath = "D:/JavaLibFiles/Files/splitImage.png";
String message = "測試拼接信息";
String splitResultImagePath = FileSplitUtil.overlapImage(formatFilePath,message,outPutPath);
// 結果地址:splitResultImagePath
總結
以上是生活随笔為你收集整理的9宫格 java_java 拼接头像9宫格的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java布尔类型比较器_Java 8比较
- 下一篇: php图片上传方案,php图片上传