如何在 poi word run.addPicture() 中动态设置图像大小? [英] How to set image size is dynamically in poi word run.addPicture()?
问题描述
XWPFDocument doc= new XWPFDocument();
InputStream a = someMethod(underConditions(inputimage));
paragraph.createRun().addPicture(a, Document.PICTURE_TYPE_PNG, "", Units.toEMU(20), Units.toEMU(20));
a.close();
doc.write(new FileOutputStream("CreateWordHeaderFooter.docx"));
doc.close();
这是在poi word中添加图片的简单代码.但是 InputStream a 在不同的条件下是不同的.如果inputimage
小Units.toEMU(20),Units.toEMU(20)
就大,如果inputimage
大Units.toEMU(20), Units.toEMU(20)
太小了.在这种情况下,图像会失真.
This is simple code for add a image in poi word. But InputStream a is different under different conditions. If inputimage
is small Units.toEMU(20), Units.toEMU(20)
is to big, and if inputimage
is big Units.toEMU(20), Units.toEMU(20)
is too small. In this conditions images are distorted.
所以我的问题是:如何根据不同的图像设置动态图像大小?
So my question is: how to set dynamic image size based on different image?
推荐答案
所以我们需要从文件中确定图像的首选尺寸.已经有很多关于如何做到这一点的讨论.搜索关键词:java 从图片文件中获取维度
.最简单的可能性是使用 ImageIO
从文件中读取 BufferedImage
然后具有 width
和 height
属性.
So we need determining the preferred dimensions of the images from their files. There are already much discussions on how to do that. Search keywords: java get dimension from image file
. Simplest possibility is using ImageIO
to read BufferedImage
from the file which then has width
and height
properties.
示例:
import java.io.*;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.util.Units;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.Dimension;
public class CreateWordImagesPreferredSize {
static Dimension getImageDimension(File imgFile) throws IOException {
BufferedImage img = ImageIO.read(imgFile);
return new Dimension(img.getWidth(), img.getHeight());
}
public static void main(String[] args) throws Exception {
XWPFDocument doc= new XWPFDocument();
XWPFParagraph paragraph = doc.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("The Images:");
String[] images = new String[]{"Koala.png", "Scannen.jpg", "Winter.bmp"};
for (int i = 0 ; i < images.length; i++) {
paragraph = doc.createParagraph();
run = paragraph.createRun();
File imgFile = new File(images[i]);
Dimension dim = getImageDimension(imgFile);
System.out.println(dim);
double width = dim.getWidth();
double height = dim.getHeight();
double scaling = 1.0;
if (width > 72*6) scaling = (72*6)/width; //scale width not to be greater than 6 inches
InputStream in = new FileInputStream(imgFile);
paragraph.createRun().addPicture(in, Document.PICTURE_TYPE_PNG, images[i],
Units.toEMU(width*scaling), Units.toEMU(height*scaling));
in.close();
}
FileOutputStream out = new FileOutputStream("CreateWordImagesPreferredSize.docx");
doc.write(out);
out.close();
doc.close();
}
}
注意:如果原始尺寸更大,我会将图像缩放到不大于 6 英寸宽.所以它将适合一页宽度.不会缩放较小的图像.
Note: I do scaling the image to be not greater than 6 inches width if its original size is greater. So it will fit into one page width. Smaller images will not be scaled.
根据评论,如果你不能使用File
而必须使用InputStream
,那么你需要知道,BufferedImage img = ImageIO.read(imgStream);
当然是从流中读取.因此,流将在此之后结束.所以 .addPicture(imgStream,...)
然后需要一个新打开的流.
As from the comments, if you cannot using File
but must using InputStream
, then you need to know, that BufferedImage img = ImageIO.read(imgStream);
of course reads from the stream. So the stream will be at its end after that. So .addPicture(imgStream,...)
will need a newly opened stream then.
示例:
import java.io.*;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.util.Units;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.Dimension;
public class CreateWordImagesPreferredSizeStream {
static InputStream getInputStream(String filename) throws Exception {
return new FileInputStream(filename);
}
public static void main(String[] args) throws Exception {
XWPFDocument doc= new XWPFDocument();
XWPFParagraph paragraph = doc.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("The Images:");
String[] images = new String[]{"Koala.png", "Scannen.jpg", "Winter.bmp"};
for (int i = 0 ; i < images.length; i++) {
paragraph = doc.createParagraph();
run = paragraph.createRun();
InputStream imgStream = getInputStream(images[i]);
BufferedImage img = ImageIO.read(imgStream);
double w = img.getWidth();
double h = img.getHeight();
imgStream.close();
System.out.println(w);
System.out.println(h);
double scaling = 1.0;
if (w > 72*6) scaling = (72*6)/w; //scale width not to be greater than 6 inches
imgStream = getInputStream(images[i]);
paragraph.createRun().addPicture(imgStream, Document.PICTURE_TYPE_PNG, images[i],
Units.toEMU(w*scaling), Units.toEMU(h*scaling));
imgStream.close();
}
FileOutputStream out = new FileOutputStream("CreateWordImagesPreferredSize.docx");
doc.write(out);
out.close();
doc.close();
}
}
这篇关于如何在 poi word run.addPicture() 中动态设置图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!