深圳網(wǎng)站建設(shè)公司的英文名是網(wǎng)站seo分析工具
? ? ? ? 之前我有一篇帖子《kfb格式文件轉(zhuǎn)jpg格式》講述到 kfb ===> tif ===> jpg,但是針對于超大tif中的大圖是無法順利提取的,就算是能順利提取,試想一下,2G的tif文件,如果能提取處理最大的那張圖,并且在不壓縮的情況下,jpg大圖大小高達1G,前端也是無法順利加載展示的。
????????這里我為大家?guī)砹私鉀Q方案:將tif中最大的那張圖切割成 x * y?份,之后讓前端去對每一份進行渲染加載。經(jīng)過了兩周時間,百度或是谷歌的雷都被我踩完了(搜索結(jié)果都不怎么靠譜)。多次實驗后,總算是成功了。
????????針對與tif中的最大的那張圖的切割,我總結(jié)出了切割實現(xiàn)方法,僅供參考。
利用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 寬度方向等分數(shù)* @param bisectionNumHeight 高度方向等分數(shù)*/private static List<ImgCutUtil.Position> listPosition(int imageWidth, int imageHeight, int bisectionNumWidth, int bisectionNumHeight) {List<ImgCutUtil.Position> list = new ArrayList<>();//每個部分的寬度int partWidth = imageWidth / bisectionNumWidth;//寬方向的余數(shù)int remainderWidth = imageWidth % bisectionNumWidth;//每個部分的高度int partHeight = imageHeight / bisectionNumHeight;//高方向的余數(shù)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 List<File> cut(String tifPath) {List<File> 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)) {Iterator<ImageReader> 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;List<Position> 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依賴
<dependency><groupId>javax.media.jai</groupId><artifactId>jai-imageio-core</artifactId><version>1.4.0</version></dependency>
有C幣的朋友可直接移步j(luò)ar文件下載傳送門(jar引入方式見:我的另一篇文章《本地Maven倉庫導入外部jar》)
2.?main方法中的測試文件較大,高達1.64G
3.?切割比較吃內(nèi)存,電腦配置太低時謹慎運行,避免電腦卡死
4.?代碼中我是將該tif切割成了 5 * 5?份,切割完后會在該tif文件所在文件內(nèi)產(chǎn)生一個新的文件夾(里面就是所有切割后的小圖)
裁剪后的圖片圖號如下:
圖序如下:
不管是幾乘幾的切割,圖序排列規(guī)則都是一樣的模式,如下圖規(guī)則:
如果你對圖序感興趣,或是我的這種圖序覺得前端不好使用,你可以再次研究修改listPositioin方法以達到你所需的圖序。
5.?計算每一份的點坐標位置時注意:tif的寬和高未必能被要切割成的寬份數(shù)和高的份數(shù)整除,所以注意余數(shù)問題。我的那個listPosition方法中已經(jīng)對于余數(shù)進行了處理,詳見那塊代碼。
6.?代碼執(zhí)行超大tif文件裁剪會報錯,報錯地方如圖:
????????報錯原因就是tif的寬*高的值大于了Integer的最大值。解決辦法很簡單,報錯的這個class文件位于rt.jar下面的javax.imgio下的ImageReader.class,而解決辦法就在此。
? ? ? ? 我們在我們的項目中按照這個包路徑創(chuàng)建一個包,并在該包下創(chuàng)建這個類,類里面的內(nèi)容直接將ImageReader.class的內(nèi)容拷貝過來,之后修改報錯的地方:
修改完后重新編譯項目,就會在target下面生產(chǎn)一個ImageReader.class文件。我們只需要替換jar包中的這個class即可。
替換前先關(guān)閉idea,找到項目所用到的rt.jar
如果打不開,自己下載個7z就能打開了,之后把我們編譯后的那個class拖進去即可完成替換
最后重新打開idea運行代碼即可
? ? ? ?
????????