当前位置:   article > 正文

ofd文件拆分图像_ofd拆分jpg

ofd拆分jpg

将ofd文件拆分多张图像,pom文件引入依赖包

<dependency>
    <groupId>org.ofdrw</groupId>
    <artifactId>ofdrw-converter</artifactId>
    <version>1.19.1</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>fontbox</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
        </exclusion>
    </exclusions>
</dependency>

以下两种方式将ofd文件图拆分成多张图像

方式一

try (OFDReader reader = new OFDReader("填写文件路径")) {
    ImageMaker imageMaker = new ImageMaker(reader, 15);
    for (int i = 0; i < imageMaker.pageSize(); i++) {
        // 指定页码转换图片
        num = num + 1;
        BufferedImage image = imageMaker.makePage(i);
        //保存jpg图像
        File imgFile = new File(String.join(File.separator, baseFilePath, "filePath", path, num + ".jpg"));

        // 存储为指定格式图片
        ImageIO.write(image, "JPG", imgFile);

} catch (Exception e) {
    e.printStackTrace();
}

方式二(比较推荐)

import org.apache.commons.io.FileUtils;
import org.ofdrw.converter.ImageMaker;
import org.ofdrw.reader.OFDReader;
import org.springframework.util.Base64Utils;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import java.util.Base64;

public class OfdUtils {


    public static final Base64.Decoder DECODER = Base64.getDecoder();

    private OfdUtils() {
    }


    public static void main(String[] a) throws Throwable{
        String ofd = "/project/00001.ofd";
        File imageFile = new File(ofd);
        String img = Base64Utils.encodeToString(FileUtils.readFileToByteArray(imageFile));
        ofdToImage(img);
    }
    

    /***
     * ofdBase64转图片,返回图片对象
     *
     * @param ofdBase64  pdf类型base64
     * @return BufferedImage
     */
    public static void ofdToImage(String ofdBase64){
        byte[] decode = DECODER.decode(ofdBase64.getBytes(StandardCharsets.UTF_8));
        int num = 0;
        try(
                ByteArrayInputStream stream = new ByteArrayInputStream(decode);
                OFDReader reader = new OFDReader(stream);
        ) {
            ImageMaker imageMaker = new ImageMaker(reader, 15);
            for (int i = 0; i < imageMaker.pageSize(); i++) {
                // 指定页码转换图片
                //list.add(imageMaker.makePage(i));
                num = num + 1;
                BufferedImage image = imageMaker.makePage(i);

                //保存jpg 数据信息
                File imgFile = new File("/project" + File.separator + num + ".jpg");
                // 5. 存储为指定格式图片
                ImageIO.write(image, "JPG", imgFile);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/781834
推荐阅读
相关标签
  

闽ICP备14008679号