5

I am writing an FTP server that receives images and then resizes and uploads them.

My current process (pseudocode) is as follows:

val imagesAsBytes: Array[Byte] = ...
val bufferedImage: BufferedImage = ImageIO.read(new ByteArrayInputStream(bytes))
uploadImage(bufferedImage)

That's the gist of it. I've left out the resizing because it's not important. Essentially, I serialise the Array[Byte] into a BufferedImage using the ImageIO module, and then resize it.

I have done some profiling, and I've noticed that creating a BufferedImage using ImageIO is horribly slow.

If I just upload the Array[Byte], I can achieve about 4x the throughput, than if I actually try and convert it to a BufferedImage. The reason I can't just upload the Array[Byte], is that I do need to resize the image. I am not tied to BufferedImage, it's just my first attempt.

Does anyone know of some ideas I can use to speed this up? Is there a better format I should be using over BufferedImage?

I've already considered pushing resizing out to a separate microservice and perform it asynchronously, but it's not an option for the first release.

Edit: I have reviewed this question, and am aware of this: ImageIO.setUseCache(false)

Community
  • 1
  • 1
Dominic Bou-Samra
  • 14,799
  • 26
  • 100
  • 156
  • Does this help? http://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html – Allison Jul 23 '15 at 01:40
  • I don't think that decoding an image, resampling (resizing) it, and finally encoding again can be considered slow (4x) according to your numbers. Actually, it's surprisingly fast, compared to just copy the bytes... Or was that compared to decoding only? Anyway, what file format are you using? Some formats are heavier to en-/decode than others. – Harald K Jul 25 '15 at 13:19

2 Answers2

1

I would suggest looking at more actively supported library (last release 4.0 at Feb 2020) like scrimage. Under the hood it uses java.awt.*. At least in case of any issues you will be able to address them and get them resolved, moreover using more "scalish" API.

Hope it helps.

sksamuel
  • 16,154
  • 8
  • 60
  • 108
tkachuko
  • 1,956
  • 1
  • 13
  • 20
0

Use external program from ImageMagic.

vbezhenar
  • 11,148
  • 9
  • 49
  • 63