Java小知识:多张图片合并成一张

本文就以jpg图片为例吧:

	public static void main(String[] args) {
        try {
            System.out.println("file=" + jpgsToJpg("ceshi", "url", "url"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

/**
     * 功能描述:多个jpg文件合并成一个jpg到本地
     *
     * @author songchengye
     * @date 2022/4/7 下午2:26
     */
    public static File jpgsToJpg(String newFileName, String... oldFileUrls) throws IOException {
        FileOutputStream outStream = null;
        PDDocument doc = null;
        InputStream inStream = null;
        byte[] bytes = null;
        try {
            //得到图片的二进制数据,以二进制封装得到数据,具有通用性
            List<BufferedImage> bufferedImages = new ArrayList<>();
            for (int i = 0; i < oldFileUrls.length; i++) {
                String oldFileUrl = oldFileUrls[i];
                //new一个URL对象
                URL url = new URL(oldFileUrl);
                //打开链接
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                //设置请求方式为"GET"
                conn.setRequestMethod("GET");
                //超时响应时间为5秒
                conn.setConnectTimeout(5 * 1000);
                //通过输入流获取图片数据
                inStream = conn.getInputStream();
                bufferedImages.add(ImageIO.read(inStream));
            }
            //合并多张图片为1张
            BufferedImage bufferedImage = mergeImage(false, bufferedImages);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "jpg", byteArrayOutputStream);
            bytes = byteArrayOutputStream.toByteArray();
            //指定文件路径
            String path = System.getProperty("user.dir") + "-" + newFileName + ".jpg";
            log.info("path=" + path);
            File jpgFile = new File(path);
            //为读取文件提供输出流通道
            outStream = new FileOutputStream(jpgFile);
            //写入数据
            outStream.write(bytes);
            return jpgFile;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭输出流
            if (outStream != null) {
                outStream.close();
            }
            //关闭输入流
            if (inStream != null) {
                inStream.close();
            }
            if (doc != null) {
                doc.close();
            }
        }
        return null;
    }

/**
 * 合成任意数量的图片
 *
 * @param isHorizontal
 * @param imgs
 * @return
 * @throws IOException
 */
public static BufferedImage mergeImage(boolean isHorizontal, List<BufferedImage> imgs) throws IOException {
    // 生成新图片
    BufferedImage DestImage = null;
    // 计算新图片的长和高
    int allw = 0, allh = 0, allwMax = 0, allhMax = 0;
    for (BufferedImage img : imgs) {
        allw += img.getWidth();
        allh += img.getHeight();
        if (img.getWidth() > allwMax) {
            allwMax = img.getWidth();
        }
        if (img.getHeight() > allhMax) {
            allhMax = img.getHeight();
        }
    }
    // 创建新图片
    if (isHorizontal) {
        DestImage = new BufferedImage(allw, allhMax, BufferedImage.TYPE_INT_RGB);
    } else {
        DestImage = new BufferedImage(allwMax, allh, BufferedImage.TYPE_INT_RGB);
    }

    // 合并所有子图片到新图片
    int wx = 0, wy = 0;
    for (BufferedImage img : imgs) {
        int w1 = img.getWidth();
        int h1 = img.getHeight();
        // 从图片中读取RGB
        int[] ImageArrayOne = new int[w1 * h1];
        ImageArrayOne = img.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
        if (isHorizontal) { // 水平方向合并
            DestImage.setRGB(wx, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
        } else { // 垂直方向合并
            DestImage.setRGB(0, wy, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
        }
        wx += w1;
        wy += h1;
    }
    return DestImage;
}
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值