Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Byte array to some sort of Java/Scala image. Performance considerations

Tags:

java

image

scala

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)

like image 805
Dominic Bou-Samra Avatar asked Nov 10 '22 08:11

Dominic Bou-Samra


1 Answers

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.

like image 91
tkachuko Avatar answered Nov 14 '22 21:11

tkachuko