java 多个单页的tif文件 合并成 一个多页的tif文件

maven依赖

<!-- tif 合并 -->
        <dependency>
            <groupId>com.sun.media</groupId>
            <artifactId>jai_codec</artifactId>
            <version>1.1-mr</version>
        </dependency>

或者

 <dependency>
            <groupId>com.sun</groupId>
            <artifactId>jai_codec</artifactId>
            <version>1.1.3</version>
        </dependency>

实现方法

    /**
     * tif合并
     * @Author chenqi
     * @Date 14:46 2020/8/6
     * @param bytes
     * @return byte[]
     **/
    public static byte[] margerTif(List<byte[]> bytes){
        ByteArrayInputStream bin = null;
        List<BufferedImage> images = new ArrayList<>();
        for(byte[] b : bytes) {
            try {
                bin = new ByteArrayInputStream(b);
                BufferedImage image = ImageIO.read(bin);
                images.add(image);
                bin.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream(OmsConstants.Number.ONE_MB);
        try{
            TIFFEncodeParam params = new TIFFEncodeParam();
            params.setCompression(TIFFEncodeParam.COMPRESSION_JPEG_TTN2);
            ImageEncoder encoder = ImageCodec.createImageEncoder("tiff", out, params);
            List<BufferedImage> imageList = new ArrayList<>();
            for (int i = 1; i < images.size(); i++){
                imageList.add(images.get(i));
            }
            params.setExtraImages(imageList.iterator());
            encoder.encode(images.get(0));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return out.toByteArray();
    }

测试方法

    public static void main(String[] args) {
        File file1 = new File("D:/TIF/1.tif");
        File file2 = new File("D:/TIF/2.tif");
        File file3 = new File("D:/TIF/3.tif");
        try {
            List<byte[]> list = new ArrayList<>();
            list.add(Files.readAllBytes(file1.toPath()));
            list.add(Files.readAllBytes(file2.toPath()));
            list.add(Files.readAllBytes(file3.toPath()));
            byte[] b = MesFileUtil.margerTif(list);
            FileOutputStream ou = new FileOutputStream(new File("D:/PDF/123.tif"));
            ou.write(b);
            ou.flush();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

jar下载不了的兄弟,可以点赞收藏然后留言邮箱,一般48小时内会发。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值