多张图片合成一个tif文件

最近在项目对接过程中,遇到一个奇葩的需求,对方提出要求我们将每一类材料合成一个tif文件提交到ftp服务器上,感觉很无奈,就找一些工具类来做,并记录一下,以防遗忘。
我使用maven来管理jar包,下面是需要的两个jar包,很无奈的是我使用maven并不能将这两个jar包下载下来,所以自己去找了一下。
maven地址:

<!-- https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core -->
<dependency>
    <groupId>com.github.jai-imageio</groupId>
    <artifactId>jai-imageio-core</artifactId>
    <version>1.3.1</version>
</dependency>


<!-- https://mvnrepository.com/artifact/com.sun.media/jai-codec -->
<dependency>
    <groupId>com.sun.media</groupId>
    <artifactId>jai-codec</artifactId>
    <version>1.1.3</version>
</dependency>

如果你可以将这两个jar包下载下来,就不用去找csdn上去找了
CSDN上该jar包的地址

public static void imgToTif(List<String> filesPath, String toPath, String distFileName) {
        if (filesPath != null && filesPath.size() > 0) {
            File[] files = new File[filesPath.size()];
            for (int i = 0; i < filesPath.size(); i++) {
                files[i] = new File(filesPath.get(i));
            }
            if (files.length > 0) {
                try {
                    ArrayList<PlanarImage> pages = new ArrayList<PlanarImage>(files.length - 1);
                    FileSeekableStream[] stream = new FileSeekableStream[files.length];
                    for (int i = 0; i < files.length; i++) {
                        stream[i] = new FileSeekableStream(
                                files[i].getCanonicalPath());
                    }
                    PlanarImage firstPage = JAI.create("stream", stream[0]);
                    for (int i = 1; i < files.length; i++) {
                        PlanarImage page = JAI.create("stream", stream[i]);
                        pages.add(page);

                    }
                    TIFFEncodeParam param = new TIFFEncodeParam();
                    File f = new File(toPath);
                    if (!f.exists()) {
                        f.mkdirs();
                    }
                    OutputStream os = new FileOutputStream(toPath + File.separator + distFileName);
                    ImageEncoder enc = ImageCodec.createImageEncoder("tiff",
                            os, param);
                    param.setExtraImages(pages.iterator());
                    enc.encode(firstPage);
                    for (int i = 0; i < files.length; i++) {
                        stream[i].close();
                    }
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

测试代码:

public static void main(String[] args) throws Exception {
        File file = new File("temp/a.jpg");
        List<String> list = new ArrayList<String>();
        list.add(file.getAbsolutePath());
        list.add(file.getAbsolutePath());
        list.add(file.getAbsolutePath());
        imgToTif(list, "temp2", "a.tif");
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值