Java 在.word文件中插入带有apache poi的图像,增加图像大小

Java 在.word文件中插入带有apache poi的图像,增加图像大小,java,apache-poi,Java,Apache Poi,我是Apache新手,我正在检查与图片一起插入的图像是否已在word文档中调整大小。我正在使用Apache文档中刚刚修改的示例。图像比原始尺寸放大了很多,当创建的word文档打开时,图片显示在文档上的大小被调整了,当我强制调整图片的大小时,我找不到任何解释 以下是使用的代码: public class SimpleImages { public static void main(String\[\] args) throws IOException, InvalidFormatExce

我是Apache新手,我正在检查与图片一起插入的图像是否已在word文档中调整大小。我正在使用Apache文档中刚刚修改的示例。图像比原始尺寸放大了很多,当创建的word文档打开时,图片显示在文档上的大小被调整了,当我强制调整图片的大小时,我找不到任何解释

以下是使用的代码:

public class SimpleImages {

    public static void main(String\[\] args) throws IOException, InvalidFormatException {
        try (XWPFDocument doc = new XWPFDocument()) {
            XWPFParagraph p = doc.createParagraph();

            XWPFRun r = p.createRun();

            for (String imgFile : args) {
                int format;

                if (imgFile.endsWith(".emf")) {
                    format = XWPFDocument.PICTURE_TYPE_EMF;
                } else if (imgFile.endsWith(".wmf")) {
                    format = XWPFDocument.PICTURE_TYPE_WMF;
                } else if (imgFile.endsWith(".pict")) {
                    format = XWPFDocument.PICTURE_TYPE_PICT;
                } else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg")) {
                    format = XWPFDocument.PICTURE_TYPE_JPEG;
                } else if (imgFile.endsWith(".png")) {
                    format = XWPFDocument.PICTURE_TYPE_PNG;
                } else if (imgFile.endsWith(".dib")) {
                    format = XWPFDocument.PICTURE_TYPE_DIB;
                } else if (imgFile.endsWith(".gif")) {
                    format = XWPFDocument.PICTURE_TYPE_GIF;
                } else if (imgFile.endsWith(".tiff")) {
                    format = XWPFDocument.PICTURE_TYPE_TIFF;
                } else if (imgFile.endsWith(".eps")) {
                    format = XWPFDocument.PICTURE_TYPE_EPS;
                } else if (imgFile.endsWith(".bmp")) {
                    format = XWPFDocument.PICTURE_TYPE_BMP;
                } else if (imgFile.endsWith(".wpg")) {
                    format = XWPFDocument.PICTURE_TYPE_WPG;
                } else {
                    System.err.println("Unsupported picture: " + imgFile +
                            ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
                    continue;
                }

                r.setText(imgFile);
                r.addBreak();
                try (FileInputStream is = new FileInputStream(imgFile)) {

                    BufferedImage bimg = ImageIO.read(new File(imgFile));
                    int anchoImagen          = bimg.getWidth();
                    int altoImagen         = bimg.getHeight();
                    System.out.println("anchoImagen: " + anchoImagen);
                    System.out.println("altoImagen: " + anchoImagen);

                    r.addPicture(is, format, imgFile, Units.toEMU(anchoImagen), Units.toEMU(altoImagen)); 
                }
                r.addBreak(BreakType.PAGE);
            }

            try (FileOutputStream out = new FileOutputStream("C:\\W_Ejm_Jasper\\example-poi-img\\src\\main\\java\\es\\eve\\example_poi_img\\images.docx")) {
                doc.write(out);
                System.out.println(" FIN " );
            }
        }
    }


}
单词内部的图像

原始图像为(131*216像素):

图像在word中缩放


测量单位是点,而不是像素。尝试
Units.toEMU(Units.pixelToPoints(anchoImagen))
Units.toEMU(Units.pixelToPoints(altoImagen))
。或者
Units.pixelToEMU(anchoImagen)
Units.pixelToEMU(altoImagen)
。答案很好,两种解决方案都有效。非常感谢你