我正在尝试通过网络流式传输相机, 我遇到的主要问题是将BufferedImage编码为jpg的ImageIO延迟
这是我的代码:
long cameraTimer = 0, cameraTimerFPS = 0, cameraFPS = 0, cameraSize = 0;
while (!Thread.interrupted()) {
cameraTimer = System.currentTimeMillis();
BufferedImage imageBuffered = // Get the image
ImageWriter imageWriter = ImageIO.getImageWritersByFormatName("jpg").next();
ImageWriteParam imageWriterParameter = imageWriter.getDefaultWriteParam();
imageWriterParameter.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
imageWriterParameter.setCompressionQuality(cameraCompression / 100F);
ByteArrayOutputStream imageBuffer = new ByteArrayOutputStream();
imageWriter.setOutput(new MemoryCacheImageOutputStream(imageBuffer));
IIOImage outputImage = new IIOImage(imageBuffered, null, null);
imageWriter.write(null, outputImage, imageWriterParameter); // ~600ms latency
imageWriter.dispose();
// Send the image
cameraSize += imageBuffer.size();
cameraTimer = System.currentTimeMillis() - cameraTimer;
cameraTimer = (1000 / cameraFramerate) - cameraTimer;
if (cameraTimerFPS - System.currentTimeMillis() < -1000) {
System.out.println(String.format("Avg Image Size: %s", formatBytes(cameraSize / (cameraFPS + 1))));
System.out.println(String.format("Frame per second: %s", cameraFPS));
cameraTimerFPS = System.currentTimeMillis();
cameraSize = 0;
cameraFPS = 0;
}
cameraFPS++;
if (cameraTimer > 0) {
Thread.sleep(cameraTimer);
}
}
所以,我的问题是如何摆脱ImageIO api? 我在网上搜索,我没有找到任何可以取代ImageIO的图书馆。