很强大的java图片拼接工具类,可用于各种图片创建、图片生成、图文拼接、藏宝图分块拼接、分享海报

ImgJoinUtil图片拼接工具类
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.xiangweihui.core.bean.imgJoin.ImgBean;
import com.xiangweihui.core.bean.imgJoin.TextBean;
import com.xiangweihui.core.util.system.UniqId;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Project xiangweihui
 * @PackageName com.xiangweihui.core.util
 * @ClassName ImgJoinUtil
 * @Author jiahong.xing
 * @Date 2019/6/17 13:32
 * @Description 图片拼接工具类
 */
public class ImgJoinUtil {

    public static File createColorImg(final int width, final int height, final Color color, List<ImgBean> imgBeanList, List<TextBean> textBeanList) {
        return createColorImg(width, height, color, null, imgBeanList, textBeanList);
    }

    /**
     * @param width        图片宽度
     * @param height 图片高度
     * @param color 背景颜色
     * @param outPath 输出路径,可为空
     * @param imgBeanList 图片列表
     * @param textBeanList 文字列表
     * @return
     */
    public static File createColorImg(final int width, final int height, final Color color, String outPath, List<ImgBean> imgBeanList, List<TextBean> textBeanList) {
        if (width < 1 || height < 1) {
            return null;
        }


        File file = null;
        FileOutputStream out = null;
        try {
            //构造一个类型为预定义图像类型之一的 BufferedImage。 宽度为第一只的宽度,高度为各个图片高度之和
            BufferedImage bufferedImage = createColorImg(width, height, color);
            //创建输出流
            if (StringUtils.isNotEmpty(outPath)) {
                file = new File(outPath);
            } else {
                file = File.createTempFile("joimImg", UniqId.getUid() + ".jpg");
            }
            out = new FileOutputStream(file);

            //进行图片的处理
            if (CollectionUtils.isNotEmptyCollection(imgBeanList)) {
                Graphics graphics = bufferedImage.createGraphics();
                for (ImgBean imgBean : imgBeanList) {
                    if (null == imgBean.getFile()) {
                        continue;
                    }
                    BufferedImage image = ImageIO.read(imgBean.getFile());
                    int img_height = imgBean.getImg_height();
                    int img_width = imgBean.getImg_width();
                    if (img_height < 1) {
                        img_height = image.getHeight();
                    }
                    if (img_width < 1) {
                        img_width = image.getWidth();
                    }
                    graphics.drawImage(image, imgBean.getLeft(), imgBean.getTop(), img_width, img_height, null);
                }

                // 释放此图形的上下文以及它使用的所有系统资源。
                graphics.dispose();
            }

            //进行文字的处理
            if (CollectionUtils.isNotEmptyCollection(textBeanList)) {
                for (TextBean textBean : textBeanList) {
                    if (StringUtils.isEmpty(textBean.getText())) {
                        continue;
                    }
                    //字体
                    Graphics2D graphics2D = bufferedImage.createGraphics();
                    Font font = null;
                    if (null != textBean.getFont()) {
                        font = textBean.getFont();
                    } else {
                        font = FontUtil.getDefaultFont();
                    }
                    graphics2D.setFont(font);

                    graphics2D.setColor(textBean.getColor());
                    graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                    //获取字符高度
                    int strHeight = graphics2D.getFontMetrics().getHeight();

                    //得到文字的长度
                    int max_length = FontUtil.getStringLength(font, textBean.getText());
                    //判断是否控制了文字长度
                    if (textBean.getMax_width() > 0 && max_length > textBean.getMax_width()) {

                        //初始高度
                        int inity = textBean.getTop();
                        //循环得到字符串的长度
                        String newStr = textBean.getText();


                        //如果不是最后的长度
                        while (!FontUtil.isMaxStr(font, newStr, textBean.getMax_width())) {
                            String istr = FontUtil.strSplitMaxLenth(font, newStr, textBean.getMax_width());
                            //直接绘制
                            graphics2D.drawString(istr, textBean.getLeft(), inity);
                            //完成后,进行初始化
                            newStr = newStr.replaceFirst(istr, "");

                            //高度再次重构
                            inity += strHeight + textBean.getLine_height();
                        }
                        //最后补一次
                        if (newStr.length() > 0) {
                            graphics2D.drawString(newStr, textBean.getLeft(), inity);
                        }

                    } else {
                        graphics2D.drawString(textBean.getText(), textBean.getLeft(), textBean.getTop());
                    }
                    // 释放此图形的上下文以及它使用的所有系统资源。
                    graphics2D.dispose();
                }
            }


            //将绘制的图像生成至输出流
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(bufferedImage);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                //关闭输出流
                if (null != out) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return file;
    }

    //创建一张底图
    public static BufferedImage createColorImg(int width, int height, Color color) {
        //创建一个图片缓冲区
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
        //获取图片处理对象
        Graphics graphics = bufferedImage.getGraphics();
        //填充背景色
        graphics.setColor(color);
        graphics.fillRect(0, 0, width, height);

        return bufferedImage;
    }

}

会默认根据大小和底色创建一个底图,然后根据根据各种图片、文字等进行拼接,最终生成一张大图片,非常高清,支持超高分辨率

ImgBean图片文件对象,对象内有很多图片操作公共方法

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * @Project my
 * @PackageName com.my.test.codeTest.test.图片组合.bean
 * @ClassName ImgBean
 * @Author jiahong.xing
 * @Date 2019/4/25 10:09
 * @Description TODO
 */
@Getter
@Setter
@ToString
public class ImgBean {

