From 7bb487419c9d3b9ff77016669857170a9040b1ce Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 14 Nov 2024 14:10:27 +0800 Subject: [PATCH] fix code --- .../hutool/core/io/file/FileTypeUtilTest.java | 2 +- .../dromara/hutool/db/PicTransferTest.java | 2 +- .../hutool/extra/qrcode/QrCodeUtilTest.java | 6 +- .../org/dromara/hutool/http/DownloadTest.java | 6 +- .../hutool/poi/excel/ExcelImgUtil.java | 145 ------------------ .../excel/cell/setters/CellSetterFactory.java | 4 +- ...{ImgCellSetter.java => PicCellSetter.java} | 24 +-- .../hutool/poi/excel/reader/ExcelReader.java | 16 +- .../ExcelPicType.java} | 6 +- .../hutool/poi/excel/shape/ExcelPicUtil.java | 130 ++++++++++++++++ .../hutool/poi/excel/shape/package-info.java | 20 +++ .../poi/excel/writer/ExcelDrawingUtil.java | 6 +- .../hutool/poi/excel/writer/ExcelWriter.java | 20 +-- .../poi/excel/shape/ExcelPicUtilTest.java | 29 ++++ .../poi/excel/writer/ExcelWriteTest.java | 2 +- .../org/dromara/hutool/swing/img/ImgTest.java | 4 +- .../dromara/hutool/swing/img/ImgUtilTest.java | 10 +- 17 files changed, 238 insertions(+), 194 deletions(-) delete mode 100644 hutool-poi/src/main/java/org/dromara/hutool/poi/excel/ExcelImgUtil.java rename hutool-poi/src/main/java/org/dromara/hutool/poi/excel/cell/setters/{ImgCellSetter.java => PicCellSetter.java} (72%) rename hutool-poi/src/main/java/org/dromara/hutool/poi/excel/{ExcelImgType.java => shape/ExcelPicType.java} (93%) create mode 100644 hutool-poi/src/main/java/org/dromara/hutool/poi/excel/shape/ExcelPicUtil.java create mode 100644 hutool-poi/src/main/java/org/dromara/hutool/poi/excel/shape/package-info.java create mode 100644 hutool-poi/src/test/java/org/dromara/hutool/poi/excel/shape/ExcelPicUtilTest.java diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/io/file/FileTypeUtilTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/io/file/FileTypeUtilTest.java index f5f31f022..ba1840387 100644 --- a/hutool-core/src/test/java/org/dromara/hutool/core/io/file/FileTypeUtilTest.java +++ b/hutool-core/src/test/java/org/dromara/hutool/core/io/file/FileTypeUtilTest.java @@ -83,7 +83,7 @@ public class FileTypeUtilTest { @Test @Disabled public void getTypeFromInputStream() throws IOException { - final File file = FileUtil.file("d:/test/pic.jpg"); + final File file = FileUtil.file("d:/test/shape.jpg"); final BufferedInputStream inputStream = FileUtil.getInputStream(file); inputStream.mark(0); final String type = FileTypeUtil.getType(inputStream); diff --git a/hutool-db/src/test/java/org/dromara/hutool/db/PicTransferTest.java b/hutool-db/src/test/java/org/dromara/hutool/db/PicTransferTest.java index 2fe65aa7c..0bfff54eb 100644 --- a/hutool-db/src/test/java/org/dromara/hutool/db/PicTransferTest.java +++ b/hutool-db/src/test/java/org/dromara/hutool/db/PicTransferTest.java @@ -43,7 +43,7 @@ public class PicTransferTest { } private static void save(final ResultSet rs) throws SQLException{ - final String destDir = "d:/test/pic"; + final String destDir = "d:/test/shape"; final String path = StrUtil.format("{}/{}-{}.jpg", destDir, rs.getString("NAME"), rs.getString("GROUP")); FileUtil.copy(rs.getBlob("PIC").getBinaryStream(), FileUtil.file(path)); } diff --git a/hutool-extra/src/test/java/org/dromara/hutool/extra/qrcode/QrCodeUtilTest.java b/hutool-extra/src/test/java/org/dromara/hutool/extra/qrcode/QrCodeUtilTest.java index a894b863f..c0a31645e 100644 --- a/hutool-extra/src/test/java/org/dromara/hutool/extra/qrcode/QrCodeUtilTest.java +++ b/hutool-extra/src/test/java/org/dromara/hutool/extra/qrcode/QrCodeUtilTest.java @@ -66,7 +66,7 @@ public class QrCodeUtilTest { @Test @Disabled public void generateWithLogoTest() { - final String icon = FileUtil.isWindows() ? "d:/test/pic/logo.jpg" : "~/Desktop/hutool/pic/logo.jpg"; + final String icon = FileUtil.isWindows() ? "d:/test/shape/logo.jpg" : "~/Desktop/hutool/shape/logo.jpg"; final String targetPath = FileUtil.isWindows() ? "d:/test/qrcodeWithLogo.jpg" : "~/Desktop/hutool/qrcodeWithLogo.jpg"; QrCodeUtil.generate(// "https://hutool.cn/", // @@ -77,7 +77,7 @@ public class QrCodeUtilTest { @Test @Disabled public void decodeTest() { - final String decode = QrCodeUtil.decode(FileUtil.file("d:/test/pic/qr.png")); + final String decode = QrCodeUtil.decode(FileUtil.file("d:/test/shape/qr.png")); Console.log(decode); } @@ -141,7 +141,7 @@ public class QrCodeUtilTest { @Disabled public void generateSvgTest() { final QrConfig qrConfig = QrConfig.of() - .setImg("d:/test/pic/logo.jpg") + .setImg("d:/test/shape/logo.jpg") .setForeColor(Color.blue) .setBackColor(Color.pink) .setRatio(8) diff --git a/hutool-http/src/test/java/org/dromara/hutool/http/DownloadTest.java b/hutool-http/src/test/java/org/dromara/hutool/http/DownloadTest.java index 5742d733c..8e3ed196d 100644 --- a/hutool-http/src/test/java/org/dromara/hutool/http/DownloadTest.java +++ b/hutool-http/src/test/java/org/dromara/hutool/http/DownloadTest.java @@ -47,7 +47,7 @@ public class DownloadTest { @Disabled public void downloadPicTest() { final String url = "http://wx.qlogo.cn/mmopen/vKhlFcibVUtNBVDjcIowlg0X8aJfHXrTNCEFBukWVH9ta99pfEN88lU39MKspCUCOP3yrFBH3y2NbV7sYtIIlon8XxLwAEqv2/0"; - HttpDownloader.downloadFile(url, new File("e:/pic/t3.jpg")); + HttpDownloader.downloadFile(url, new File("e:/shape/t3.jpg")); Console.log("ok"); } @@ -56,8 +56,8 @@ public class DownloadTest { @Disabled public void downloadSizeTest() { final String url = "https://res.t-io.org/im/upload/img/67/8948/1119501/88097554/74541310922/85/231910/366466 - 副本.jpg"; - ClientEngineFactory.getEngine().send(Request.of(url)).body().write("e:/pic/366466.jpg"); - //HttpRequest.get(url).setSSLProtocol("TLSv1.2").executeAsync().body().write("e:/pic/366466.jpg"); + ClientEngineFactory.getEngine().send(Request.of(url)).body().write("e:/shape/366466.jpg"); + //HttpRequest.get(url).setSSLProtocol("TLSv1.2").executeAsync().body().write("e:/shape/366466.jpg"); } @Test diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/ExcelImgUtil.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/ExcelImgUtil.java deleted file mode 100644 index 7afcc61db..000000000 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/ExcelImgUtil.java +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Copyright (c) 2013-2024 Hutool Team and hutool.cn - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.dromara.hutool.poi.excel; - -import org.apache.poi.hssf.usermodel.*; -import org.apache.poi.ooxml.POIXMLDocumentPart; -import org.apache.poi.ss.usermodel.*; -import org.apache.poi.xssf.usermodel.*; -import org.dromara.hutool.core.collection.CollUtil; -import org.dromara.hutool.core.io.file.FileTypeUtil; -import org.dromara.hutool.core.lang.Assert; -import org.dromara.hutool.core.map.multi.ListValueMap; -import org.dromara.hutool.core.text.StrUtil; -import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker; - -import java.io.File; -import java.util.List; - -/** - * Excel图片工具类 - * - * @author looly - * @since 4.0.7 - */ -public class ExcelImgUtil { - - /** - * 获取图片类型 - * - * @param imgFile 图片文件 - * @return 图片类型,默认PNG - * @since 6.0.0 - */ - public static ExcelImgType getImgType(final File imgFile) { - final String type = FileTypeUtil.getType(imgFile); - if (StrUtil.equalsAnyIgnoreCase(type, "jpg", "jpeg")) { - return ExcelImgType.JPEG; - } else if (StrUtil.equalsAnyIgnoreCase(type, "emf")) { - return ExcelImgType.EMF; - } else if (StrUtil.equalsAnyIgnoreCase(type, "wmf")) { - return ExcelImgType.WMF; - } else if (StrUtil.equalsAnyIgnoreCase(type, "pict")) { - return ExcelImgType.PICT; - } else if (StrUtil.equalsAnyIgnoreCase(type, "dib")) { - return ExcelImgType.DIB; - } - - // 默认格式 - return ExcelImgType.PNG; - } - - /** - * 获取工作簿指定sheet中图片列表 - * - * @param workbook 工作簿{@link Workbook} - * @param sheetIndex sheet的索引 - * @return 图片映射,键格式:行_列,值:{@link PictureData} - */ - public static ListValueMap getPicMap(final Workbook workbook, int sheetIndex) { - Assert.notNull(workbook, "Workbook must be not null !"); - if (sheetIndex < 0) { - sheetIndex = 0; - } - - if (workbook instanceof HSSFWorkbook) { - return getPicMapXls((HSSFWorkbook) workbook, sheetIndex); - } else if (workbook instanceof XSSFWorkbook) { - return getPicMapXlsx((XSSFWorkbook) workbook, sheetIndex); - } else { - throw new IllegalArgumentException(StrUtil.format("Workbook type [{}] is not supported!", workbook.getClass())); - } - } - - // -------------------------------------------------------------------------------------------------------------- Private method start - - /** - * 获取XLS工作簿指定sheet中图片列表 - * - * @param workbook 工作簿{@link Workbook} - * @param sheetIndex sheet的索引 - * @return 图片映射,键格式:行_列,值:{@link PictureData} - */ - private static ListValueMap getPicMapXls(final HSSFWorkbook workbook, final int sheetIndex) { - final ListValueMap picMap = new ListValueMap<>(); - final List pictures = workbook.getAllPictures(); - if (CollUtil.isNotEmpty(pictures)) { - final HSSFSheet sheet = workbook.getSheetAt(sheetIndex); - HSSFClientAnchor anchor; - int pictureIndex; - for (final HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) { - if (shape instanceof HSSFPicture) { - pictureIndex = ((HSSFPicture) shape).getPictureIndex() - 1; - anchor = (HSSFClientAnchor) shape.getAnchor(); - picMap.putValue(StrUtil.format("{}_{}", anchor.getRow1(), anchor.getCol1()), pictures.get(pictureIndex)); - } - } - } - return picMap; - } - - /** - * 获取XLSX工作簿指定sheet中图片列表 - * - * @param workbook 工作簿{@link Workbook} - * @param sheetIndex sheet的索引 - * @return 图片映射,键格式:行_列,值:{@link PictureData} - */ - private static ListValueMap getPicMapXlsx(final XSSFWorkbook workbook, final int sheetIndex) { - final ListValueMap sheetIndexPicMap = new ListValueMap<>(); - final XSSFSheet sheet = workbook.getSheetAt(sheetIndex); - XSSFDrawing drawing; - for (final POIXMLDocumentPart dr : sheet.getRelations()) { - if (dr instanceof XSSFDrawing) { - drawing = (XSSFDrawing) dr; - final List shapes = drawing.getShapes(); - XSSFPicture pic; - CTMarker ctMarker; - for (final XSSFShape shape : shapes) { - if (shape instanceof XSSFPicture) { - pic = (XSSFPicture) shape; - ctMarker = pic.getPreferredSize().getFrom(); - sheetIndexPicMap.putValue(StrUtil.format("{}_{}", ctMarker.getRow(), ctMarker.getCol()), pic.getPictureData()); - } - // 其他类似于图表等忽略,see: https://gitee.com/dromara/hutool/issues/I38857 - } - } - } - return sheetIndexPicMap; - } - // -------------------------------------------------------------------------------------------------------------- Private method end -} diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/cell/setters/CellSetterFactory.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/cell/setters/CellSetterFactory.java index 725e9f634..290b9f03a 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/cell/setters/CellSetterFactory.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/cell/setters/CellSetterFactory.java @@ -59,9 +59,9 @@ public class CellSetterFactory { return new HyperlinkCellSetter((Hyperlink) value); }else if (value instanceof byte[]) { // 二进制理解为图片 - return new ImgCellSetter((byte[]) value); + return new PicCellSetter((byte[]) value); }else if (value instanceof File) { - return new ImgCellSetter((File) value); + return new PicCellSetter((File) value); } else { return new CharSequenceCellSetter(value.toString()); } diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/cell/setters/ImgCellSetter.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/cell/setters/PicCellSetter.java similarity index 72% rename from hutool-poi/src/main/java/org/dromara/hutool/poi/excel/cell/setters/ImgCellSetter.java rename to hutool-poi/src/main/java/org/dromara/hutool/poi/excel/cell/setters/PicCellSetter.java index f78aa05e3..26d494af2 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/cell/setters/ImgCellSetter.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/cell/setters/PicCellSetter.java @@ -18,8 +18,8 @@ package org.dromara.hutool.poi.excel.cell.setters; import org.apache.poi.ss.usermodel.*; import org.dromara.hutool.core.io.file.FileUtil; -import org.dromara.hutool.poi.excel.ExcelImgType; -import org.dromara.hutool.poi.excel.ExcelImgUtil; +import org.dromara.hutool.poi.excel.shape.ExcelPicType; +import org.dromara.hutool.poi.excel.shape.ExcelPicUtil; import org.dromara.hutool.poi.excel.SimpleClientAnchor; import org.dromara.hutool.poi.excel.writer.ExcelDrawingUtil; @@ -31,10 +31,10 @@ import java.io.File; * @author Looly * @since 6.0.0 */ -public class ImgCellSetter implements CellSetter { +public class PicCellSetter implements CellSetter { private final byte[] pictureData; - private final ExcelImgType imgType; + private final ExcelPicType picType; // region ----- 构造 @@ -43,8 +43,8 @@ public class ImgCellSetter implements CellSetter { * * @param pictureData 图片数据 */ - public ImgCellSetter(final byte[] pictureData) { - this(pictureData, ExcelImgType.PNG); + public PicCellSetter(final byte[] pictureData) { + this(pictureData, ExcelPicType.PNG); } /** @@ -52,19 +52,19 @@ public class ImgCellSetter implements CellSetter { * * @param picturefile 图片数据 */ - public ImgCellSetter(final File picturefile) { - this(FileUtil.readBytes(picturefile), ExcelImgUtil.getImgType(picturefile)); + public PicCellSetter(final File picturefile) { + this(FileUtil.readBytes(picturefile), ExcelPicUtil.getPicType(picturefile)); } /** * 构造 * * @param pictureData 图片数据 - * @param imgType 图片类型 + * @param picType 图片类型 */ - public ImgCellSetter(final byte[] pictureData, final ExcelImgType imgType) { + public PicCellSetter(final byte[] pictureData, final ExcelPicType picType) { this.pictureData = pictureData; - this.imgType = imgType; + this.picType = picType; } // endregion @@ -74,7 +74,7 @@ public class ImgCellSetter implements CellSetter { final int columnIndex = cell.getColumnIndex(); final int rowIndex = cell.getRowIndex(); - ExcelDrawingUtil.drawingImg(sheet, this.pictureData, this.imgType, + ExcelDrawingUtil.drawingPic(sheet, this.pictureData, this.picType, new SimpleClientAnchor(columnIndex, rowIndex, columnIndex + 1, rowIndex + 1)); } } diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/ExcelReader.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/ExcelReader.java index 37798f54c..62d072e0c 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/ExcelReader.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/reader/ExcelReader.java @@ -17,10 +17,7 @@ package org.dromara.hutool.poi.excel.reader; import org.apache.poi.ss.extractor.ExcelExtractor; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.*; import org.dromara.hutool.core.func.SerBiConsumer; import org.dromara.hutool.core.io.IoUtil; import org.dromara.hutool.core.io.file.FileUtil; @@ -28,6 +25,7 @@ import org.dromara.hutool.core.lang.Assert; import org.dromara.hutool.poi.excel.*; import org.dromara.hutool.poi.excel.cell.CellUtil; import org.dromara.hutool.poi.excel.reader.sheet.*; +import org.dromara.hutool.poi.excel.shape.ExcelPicUtil; import org.dromara.hutool.poi.excel.writer.ExcelWriter; import java.io.File; @@ -364,6 +362,16 @@ public class ExcelReader extends ExcelBase { return CellUtil.getCellValue(getCell(x, y), this.config.getCellEditor()); } + /** + * 读取绘制的图片列表 + * + * @return 图片列表 + * @since 6.0.0 + */ + public List readPics() { + return ExcelPicUtil.getShapePics(this.sheet); + } + /** * 获取Excel写出器
* 在读取Excel并做一定编辑后,获取写出器写出,规则如下: diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/ExcelImgType.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/shape/ExcelPicType.java similarity index 93% rename from hutool-poi/src/main/java/org/dromara/hutool/poi/excel/ExcelImgType.java rename to hutool-poi/src/main/java/org/dromara/hutool/poi/excel/shape/ExcelPicType.java index 1db7dabf6..e1cc44358 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/ExcelImgType.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/shape/ExcelPicType.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.dromara.hutool.poi.excel; +package org.dromara.hutool.poi.excel.shape; import org.apache.poi.ss.usermodel.Workbook; @@ -30,7 +30,7 @@ import org.apache.poi.ss.usermodel.Workbook; * @see Workbook#PICTURE_TYPE_DIB * @since 6.0.0 */ -public enum ExcelImgType { +public enum ExcelPicType { /** * Extended windows meta file */ @@ -68,7 +68,7 @@ public enum ExcelImgType { * * @param value 类型编码 */ - ExcelImgType(final int value) { + ExcelPicType(final int value) { this.value = value; } diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/shape/ExcelPicUtil.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/shape/ExcelPicUtil.java new file mode 100644 index 000000000..bae4cec0e --- /dev/null +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/shape/ExcelPicUtil.java @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2013-2024 Hutool Team and hutool.cn + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dromara.hutool.poi.excel.shape; + +import org.apache.poi.ss.usermodel.*; +import org.dromara.hutool.core.collection.ListUtil; +import org.dromara.hutool.core.io.file.FileTypeUtil; +import org.dromara.hutool.core.io.file.FileUtil; +import org.dromara.hutool.core.lang.Assert; +import org.dromara.hutool.core.stream.StreamUtil; +import org.dromara.hutool.core.text.StrUtil; + +import java.io.File; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Excel图片工具类 + * + * @author looly + * @since 4.0.7 + */ +public class ExcelPicUtil { + + /** + * 获取图片类型 + * + * @param picFile 图片文件 + * @return 图片类型,默认PNG + * @since 6.0.0 + */ + public static ExcelPicType getPicType(final File picFile) { + final String type = FileTypeUtil.getType(picFile); + if (StrUtil.equalsAnyIgnoreCase(type, "jpg", "jpeg")) { + return ExcelPicType.JPEG; + } else if (StrUtil.equalsAnyIgnoreCase(type, "emf")) { + return ExcelPicType.EMF; + } else if (StrUtil.equalsAnyIgnoreCase(type, "wmf")) { + return ExcelPicType.WMF; + } else if (StrUtil.equalsAnyIgnoreCase(type, "pict")) { + return ExcelPicType.PICT; + } else if (StrUtil.equalsAnyIgnoreCase(type, "dib")) { + return ExcelPicType.DIB; + } + + // 默认格式 + return ExcelPicType.PNG; + } + + /** + * 写入图片到文件 + * + * @param pic 图片数据 + * @param file 文件 + */ + public static void writePicTo(final Picture pic, final File file) { + writePicTo(pic.getPictureData(), file); + } + + /** + * 写入图片到文件 + * + * @param pic 图片数据 + * @param file 文件 + */ + public static void writePicTo(final PictureData pic, final File file) { + FileUtil.writeBytes(pic.getData(), file); + } + + /** + * 获取所有图片列表 + * + * @param workbook 工作簿{@link Workbook} + * @return 图片列表 + */ + public static List getAllPictures(final Workbook workbook) { + return workbook.getAllPictures(); + } + + /** + * 获取工作簿指定sheet中绘制的图片列表 + * + * @param workbook 工作簿{@link Workbook} + * @param sheetIndex sheet的索引 + * @return 图片映射,键格式:行_列,值:{@link PictureData} + */ + public static List getShapePics(final Workbook workbook, int sheetIndex) { + Assert.notNull(workbook, "Workbook must be not null !"); + if (sheetIndex < 0) { + sheetIndex = 0; + } + + return getShapePics(workbook.getSheetAt(sheetIndex)); + } + + /** + * 获取工作簿指定sheet中绘制的图片列表
+ * 结果中{@link Picture#getClientAnchor()}标识位置信息,{@link Picture#getPictureData()}标识图片数据 + * + * @param sheet 工作表{@link Sheet} + * @return 图片列表 + */ + public static List getShapePics(final Sheet sheet) { + Assert.notNull(sheet, "Sheet must be not null !"); + + final Drawing drawing = sheet.getDrawingPatriarch(); + if (null == drawing) { + return ListUtil.empty(); + } + + return StreamUtil.of(drawing) + .filter(shape -> shape instanceof Picture) + .map(shape -> (Picture) shape) + .collect(Collectors.toList()); + } +} diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/shape/package-info.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/shape/package-info.java new file mode 100644 index 000000000..881546142 --- /dev/null +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/shape/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 Hutool Team and hutool.cn + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Excel中绘图相关操作 + */ +package org.dromara.hutool.poi.excel.shape; diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/writer/ExcelDrawingUtil.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/writer/ExcelDrawingUtil.java index 2d15cc471..450fe31f2 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/writer/ExcelDrawingUtil.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/writer/ExcelDrawingUtil.java @@ -23,7 +23,7 @@ import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xssf.usermodel.XSSFDrawing; import org.apache.poi.xssf.usermodel.XSSFSimpleShape; -import org.dromara.hutool.poi.excel.ExcelImgType; +import org.dromara.hutool.poi.excel.shape.ExcelPicType; import org.dromara.hutool.poi.excel.SimpleClientAnchor; import org.dromara.hutool.poi.excel.style.ShapeConfig; @@ -49,8 +49,8 @@ public class ExcelDrawingUtil { * @author vhukze * @since 6.0.0 */ - public static void drawingImg(final Sheet sheet, final byte[] pictureData, - final ExcelImgType imgType, final SimpleClientAnchor clientAnchor) { + public static void drawingPic(final Sheet sheet, final byte[] pictureData, + final ExcelPicType imgType, final SimpleClientAnchor clientAnchor) { final Drawing patriarch = sheet.createDrawingPatriarch(); final Workbook workbook = sheet.getWorkbook(); final ClientAnchor anchor = workbook.getCreationHelper().createClientAnchor(); diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/writer/ExcelWriter.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/writer/ExcelWriter.java index a5019877e..68e3ce095 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/writer/ExcelWriter.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/writer/ExcelWriter.java @@ -29,6 +29,8 @@ import org.dromara.hutool.poi.POIException; import org.dromara.hutool.poi.excel.*; import org.dromara.hutool.poi.excel.cell.CellRangeUtil; import org.dromara.hutool.poi.excel.cell.CellUtil; +import org.dromara.hutool.poi.excel.shape.ExcelPicType; +import org.dromara.hutool.poi.excel.shape.ExcelPicUtil; import org.dromara.hutool.poi.excel.style.*; import java.awt.Color; @@ -769,7 +771,7 @@ public class ExcelWriter extends ExcelBase { } // endregion - // region ----- writeImg + // region ----- writePic /** * 写出数据,本方法只是将数据写入Workbook中的Sheet,并不写出到文件
@@ -784,8 +786,8 @@ public class ExcelWriter extends ExcelBase { * @author vhukze * @since 5.7.18 */ - public ExcelWriter writeImg(final File imgFile, final int col1, final int row1, final int col2, final int row2) { - return writeImg(imgFile, new SimpleClientAnchor(col1, row1, col2, row2)); + public ExcelWriter writePic(final File imgFile, final int col1, final int row1, final int col2, final int row2) { + return writePic(imgFile, new SimpleClientAnchor(col1, row1, col2, row2)); } /** @@ -798,8 +800,8 @@ public class ExcelWriter extends ExcelBase { * @author vhukze * @since 6.0.0 */ - public ExcelWriter writeImg(final File imgFile, final SimpleClientAnchor clientAnchor) { - return writeImg(imgFile, ExcelImgUtil.getImgType(imgFile), clientAnchor); + public ExcelWriter writePic(final File imgFile, final SimpleClientAnchor clientAnchor) { + return writePic(imgFile, ExcelPicUtil.getPicType(imgFile), clientAnchor); } /** @@ -813,8 +815,8 @@ public class ExcelWriter extends ExcelBase { * @author vhukze * @since 6.0.0 */ - public ExcelWriter writeImg(final File imgFile, final ExcelImgType imgType, final SimpleClientAnchor clientAnchor) { - return writeImg(FileUtil.readBytes(imgFile), imgType, clientAnchor); + public ExcelWriter writePic(final File imgFile, final ExcelPicType imgType, final SimpleClientAnchor clientAnchor) { + return writePic(FileUtil.readBytes(imgFile), imgType, clientAnchor); } /** @@ -828,8 +830,8 @@ public class ExcelWriter extends ExcelBase { * @author vhukze * @since 6.0.0 */ - public ExcelWriter writeImg(final byte[] pictureData, final ExcelImgType imgType, final SimpleClientAnchor clientAnchor) { - ExcelDrawingUtil.drawingImg(this.sheet, pictureData, imgType, clientAnchor); + public ExcelWriter writePic(final byte[] pictureData, final ExcelPicType imgType, final SimpleClientAnchor clientAnchor) { + ExcelDrawingUtil.drawingPic(this.sheet, pictureData, imgType, clientAnchor); return this; } diff --git a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/shape/ExcelPicUtilTest.java b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/shape/ExcelPicUtilTest.java new file mode 100644 index 000000000..e95dfa5df --- /dev/null +++ b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/shape/ExcelPicUtilTest.java @@ -0,0 +1,29 @@ +package org.dromara.hutool.poi.excel.shape; + +import org.apache.poi.ss.usermodel.Picture; +import org.apache.poi.ss.usermodel.Workbook; +import org.dromara.hutool.core.io.IoUtil; +import org.dromara.hutool.core.lang.Console; +import org.dromara.hutool.poi.excel.WorkbookUtil; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +import java.util.List; + +public class ExcelPicUtilTest { + @Test + @Disabled + void readPicTest() { + final Workbook book = WorkbookUtil.createBook("d:/test/poi/a.xlsx"); + final List picMap = ExcelPicUtil.getShapePics( + WorkbookUtil.createBook("d:/test/poi/a.xlsx"), 0); + Console.log(picMap); + +// final List allPictures = book.getAllPictures(); +// for (PictureData shape : allPictures) { +// Console.log(shape); +// } + + IoUtil.closeQuietly(book); + } +} diff --git a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/ExcelWriteTest.java b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/ExcelWriteTest.java index c3286fbe8..31b978637 100644 --- a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/ExcelWriteTest.java +++ b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/ExcelWriteTest.java @@ -835,7 +835,7 @@ public class ExcelWriteTest { final File file = new File("C:\\Users\\zsz\\Desktop\\1.jpg"); - writer.writeImg(file, 0, 0, 5, 10); + writer.writePic(file, 0, 0, 5, 10); writer.flush(new File("C:\\Users\\zsz\\Desktop\\2.xlsx"), true); diff --git a/hutool-swing/src/test/java/org/dromara/hutool/swing/img/ImgTest.java b/hutool-swing/src/test/java/org/dromara/hutool/swing/img/ImgTest.java index 44856a4c1..e028890ed 100644 --- a/hutool-swing/src/test/java/org/dromara/hutool/swing/img/ImgTest.java +++ b/hutool-swing/src/test/java/org/dromara/hutool/swing/img/ImgTest.java @@ -33,7 +33,7 @@ public class ImgTest { @Test @Disabled public void cutTest1() { - Img.from(FileUtil.file("e:/pic/face.jpg")).cut(0, 0, 200).write(FileUtil.file("e:/pic/face_radis.png")); + Img.from(FileUtil.file("e:/shape/face.jpg")).cut(0, 0, 200).write(FileUtil.file("e:/shape/face_radis.png")); } @Test @@ -52,7 +52,7 @@ public class ImgTest { @Test @Disabled public void roundTest() { - Img.from(FileUtil.file("e:/pic/face.jpg")).round(0.5).write(FileUtil.file("e:/pic/face_round.png")); + Img.from(FileUtil.file("e:/shape/face.jpg")).round(0.5).write(FileUtil.file("e:/shape/face_round.png")); } @Test diff --git a/hutool-swing/src/test/java/org/dromara/hutool/swing/img/ImgUtilTest.java b/hutool-swing/src/test/java/org/dromara/hutool/swing/img/ImgUtilTest.java index 8f2581108..b0df8df89 100644 --- a/hutool-swing/src/test/java/org/dromara/hutool/swing/img/ImgUtilTest.java +++ b/hutool-swing/src/test/java/org/dromara/hutool/swing/img/ImgUtilTest.java @@ -37,7 +37,7 @@ public class ImgUtilTest { @Test @Disabled public void scaleTest() { - ImgUtil.scale(FileUtil.file("e:/pic/test.jpg"), FileUtil.file("e:/pic/test_result.jpg"), 0.8f); + ImgUtil.scale(FileUtil.file("e:/shape/test.jpg"), FileUtil.file("e:/shape/test_result.jpg"), 0.8f); } @Test @@ -71,8 +71,8 @@ public class ImgUtilTest { @Test @Disabled public void rotateTest() throws IOException { - final Image image = ImgUtil.rotate(ImageIO.read(FileUtil.file("e:/pic/366466.jpg")), 180); - ImgUtil.write(image, FileUtil.file("e:/pic/result.png")); + final Image image = ImgUtil.rotate(ImageIO.read(FileUtil.file("e:/shape/366466.jpg")), 180); + ImgUtil.write(image, FileUtil.file("e:/shape/result.png")); } @Test @@ -148,8 +148,8 @@ public class ImgUtilTest { @Test @Disabled public void copyTest() { - final BufferedImage image = ImgUtil.copyImage(ImgUtil.read("f:/pic/test.png"), BufferedImage.TYPE_INT_RGB); - ImgUtil.write(image, FileUtil.file("f:/pic/test_dest.jpg")); + final BufferedImage image = ImgUtil.copyImage(ImgUtil.read("f:/shape/test.png"), BufferedImage.TYPE_INT_RGB); + ImgUtil.write(image, FileUtil.file("f:/shape/test_dest.jpg")); } @Test