2

I have to create an XSSF sheet with multiple copies of the same 2 images over 10k times. Currently I call the following block in a loop to set the image into its various places using ClientAnchor.

ClientAnchor anchor = helper.createClientAnchor(); //helper is instance of CreationHelper
anchor.setCol1(colNumber);
anchor.setRow1(rowNumber);
anchor.setAnchorType(ClientAnchor.MOVE_DONT_RESIZE);
Picture p = drawing.createPicture(anchor, imageIndex);
p.resize();

This is making the application very slow because of the internal calls to ImageUtil's readImage function to simply get the dimension it looks like. I already have the dimensions of the image and the image byte data before this loop even runs as I read that from a file directly. Is there another way to do this?

Reference #1:

Image retrieval code:

FileInputStream imageStream = new FileInputStream("image.png");
Byte[] image = IOUtils.toByteArray(imageStream);
imageStream.close();
imageIndex = output.addPicture(image, Workbook.PICTURE_TYPE_PNG);

Reference #2: Here is a snapshot from JVM Monitor showing the internal calls of resize() and the time taken for reference.

enter image description here

Any thing I'm missing out on? Is there a better way to achieve this?

amankapur91
  • 484
  • 4
  • 15
  • Jut looked at the code for `ImageUtils.getImageDimension()` and it seems to decode the entire image just to get the width/height..! This is something that is usually encoded in the header of the file format, and can be extracted *much* faster, by using (`ImageReader r`),`r.getHeight()/r.getWidth()`. Consider filing an issue. – Harald K Dec 09 '15 at 12:55
  • PS: You can mitigate the issue somewhat by using `PICTURE_TYPE_DIB` and using BMP format (it's faster to read/write than PNG) + using `ImageIO.setUseCache(false)` to disable disk caching in `ImageIO`. However, if you already know the image dimensions, having a `Picture.setSize(...)` method would be much faster. You would have to take the DPI setting into account, but still... Another possible enhancement request. :-) – Harald K Dec 09 '15 at 12:59
  • @haraldK thanks for the help. your changes alone with BMP have given me a 2x increase. – amankapur91 Dec 10 '15 at 11:05

0 Answers0