    //图片文件
    private File file;

    //距左距离
    private int left;

    //距上距离
    private int top;

    //图片宽度
    private int img_height;

    //图片高度
    private int img_width;


    public ImgBean(File file, int left, int top) {
        this.file = file;
        this.left = left;
        this.top = top;
    }

    public ImgBean(File file, int left, int top, int img_height, int img_width) {
        this.file = file;
        this.left = left;
        this.top = top;
        this.img_height = img_height;
        this.img_width = img_width;
    }

    public ImgBean() {
    }

    //控制最高和最宽,如果是图片不够
    public void setMaxWidthAndHeight(int maxWidth, int maxHeight) {
        BufferedImage bufferedImage = getBufferedImage();
        if (null == bufferedImage) {
            return;
        }

        int height = bufferedImage.getHeight();
        int width = bufferedImage.getWidth();

        //首先计算宽度
        if (maxWidth > 0) {
            //计算宽的比例
            BigDecimal bili_w = new BigDecimal(maxWidth).divide(new BigDecimal(width), 2, BigDecimal.ROUND_HALF_UP);
            width = maxWidth;

            //得到比例后的高
            height = bili_w.multiply(new BigDecimal(height)).intValue();
        }

        //在判断高度
        if (maxHeight > 0 && height > maxHeight) {
            //计算高的比例
            BigDecimal bili_w = new BigDecimal(maxHeight).divide(new BigDecimal(height), 2, BigDecimal.ROUND_HALF_UP);
            height = maxHeight;

            //得到比例后的宽
            width = bili_w.multiply(new BigDecimal(width)).intValue();
        }
        if (width > 0) {
            setImg_width(width);
        }
        if (height > 0) {
            setImg_height(height);
        }
    }


    //得到图片
    public BufferedImage getBufferedImage() {
        if (null == file) {
            return null;
        }

        BufferedImage bufferedImage = null;
        try {
            bufferedImage = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (null == bufferedImage || bufferedImage.getHeight() < 1 || bufferedImage.getWidth() < 1) {
            return null;
        }
        return bufferedImage;
    }

    //根据宽度放大图片
    public void toWidthUpperCase(int maxWidth) {
        BufferedImage bfimg = getBufferedImage();
        if (null == bfimg) {
            return;
        }

        int height = bfimg.getHeight();
        int width = bfimg.getWidth();

        //如果小于才进行放大操作,否则不操作
        if (width < maxWidth) {
            try {
                //计算比例
                BigDecimal bili_w = new BigDecimal(maxWidth).divide(new BigDecimal(width), 2, BigDecimal.ROUND_HALF_UP);
                width = maxWidth;

                //得到比例后的宽
                height = bili_w.multiply(new BigDecimal(height)).intValue();

                //进行图片处理
                Image srcImg = ImageIO.read(getFile());

                BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                buffImg.getGraphics().drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

                ImageIO.write(buffImg, "JPEG", file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }

}

TextBean文字对象,有很多工具方法

import com.xiangweihui.core.util.FontUtil;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import sun.font.FontDesignMetrics;

import java.awt.*;

/**
 * @Project my
 * @PackageName com.my.test.codeTest.test.图片组合.bean
 * @ClassName TextBean
 * @Author jiahong.xing
 * @Date 2019/5/15 17:52
 * @Description 文字对象
 */
@Getter
@Setter
@ToString
public class TextBean {

    //字体
    private Font font;

    //颜色
    private Color color;

    //文字
    private String text;

    //距左距离
    private int left;

    //距上距离
    private int top;

    //最大宽度
    private int max_width;

    //上下行间距
    private int line_height;


    public TextBean(Font font, Color color, String text, int left, int top) {
        this.font = font;
        this.color = color;
        this.text = text;
        this.left = left;
        this.top = top;
    }

    public TextBean(Font font, Color color, String text, int left, int top, int max_width, int line_height) {
        this.font = font;
        this.color = color;
        this.text = text;
        this.left = left;
        this.top = top;
        this.max_width = max_width;
        this.line_height = line_height;
    }

    public TextBean(Color color, String text, int left, int top) {
        this.text = text;
        this.color = color;
        this.left = left;
        this.top = top;
    }


    //得到最终的高度
    public int getMaxHeight() {
        //初始高度
        int inity = 0;

        FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
        //获取字符高度
        int strHeight = metrics.getHeight();
        //得到文字的长度
        int max_length = FontUtil.getStringLength(font, getText());
        //判断是否控制了文字长度
        if (getMax_width() > 0 && max_length > getMax_width()) {


            //循环得到字符串的长度
            String newStr = getText();
            //如果不是最后的长度
            while (!FontUtil.isMaxStr(font, newStr, getMax_width())) {
                String istr = FontUtil.strSplitMaxLenth(font, newStr, getMax_width());
                //完成后,进行初始化
                newStr = newStr.replaceFirst(istr, "");

                //高度再次重构
                inity += strHeight + getLine_height();
            }
            //最后补一次
            if (newStr.length() > 0) {
                inity += strHeight;
            }
        }
        return inity;
    }
}

其中字体工具类FontUtil

https://blog.csdn.net/qq_21235239/article/details/99417433

UniqId工具,唯一id生成

https://blog.csdn.net/qq_21235239/article/details/99417796

 

根据底图、二维码、文字生成出来的效果,下篇讲java生成二维码并中间自带头像logo

生成出来的图片非常高清,下篇是二维码生成并带logo,有疑问留言

  • 1
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 21
    评论
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

成都java小生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值