openslide / openslide-java

Java bindings to OpenSlide
https://openslide.org/
GNU Lesser General Public License v2.1
40 stars 24 forks source link

openslide-java parses the rectangular pathological image into the format required by deep zoom #56

Open h616016784 opened 1 year ago

h616016784 commented 1 year ago
OpenSlide os = null;
        try {
            os = new OpenSlide(infile);
            int levelCount = os.getLevelCount();
            // 切割
            for (int n = levelCount - 1; n > -1; n--) { //n表示层级,也表示需要创建的文件夹名字
                int nHeight = (int) (os.getLevel0Height() / os.getLevelHeight(n)); //切高的次数
                int nWidth = (int) (os.getLevel0Width() / os.getLevelWidth(n)); //切长的次数
                for (int j = 0; j < nWidth; j++) {  //循环切长
                    for (int i = 0; i < nHeight; i++) {  //循环切高
                        BufferedImage th = os.createThumbnailImage((int) (j * os.getLevelWidth(n)), (int) (i * os.getLevelHeight(n)), os.getLevelWidth(n), os.getLevelHeight(n), imgSize);  //开始切图
                        // 输出文件到对应目录
                        //创建目录 nameWithoutExtension + Constants.UNDERSCORE +
                        String resultName = outfile + File.separator + (n+9) + File.separator +  j + Constants.UNDERSCORE + i + Constants.DOT + imgType;
                        File file = new File(outfile + File.separator + (n+9));//文件的名称第9层开始
                        if (!file.exists()) {
                            System.out.println("文件不存在");
                            file.mkdirs();//创建失败会抛出异常throw new IOException("Invalid file path");
                        }
                        ImageIO.write(th, imgType, new File(resultName));
                        System.out.println("保存成功:" + resultName);
                    }
                }
            }

i use the code parses the pathological image ,it works if the image is square; However it is wrong if the image is rectangular,so Do you have code examples?thanks!