TIF文件Thumbnils生成

项目背景

在项目中希望实现对于TIF的Thumbnail的获取,但是如果在获取TIF文件后实时生成,性能方面较差。因此我采用的方案是文件获取一次,生成Thumbnail塞入TIF文件中,以提高性能那个。

功能条件:

使用开源项目:
GitHub:

<dependency>
		    <groupId>net.coobird</groupId>
		    <artifactId>thumbnailator</artifactId>
		    <version>0.4.8</version>
		</dependency>

实现方式:

public Boolean generateThumbnailsIntoFile(String fileName) throws Exception {
		Boolean result = false;
		;
		try {
			Tiff newTiff = new Tiff(fileToBytes(fileName));
			int pageCount = newTiff.getPageCount();
			for (int i = 0; i < pageCount; i++) {
				byte[] page = newTiff.getPage(i);
				if (hasThumnails(page)) {
					continue;
				}
				byte[] thumbnail = createThumbnailFile(page, newTiff.getFileName(i));
				newTiff.putThumbnail(i, thumbnail);
			}
			// replace the sourceFile
			bytesToFile(newTiff.toByteArray(), fileName);
			result = true;
		} catch (Exception e) {
			throw e;
		}
		return result;
	}

public static byte[] fileToBytes(String filePath) throws Exception {
		byte[] buffer = null;
		File file = new File(filePath);

		FileInputStream fis = new FileInputStream(file);
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] b = new byte[1024];
		int n;
		while ((n = fis.read(b)) != -1) {
			bos.write(b, 0, n);
		}
		buffer = bos.toByteArray();
		if (null != bos) {
			bos.close();
		}
		if (null != fis) {
			fis.close();
		}
		return buffer;
	}
	public Boolean hasThumnails(byte[] pageData) {
		if (pageData != null) {
			return thumbnailsFormate(pageData);
		}
		return false;
	}

public byte[] createThumbnailFile(byte[] page, String fileName) throws IOException {
		log.info("createThumbnailFile file name: " + fileName + " page=null:" + (page == null ? true : false));
		ImageIO.scanForPlugins();
		ByteArrayInputStream in = new ByteArrayInputStream(page);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		BufferedImage originalImage = ImageIO.read(in);
		Thumbnails.of(originalImage).size(200, 220).imageType(BufferedImage.TYPE_INT_RGB).outputQuality(0.5f)
				.keepAspectRatio(false).outputFormat("jpg").toOutputStream(out);
		return out.toByteArray();
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值