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<String>* @param message       String* @param outPutPath    String* @throws IOException* return String*/public static String overlapImage(List<String> imagePaths, 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列表List<BufferedImage> bufferedImageList = 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<bufferedImageList.size();i++){if(i<3){//012graphics.drawImage(bufferedImageList.get(i), sonImageWithAndHeight*i+dividerWithAndHeight*i+dividerWithAndHeight, dividerWithAndHeight, sonImageWithAndHeight, sonImageWithAndHeight, null);}else if(i<6){//345graphics.drawImage(bufferedImageList.get(i), sonImageWithAndHeight*(i%3)+dividerWithAndHeight*(i%3)+dividerWithAndHeight, sonImageWithAndHeight+dividerWithAndHeight*2, sonImageWithAndHeight, sonImageWithAndHeight, null);}else if(i<9){//678graphics.drawImage(bufferedImageList.get(i), sonImageWithAndHeight*(i%6)+dividerWithAndHeight*(i%6)+dividerWithAndHeight, sonImageWithAndHeight*2+dividerWithAndHeight*3, sonImageWithAndHeight, sonImageWithAndHeight, null);}}}}graphics.dispose();// 输出新的图片ImageIO.write(backgroundImage, "png", new File(outPutPath));return outPutPath;}/*** 重新设置图片大小** @param width* @param height* @param bufferedImage* @return*/private static BufferedImage resizeImage(int width, int height, BufferedImage bufferedImage) {BufferedImage newBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);newBufferedImage.getGraphics().drawImage(bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);return newBufferedImage;}/*** 图片设置圆角 (画白边)* @param srcImage  BufferedImage* @param radius    Int* @param border    Int* @param padding   Int* @return          BufferedImage* @throws IOException*/private 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  BufferedImage* @return          BufferedImage* @throws IOException*/private static BufferedImage setRadius(BufferedImage srcImage) throws IOException{int radius = (srcImage.getWidth() + srcImage.getHeight()) / 6;return setRadius(srcImage, radius, 2, 5);}/*** 图片切圆角*  圆角矩形:*  RoundRectangle2D rectRound = new RoundRectangle2D.Double(20,30,130,100,18,15);//左上角是(20,30),宽是130,高是100,圆角的长轴是18,短轴是15。** @param srcImage  BufferedImage* @param radius    radius* @return          BufferedImage*/private 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;}
}

2、调用:

        List<String> formatFilePath = 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 

java 拼接头像9宫格相关推荐

  1. 9宫格 java_java 拼接头像9宫格

    java后台拼接9宫格头像和添加水印文字,效果:       参考:https://blog.csdn.net/shichen88/article/details/101293541 1.拼接工具类: ...

  2. Java YUV图像四宫格拼接

    //四宫格拼接 byte[] dataH1 = YUVUtil.horizontalMontage(data, data, mWidth, mHeight); byte[] dataH2 = YUVU ...

  3. java实现求解n宫格

    n宫格:奇数阶宫格,使用1-n×n填入其中,使得每一行.每一列.正斜线和反斜线上每n个数的和都相等. 解法:使用网上已有算法,具体见附录. public class N_nine {public st ...

  4. QT利用opengl 进行视频裁剪、拼接,4宫格,9宫格

    一.概述 1.1 前言 在上一篇文章我们讲了Y420P视频数据如何裁剪.拼接.旋转等,但是缺点也很明显,一是工作量大,代码量较大.二是容错率低,因为涉及到大量的浮点型计算,导致在数据拷贝的时候存在误差 ...

  5. php九宫格图片合成,多宫格图片合成

    多宫格图片合成工具可以实现多种不同的图片合成以及图片特效图片加文字diy图片等组合功能. 它能够将多张图片按多宫格样式拼接合成为一张宫格图片,可自由设置拼接而成的图片大小以及每张之间的上下距离以及左右 ...

  6. java 9宫格抽奖_多宫格抽奖

    网上大多的多宫格抽奖都是自定义view,如果view的布局不一样而且太多的话容易出现oom,不好管理 结合RecyclerView实现多宫格抽奖  效果图如下 满足所有矩形多宫格抽奖 9宫格 16宫格 ...

  7. Java原来可以这么玩!CV视频合成处理,制作2宫格、4宫格、多宫格的视频

    效果展示 图片合成效果,不再演示,主要演示视频合成效果,因为是视频合成的原理就是先将每一张帧图合成图片,再用合成的图片制作成视频. 视频左右翻转合成效果 javacv实现视频翻转,合成 四宫格视频 更 ...

  8. Android:使用LayerDrawable动态生成四宫格头像(包含双人、三人头像)

    其实用自定义View也可以实现,我比较懒,就用LayerDrawable来创建一个新的Drawable资源实现. 举例4宫格,9宫格原理类似,每个图标的位置需要用边距慢慢调成预期的效果 效果如下: 双 ...

  9. java 9宫格抽奖_前端js实现九宫格模式抽奖(多宫格抽奖)

    介绍: 前端九宫格是一种常见的抽奖方式,js实现如下,掌握其原理,不论多少宫格,都可以轻松应对.(代码可复制直接运行看效果). 该案例以四宫格入门,可扩展多宫格,奖品模块的布局可自由设置. 四宫格抽奖 ...

最新文章

  1. [SAP ABAP开发技术总结]EXIT-COMMAND
  2. C语言实例:将人员信息写入磁盘文件并读出显示
  3. 将上传图片打上防伪图片水印并写入数据库
  4. 《如何搭建小微企业风控模型》第七节 准入规则节选
  5. 基于cxf框架javaweb服务说明
  6. jdk生成keystore、tomcat配置https
  7. 上位机与1200组态步骤_图解组态王一组态王软件
  8. php过滤excel文件,phpexcel读取excel内存释放怎么处理
  9. Juniper 210 密码清不掉_iPhone 11 每次下载应用都需要输入密码怎么办?
  10. 只有PHP大牛才能读懂的内涵图
  11. 海南大学研究生计算机分数线,海南大学研究生录取分数线
  12. (电力开发)376.1 主站通信协议基本结构解析
  13. TextView实现水平滚动
  14. Visual Studio 中使用万能头文件 #include bits/stdc++.h
  15. 机器学习---数据科学包-第4天
  16. 【网络游戏§绿色DOTA2魔笛V1.001 官方最新版§DOTA游戏辅助工具§】
  17. DLG、DRG、DEM、DOM定义与区别
  18. 科技项目撰写:关键技术、研发内容、创新点的差异和案例
  19. thunderbird 日历
  20. 春夏时节,淘宝店铺如何快速打造爆款?

热门文章

  1. 138.(前端)数据统计显示图表——vue子组件的挂载与echarts基本效果实现
  2. js执行机制(宏观,微观)
  3. QT设置调试器Debugger
  4. 看蓝鲸标准运维如何编排一切
  5. 生活中的定律之青蛙效应
  6. 球迷上演真实版肖申克救赎
  7. 大数据| 数据可视化学习-高德地图可视化工具
  8. aws azure_Microsoft Azure击败AWS的13种方式
  9. 小程序 成语猜题 升级版逻辑分享
  10. win10电脑如何使用本地账户登录