简体   繁体   English

字节数组到某种 Java/Scala 图像。 性能注意事项

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

I am writing an FTP server that receives images and then resizes and uploads them.我正在编写一个接收图像然后调整大小并上传它们的 FTP 服务器。

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.本质上,我使用ImageIO模块将Array[Byte]序列化为BufferedImage ,然后调整其大小。

I have done some profiling, and I've noticed that creating a BufferedImage using ImageIO is horribly slow.我做了一些分析,我注意到使用ImageIO创建BufferedImage非常慢。

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 .如果我只是上传Array[Byte] ,我可以获得大约 4 倍的吞吐量,而不是我实际尝试将其转换为BufferedImage The reason I can't just upload the Array[Byte] , is that I do need to resize the image.我不能只上传Array[Byte]的原因是我确实需要调整图像大小。 I am not tied to BufferedImage , it's just my first attempt.我与BufferedImage无关,这只是我的第一次尝试。

Does anyone know of some ideas I can use to speed this up?有谁知道我可以用来加快速度的一些想法? Is there a better format I should be using over BufferedImage ?我应该在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)编辑:我已经查看了这个问题,并且知道这一点: ImageIO.setUseCache(false)

I would suggest looking at more actively supported library (last release 4.0 at Feb 2020) like scrimage .我建议查看更积极支持的库(2020 年 2 月的最新版本 4.0),例如scrimage Under the hood it uses java.awt.* .在幕后,它使用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.至少在任何问题的情况下,您将能够解决它们并解决它们,而且使用更多“scalish”API。

Hope it helps.希望能帮助到你。

使用 ImageMagic 的外部程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM