屏幕截图正在创建,word doc也有一些大小但是当我打开它时它是空的...任何人都可以建议,附加代码
我需要将所有截图放在word文档中。
在打开word文档时出现了一些“启用内容”错误,我已经信任发布者...现在错误(不是错误)没有出现,但是WORD doc是空的,大小合理。< / p>
import java.awt.AWTException;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.apache.commons.io.IOUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class TakeScreenshots {
public static void main(String[] args) {
try {
// for (int counter = 0; counter <= 5; counter++) {
captureScreenShot();
// captureScreenShot(out, docx);
// }
} catch (Exception e) {
e.printStackTrace();
}
}
public static void captureScreenShot() throws Exception {
XWPFDocument docx = new XWPFDocument();
String screenshot_name = System.currentTimeMillis() + ".png";
BufferedImage image = new Robot()
.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "png", new File("d:/xyz/" + screenshot_name));
InputStream pic = new FileInputStream("d:/xyz/" + screenshot_name);
docx.addPictureData(pic, Document.PICTURE_TYPE_PNG);
FileOutputStream out = new FileOutputStream("d:/xyz/doc1.docx");
docx.write(out);
pic.close();
out.flush();
out.close();
TimeUnit.SECONDS.sleep(1);
}
}
答案 0 :(得分:0)
修改你的captureScreenShot方法,如下所示,它将起作用。
public static void captureScreenShot() throws Exception {
XWPFDocument docx = new XWPFDocument();
XWPFParagraph paragraph = docx.createParagraph();
XWPFRun run = paragraph.createRun();
String screenshot_name = System.currentTimeMillis() + ".png";
BufferedImage image = new Robot()
.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "png", new File("d:/xyz/" + screenshot_name));
InputStream pic = new FileInputStream("d:/xyz/" + screenshot_name);
//docx.addPictureData(pic, Document.PICTURE_TYPE_PNG);
run.addBreak();
run.addPicture(pic, XWPFDocument.PICTURE_TYPE_JPEG, screenshot_name, Units.toEMU(350), Units.toEMU(350));
pic.close();
FileOutputStream out = new FileOutputStream("d:/xyz/doc1.docx");
docx.write(out);
out.flush();
out.close();
TimeUnit.SECONDS.sleep(1);
}