项目实战—OFD文件转换成图片

引言:项目需要预览OFD文件,但前端对OFD文件支持太差,因此将OFD文件直接转换成PNG格式、Base64编码的数据并返回给前端

依赖

xml 复制代码
 <dependency>
    <groupId>org.ofdrw</groupId>
    <artifactId>ofdrw-converter</artifactId>
    <version>1.17.2</version>
    <!--  如果项目已经引入了log4j,则排除log4j        -->
    <exclusions>
        <exclusion>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
        </exclusion>
    </exclusions>
</dependency>

接口

java 复制代码
/**
     * ofd文件转换成png图片

     * @return
     * @throws Exception
     */
    @GetMapping(value = "/getOfdToPng")
	public Result<List<String>> getOfdToPng() throws Exception {
        long start = System.currentTimeMillis();
        // 这里是minio访问url
        String ofdPath = "http://127.0.0.1:9000/ofd/42181c66_20240531_%E8%88%AA%E4%BD%B3%EF%BC%88%E6%8A%95%E6%A0%87%EF%BC%89%E5%AD%972024%E5%B9%B4%E7%AC%ACHJ000033%E5%8F%B7%E4%BF%9D%E5%87%BD.ofd?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=minioadmin%2F20240612%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240612T080307Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&versionId=null&X-Amz-Signature=30626032831f76333120327cba020c4aaffcf68c27912d629bf31c99d37b6aa9";
        InputStream isFromUrl = DocUtils.getIsFromUrl(ofdPath);
        List<String> list = new ArrayList<>();
        // 输入OFD文件路径
        // 输出图片路径
        try (OFDReader reader = new OFDReader(isFromUrl)) {
            // ppm 设置来指定图片生成的分辨率,数值越高图片越清晰,生成时间越慢
            ImageMaker imageMaker = new ImageMaker(reader,16);
            int pageSize = imageMaker.pageSize();
            for (int i = 0; i < pageSize; i++) {
                BufferedImage bufferedImage = imageMaker.makePage(i);
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                ImageIO.write(bufferedImage, "png", outputStream);
                byte[] imageBytes = outputStream.toByteArray();
                // 生成base64格式的图片
                String base64String = "data:image/png;base64," + Base64.getEncoder().encodeToString(imageBytes);
                outputStream.close();
                list.add(base64String);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            isFromUrl.close();
        }
        long end = System.currentTimeMillis();
        System.out.println("耗费时间:" + (end -start) * 1.0 / 1000);
        return Result.ok(list);
    }
	
	// 将远程url转换成InputStream 
	public static InputStream getIsFromUrl(String remoteUrl) throws Exception {
        byte[] fileBytes = null;
        InputStream inStream = null;
        ByteArrayOutputStream bOutStream = null;
        try {
            URL url = new URL(remoteUrl);
            HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
            httpConn.connect();
            inStream = httpConn.getInputStream();
            return inStream;
        } catch (UnknownHostException e) {
            String msg = MessageFormat.format("网络请求时发生UnknownHostException异常: {0}", e.getMessage());
            Exception ex = new Exception(msg);
            ex.initCause(e);
            throw ex;
        } catch (MalformedURLException e) {
            String msg = MessageFormat.format("网络请求时发生MalformedURLException异常: {0}", e.getMessage());
            Exception ex = new Exception(msg);
            ex.initCause(e);
            throw ex;
        } catch (IOException e) {
            String msg = MessageFormat.format("从远程Url中获取文件流时发生IOException异常: {0}", e.getMessage());
            Exception ex = new Exception(msg);
            ex.initCause(e);
            throw ex;
        }
    }

前端展示

请求接口

js 复制代码
getAction("/getOfdToPng").then(res =>{
         this.imgList = res.result
      })

因为是PNG格式、Base64编码的数据,可以直接使用img标签

html 复制代码
<div style="display:flex;justify-content:center;" v-for="img in imgList" :key="img">
     <img class="imgOfd" :src="img">
 </div>
相关推荐
程序员志哥1 天前
手把手带你使用JWT实现单点登录
springboot
小沈同学呀2 天前
【实战】EasyExcel实现百万级数据导入导出
java·大数据·spring·springboot·mybatisplus·easyexcel
程序员志哥2 天前
SpringBoot+mail 轻松实现各类邮件自动推送
springboot
宁波阿成4 天前
SpringUtils.getBean 空指针异常问题
java·springboot·flowable·jeecg-boot
飞翔的佩奇5 天前
SpringBoot3基础用法
java·开发语言·spring·maven·框架·ssm·springboot
代码匠心5 天前
从零开始学Spring Boot系列-集成Spring Security实现用户认证与授权
java·后端·springboot·spring security
Pleasure12347 天前
我给我的学校写了个校园墙小程序
java·vue.js·spring boot·小程序·vue·springboot
吃海的虾酱7 天前
【JAVA】利用Redisson和Spring实现高效物联温度控制链路,确保温度调节的准确性和效率,定时链路执行使用案例,一环扣一环
java·分布式·后端·spring·spring cloud·bootstrap·springboot
qq_35323353898 天前
【原创】springboot+mysql海鲜商城设计与实现
java·mysql·mvc·springboot·web
绝命Coding11 天前
大厂面试官问我:布隆过滤器有不能扩容和删除的缺陷,有没有可以替代的数据结构呢?【后端八股文二:布隆过滤器八股文合集】
java·redis·后端·springboot·springcloud·guava·布隆过滤器