springboot读取tif图片转为png在前端预览
我这里是读取tif后转为png,再转为base64直接传给前端。
在线预览base64的地址:http://www.ecomcn.com/tool/Base64/
文件目录结构:
代码:
@Overridepublic List<YbglSetYbSPlitListVo> ybglSetYbSPlitList(String outPath) {if (outPath == null || outPath.equals("")) {return null;}List<YbglSetYbSPlitListVo> ybSPlitListVos = new ArrayList<>();// 跟目录F:\\tempString directory = uploadConfig.getLocalStorageDirectory();File outDir = new File(directory + outPath);// 遍历目录下的文件夹for (File file : Objects.requireNonNull(outDir.listFiles())) {if (file.isDirectory()) {// 只遍历文件夹// 文件夹名用作返回数据的type,在前端区分切片的类型String fileName = file.getName();System.out.println("文件夹: " + fileName);for (File subFile : Objects.requireNonNull(file.listFiles())) {System.out.println(subFile);String[] names = subFile.getName().split("\\.");String[] names2 = subFile.getName().split("_");if (names.length > 1 && "tif".equalsIgnoreCase(names[names.length - 1])) {// 目前只返回tif文件YbglSetYbSPlitListVo ybglSetYbSPlitListVo = new YbglSetYbSPlitListVo();ybglSetYbSPlitListVo.setType(fileName);ybglSetYbSPlitListVo.setYbId(names2[0]);ybglSetYbSPlitListVo.setName(names2[1]);try {// 读取 TIFF 文件BufferedImage image = ImageIO.read(subFile);// 将 BufferedImage 转为 PNG 格式的字节数组ByteArrayOutputStream baos = new ByteArrayOutputStream();ImageIO.write(image, "png", baos);baos.flush();byte[] imageBytes = baos.toByteArray();baos.close();// 将字节数组编码为 Base64 字符串String base64String = Base64.getEncoder().encodeToString(imageBytes);// 转化为base64String thumbBase64 = "data:image/png;base64," + base64String;ybglSetYbSPlitListVo.setThumb(thumbBase64);} catch (IOException e) {e.printStackTrace();}ybSPlitListVos.add(ybglSetYbSPlitListVo);}}}}return ybSPlitListVos;}
前端直接放img预览:
<img :src="item.thumb">
接口返回格式: