如何在java中高效地将JPEG读入BufferedImage
ImageIO.read
将文件读入 BufferedImage 的速度至少慢 200 倍,其他程序(如 feh
)可以读取该文件并将其呈现在屏幕上。您可以使用什么来代替 ImageIO 或者如何配置 ImageIO,以便它在合理的时间范围内读取数据?
编辑我如何加载/测量。
long start = System.nanoTime();
ImageIO.setUseCache(false);
try {
ImageIO.read(new URL("file:///tmp/reallyBig.jpg"));
} catch (IOException e) {
throw new RuntimeException(e);
}
end = System.nanoTime();
System.out.println("duration: "+ TimeUnit.NANOSECONDS.toMillis(end-start)));
是的,它不精确,因为我没有做热身等等。但这不会从 4000 毫秒到 200 毫秒,所以我可以忍受这种不精确。我不需要知道它在我们的精度中到底有多快,我可以看到它非常慢,不需要任何测量。我实际上不需要这样的情况,即在 10k 次迭代后它最终会快 5 毫秒,我真的需要根据正在运行的应用程序的用户请求查看正常的等待时间。同样,该图像可以在 200 毫秒内由 feh
渲染,而 ImageIO 在 4000 毫秒内无法将其加载到 BufferedImage 中。
将 ImageIO.read
替换为 new ImagePlus(...).getBufferedImage();
即可将速度提高 1.6 倍。需要实际阅读文档才能使用 ImageJ 获得更好的结果。
如果有人知道如何以不同的方式获得更好的结果,请分享。
编辑2:这张图片是什么(严重缩短):
identify -verbose fullImage.jpg
Image:
Format: JPEG (Joint Photographic Experts Group JFIF format)
Mime type: image/jpeg
Class: DirectClass
Geometry: 4032x3024+0+0
Resolution: 72x72
Print size: 56x42
Units: PixelsPerInch
Colorspace: sRGB
Type: TrueColor
Base type: Undefined
Endianness: Undefined
Depth: 8-bit
Interlace: JPEG
Intensity: Undefined
Compose: Over
Page geometry: 4032x3024+0+0
Dispose: Undefined
Iterations: 0
Compression: JPEG
Quality: 100
Orientation: TopLeft
Profiles:
Profile-exif: 12424 bytes
Profile-icc: 672 bytes
Profile-xmp: 3160 bytes
Filesize: 5.65549MiB
Number pixels: 12.1928M
ImageIO.read
reads the file into BufferedImage at least 200 times slower, that other programs like feh
can read the file and render it on screen. What can you use instead of ImageIO or how to configure ImageIO, so that it reads the data in reasonable timeframe?
EDIT how do I load/measure.
long start = System.nanoTime();
ImageIO.setUseCache(false);
try {
ImageIO.read(new URL("file:///tmp/reallyBig.jpg"));
} catch (IOException e) {
throw new RuntimeException(e);
}
end = System.nanoTime();
System.out.println("duration: "+ TimeUnit.NANOSECONDS.toMillis(end-start)));
yes, it's not precise, because I didn't do warmups etc. etc. But this won't do 200ms from 4000ms, so I can live with this imprecision. I need not to know exactly how fast it is in us precision, I can see it's really slow without any measuring needed. I actually don't need situation, that it eventually get 5ms faster after 10k iterations, I really need to see the normal waiting times upon user request from running application. And again, this image can be rendered by feh
in 200ms, while it's not loaded into BufferedImage by ImageIO in 4000ms.
Trivially replacing ImageIO.read
with new ImagePlus(...).getBufferedImage();
yields 1.6x speed up. Need to actually read documentation to get better results with ImageJ.
If someone know how to get better results in different way, please share.
EDIT 2: what is this image(severely shortened):
identify -verbose fullImage.jpg
Image:
Format: JPEG (Joint Photographic Experts Group JFIF format)
Mime type: image/jpeg
Class: DirectClass
Geometry: 4032x3024+0+0
Resolution: 72x72
Print size: 56x42
Units: PixelsPerInch
Colorspace: sRGB
Type: TrueColor
Base type: Undefined
Endianness: Undefined
Depth: 8-bit
Interlace: JPEG
Intensity: Undefined
Compose: Over
Page geometry: 4032x3024+0+0
Dispose: Undefined
Iterations: 0
Compression: JPEG
Quality: 100
Orientation: TopLeft
Profiles:
Profile-exif: 12424 bytes
Profile-icc: 672 bytes
Profile-xmp: 3160 bytes
Filesize: 5.65549MiB
Number pixels: 12.1928M
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ImageJ 是一个受 NIH 启发的公共领域 Java 图像处理程序,它是多线程的,并且比 Buffering 中的 ImageIo 实现快得多,因此可以并行执行诸如图像文件读取之类的耗时操作,更具体地说, ij.io 包包含用于读取/解码的类以及写入/编码图像,您可以研究将文件转换为 bufferImages 的实现。
ImageJ is a public domain Java image processing program inspired by NIH, It is multithreaded and works way faster than ImageIo implementation in Buffering, so time-consuming operations such as image file reading can be performed in parallel, more sepecificly the
ij.io
package contains classes for reading/decoding and writing/encoding images which you can investigate the implementation of converting files to bufferImages.