在线网站流量查询,08r2 搭建php网站,wordpress速度和cms,html5商城网页模板之前我有一篇帖子《kfb格式文件转jpg格式》讲述到 kfb tif jpg#xff0c;但是针对于超大tif中的大图是无法顺利提取的#xff0c;就算是能顺利提取#xff0c;试想一下#xff0c;2G的tif文件#xff0c;如果能提取处理最大的那张图#xff0c;并且在不压缩的… 之前我有一篇帖子《kfb格式文件转jpg格式》讲述到 kfb tif jpg但是针对于超大tif中的大图是无法顺利提取的就算是能顺利提取试想一下2G的tif文件如果能提取处理最大的那张图并且在不压缩的情况下jpg大图大小高达1G前端也是无法顺利加载展示的。 这里我为大家带来了解决方案将tif中最大的那张图切割成 x * y 份之后让前端去对每一份进行渲染加载。经过了两周时间百度或是谷歌的雷都被我踩完了搜索结果都不怎么靠谱。多次实验后总算是成功了。 针对与tif中的最大的那张图的切割我总结出了切割实现方法仅供参考。
利用jai-imageio进行处理
package com.lonzh.utils;import javax.imageio.IIOException;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.FileImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;/*** 图片切割** author DaiHaijiao*/
public class ImgCutUtil {public static void main(String[] args) throws Exception {cut(D:\\病理切片\\教学切片新\\BL-01-11 心肌梗死.tif);}/*** param imageWidth 图片宽度* param imageHeight 图片高度* param bisectionNumWidth 宽度方向等分数* param bisectionNumHeight 高度方向等分数*/private static ListImgCutUtil.Position listPosition(int imageWidth, int imageHeight, int bisectionNumWidth, int bisectionNumHeight) {ListImgCutUtil.Position list new ArrayList();//每个部分的宽度int partWidth imageWidth / bisectionNumWidth;//宽方向的余数int remainderWidth imageWidth % bisectionNumWidth;//每个部分的高度int partHeight imageHeight / bisectionNumHeight;//高方向的余数int remainderHeight imageHeight % bisectionNumHeight;for (int i 0; i bisectionNumWidth; i) {for (int j 0; j bisectionNumHeight; j) {//左下角 x 坐标int left i * partWidth;//左下角 y 坐标int bottom j * partHeight;//右上角 x 坐标int right i bisectionNumWidth - 1 ? (i 1) * partWidth remainderWidth : (i 1) * partWidth;//右上角 y 坐标int top j bisectionNumWidth - 1 ? (j 1) * partHeight remainderHeight : (j 1) * partHeight;
// System.out.println(第 (i * bisectionNumWidth j 1) 等分:);
// System.out.println(左下角坐标( left , bottom ));
// System.out.println(右上角坐标( right , top ));list.add(new ImgCutUtil.Position(left, bottom, right, top));}}return list;}public static ListFile cut(String tifPath) {ListFile jpgFileList new ArrayList();File tifFile new File(tifPath);String outPutDirPath tifPath.substring(0, tifPath.lastIndexOf(.)) _large_img File.separator;File outDirPath new File(outPutDirPath);if (!outDirPath.exists()) {outDirPath.mkdir();}try (FileImageInputStream fis new FileImageInputStream(tifFile)) {IteratorImageReader readers ImageIO.getImageReadersByFormatName(TIFF);if (!readers.hasNext()) {throw new IIOException(No suitable image reader found!);}ImageReader reader readers.next();reader.setInput(fis, true);int w reader.getWidth(0);int h reader.getHeight(0);int bisectionNumWidth 5, bisectionNumHeight 5;ListPosition positions listPosition(w, h, bisectionNumWidth, bisectionNumHeight);for (int i 0; i positions.size(); i) {ImageReadParam imageReadParam new ImageReadParam();Position position positions.get(i);System.out.println(position.left --- position.bottom --- (position.right - position.left) --- (position.top - position.bottom));imageReadParam.setSourceRegion(new Rectangle(position.left, position.bottom, position.right - position.left, position.top - position.bottom));BufferedImage image reader.read(0, imageReadParam);File file new File(outPutDirPath i .jpg);ImageIO.write(image, jpg, ImageIO.createImageOutputStream(file));jpgFileList.add(file);}} catch (IOException e) {e.printStackTrace();}return jpgFileList;}static class Position {//左下角 x 坐标private Integer left;//左下角 y 坐标private Integer bottom;//右上角 x 坐标private Integer right;//右上角 y 坐标private Integer top;public Position(Integer left, Integer bottom, Integer right, Integer top) {this.left left;this.bottom bottom;this.right right;this.top top;}Overridepublic String toString() {return Position{ left left , bottom bottom , right right , top top };}}}上述代码说明
1. 代码运行需要引用maven依赖 dependencygroupIdjavax.media.jai/groupIdartifactIdjai-imageio-core/artifactIdversion1.4.0/version/dependency
有C币的朋友可直接移步jar文件下载传送门jar引入方式见我的另一篇文章《本地Maven仓库导入外部jar》
2. main方法中的测试文件较大高达1.64G 3. 切割比较吃内存电脑配置太低时谨慎运行避免电脑卡死 4. 代码中我是将该tif切割成了 5 * 5 份切割完后会在该tif文件所在文件内产生一个新的文件夹里面就是所有切割后的小图 裁剪后的图片图号如下 图序如下 不管是几乘几的切割图序排列规则都是一样的模式如下图规则 如果你对图序感兴趣或是我的这种图序觉得前端不好使用你可以再次研究修改listPositioin方法以达到你所需的图序。
5. 计算每一份的点坐标位置时注意tif的宽和高未必能被要切割成的宽份数和高的份数整除所以注意余数问题。我的那个listPosition方法中已经对于余数进行了处理详见那块代码。
6. 代码执行超大tif文件裁剪会报错报错地方如图 报错原因就是tif的宽*高的值大于了Integer的最大值。解决办法很简单报错的这个class文件位于rt.jar下面的javax.imgio下的ImageReader.class而解决办法就在此。 我们在我们的项目中按照这个包路径创建一个包并在该包下创建这个类类里面的内容直接将ImageReader.class的内容拷贝过来之后修改报错的地方 修改完后重新编译项目就会在target下面生产一个ImageReader.class文件。我们只需要替换jar包中的这个class即可。
替换前先关闭idea找到项目所用到的rt.jar 如果打不开自己下载个7z就能打开了之后把我们编译后的那个class拖进去即可完成替换 最后重新打开idea运行代码即可