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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With