java JAI 实现多页TIF文件拆分保存成PNG格式

本文介绍如何利用Java Advanced Imaging (JAI) API来读取TIFF文件并将其转换为PNG格式。通过示例代码展示了如何加载必要的JAI库,并逐页将TIFF文件中的图片保存为单独的PNG文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先前置工作室需要的JAI jar包有两个jai_codec.jar和jai_core.jar
maven项目可以添加(注:如果提示找不到的话就手动下载导入吧):

        <dependency>
            <groupId>javax.media</groupId>
            <artifactId>jai_codec</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>javax.media</groupId>
            <artifactId>jai_core</artifactId>
            <version>1.1.3</version>
        </dependency>

直接粘贴一下代码吧,有了下面的小例子再去看JAI就会懂很多,当然JAI图像处理还可以放缩大小,改变像素,截图,压缩,合并等等,有兴趣的可以了解一下。

public void saveTifFile(InputStream inputStream) {
     ImageDecodeParam imageDecodeParam = new TIFFDecodeParam();
     PNGEncodeParam pngEncodeParam = new PNGEncodeParam.RGB();
     ImageDecoder imageDecoder = ImageCodec.createImageDecoder("tiff", inputStream, imageDecodeParam);
     int size = 0;
     try {
         size = imageDecoder.getNumPages();
         long date = new Date().getTime();
         String filePath = filestoreConfig.getPath() + "/tifFile/" + date;
         File file = new File(filePath);
         if (!file.exists()) {
             file.mkdirs();
         }
         for (int i = 0; i < size; i++) {
             RenderedImage renderedImage = imageDecoder.decodeAsRenderedImage(i);
             ParameterBlock args = new ParameterBlock();
             String id = UUID.randomUUID().toString();
             String path = filePath + "/" + (i + 1);
             File file2 = new File(path);
             args.addSource(renderedImage);
             args.add(file2.toString());
             args.add("PNG");
             args.add(pngEncodeParam);
             RenderedOp r = JAI.create("filestore", args);
             r.dispose();
         }
     } catch (IOException e) {
         e.printStackTrace();
     }
 }

这样就可以实现了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值