I have a scenario where i need to capture image and write them to a word file one after the other. I have written the below code but doesn't seem to be working. Please help
Robot robot;
try {
robot = new Robot();
BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(screenShot, "JPG", new File("C:\\xxx\\Gaurav\\NEW1.JPG"));
InputStream is = new ByteArrayInputStream(baos.toByteArray());
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText(resulttext);
run.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, "new", 300, 300);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Blank Document
}
Using
ImageIO.write(screenShot, "JPG", baos);
the image is there but it is a little bit small because the measurement unit is not pixel but EMU (English Metric Unit). There is aorg.apache.poi.util.Units
which can convert pixels to EMUs.The following does work for me: