如何在poi词run.addPicture()中动态设置图像大小? [英] How to set image size is dynamically in poi word run.addPicture()?

查看:446
本文介绍了如何在poi词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. Thanks.

推荐答案

因此,我们需要从文件中确定图像的首选尺寸.关于如何做到这一点已经有很多讨论.搜索关键字:java get dimension from image file.最简单的可能性是使用ImageIO从文件中读取BufferedImage,然后该文件具有widthheight属性.

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 widthand 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();

  }

  doc.write(new FileOutputStream("CreateWordImagesPreferredSize.docx"));
  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();

  }

  doc.write(new FileOutputStream("CreateWordImagesPreferredSize.docx"));
  doc.close();

 }
}

这篇关于如何在poi词run.addPicture()中动态设置图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