!793 修复图片分割方法ImgUtil.sliceByRowsAndCols()导致分割后图片失真问题

Merge pull request !793 from 小白仙人/v5-master
This commit is contained in:
Looly 2022-09-05 09:14:53 +00:00 committed by Gitee
commit 366a0e5c93
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 7 additions and 5 deletions

View File

@ -439,12 +439,13 @@ public class ImgUtil {
*
* @param srcImageFile 源图像文件
* @param destDir 切片目标文件夹
* @param format 目标文件格式
* @param rows 目标切片行数默认2必须是范围 [1, 20] 之内
* @param cols 目标切片列数默认2必须是范围 [1, 20] 之内
*/
public static void sliceByRowsAndCols(File srcImageFile, File destDir, int rows, int cols) {
public static void sliceByRowsAndCols(File srcImageFile, File destDir, String format, int rows, int cols) {
try {
sliceByRowsAndCols(ImageIO.read(srcImageFile), destDir, rows, cols);
sliceByRowsAndCols(ImageIO.read(srcImageFile), destDir, format, rows, cols);
} catch (IOException e) {
throw new IORuntimeException(e);
}
@ -455,10 +456,11 @@ public class ImgUtil {
*
* @param srcImage 源图像如果非{@link BufferedImage}则默认使用RGB模式
* @param destDir 切片目标文件夹
* @param format 目标文件格式
* @param rows 目标切片行数默认2必须是范围 [1, 20] 之内
* @param cols 目标切片列数默认2必须是范围 [1, 20] 之内
*/
public static void sliceByRowsAndCols(Image srcImage, File destDir, int rows, int cols) {
public static void sliceByRowsAndCols(Image srcImage, File destDir, String format, int rows, int cols) {
if (false == destDir.exists()) {
FileUtil.mkdir(destDir);
} else if (false == destDir.isDirectory()) {
@ -486,7 +488,7 @@ public class ImgUtil {
for (int j = 0; j < cols; j++) {
tag = cut(bi, new Rectangle(j * destWidth, i * destHeight, destWidth, destHeight));
// 输出为文件
ImageIO.write(toRenderedImage(tag), IMAGE_TYPE_JPEG, new File(destDir, "_r" + i + "_c" + j + ".jpg"));
ImageIO.write(toRenderedImage(tag), format, new File(destDir, "_r" + i + "_c" + j + "." + format));
}
}
} catch (IOException e) {

View File

@ -89,7 +89,7 @@ public class ImgUtilTest {
@Test
@Ignore
public void sliceByRowsAndColsTest() {
ImgUtil.sliceByRowsAndCols(FileUtil.file("d:/test/logo.jpg"), FileUtil.file("d:/test/dest"), 1, 5);
ImgUtil.sliceByRowsAndCols(FileUtil.file("d:/temp/2.png"), FileUtil.file("d:/temp/slice/png"),ImgUtil.IMAGE_TYPE_PNG, 1, 5);
}
@Test