我正在编写一个连接和流式传输网络摄像头视频的应用程序。为此,我使用Sarxos网络摄像头库(链接here)获取默认网络摄像头,然后使用WebcamPanel绘制图像。当我将应用程序交付给我的客户时,问题就出现了,他们在旧机器上测试了它,并抱怨应用程序占用了太多的CPU。
我以前从未注意到这一点,当我再次测试它时,令我惊讶的是,该应用程序占用了大约33%的CPU,这对于只连接网络摄像头并使用30 FPS绘制图像的简单应用来说太多了。这是我的编程环境:Windows 7 64位,CoreI5-4460 CPU(3.2-3.4Ghz),Zotac Geforce GTX 650 Ti,Java 7u45。
我已经测试过指出哪个部分占用了最多的CPU,而且它是渲染。如果我只获取网络摄像头图像但不绘制它们,CPU占用6-7%,但是当我渲染它们时,CPU会跳到30-33%。我看了WebcamPanel课,看看他们可能有些不对劲,但到目前为止我什么都没发现。绘制方法如下:
@Override
public void paintImage(WebcamPanel owner, BufferedImage image, Graphics2D g2) {
assert owner != null;
assert image != null;
assert g2 != null;
int pw = getWidth();
int ph = getHeight();
int iw = image.getWidth();
int ih = image.getHeight();
Object antialiasing = g2.getRenderingHint(KEY_ANTIALIASING);
Object rendering = g2.getRenderingHint(KEY_RENDERING);
g2.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_OFF);
g2.setRenderingHint(KEY_RENDERING, VALUE_RENDER_SPEED);
g2.setBackground(Color.BLACK);
g2.setColor(Color.BLACK);
g2.fillRect(0, 0, pw, ph);
// resized image position and size
int x = 0;
int y = 0;
int w = 0;
int h = 0;
switch (drawMode) {
case NONE:
w = image.getWidth();
h = image.getHeight();
break;
case FILL:
w = pw;
h = ph;
break;
case FIT:
double s = Math.max((double) iw / pw, (double) ih / ph);
double niw = iw / s;
double nih = ih / s;
double dx = (pw - niw) / 2;
double dy = (ph - nih) / 2;
w = (int) niw;
h = (int) nih;
x = (int) dx;
y = (int) dy;
break;
}
if (resizedImage != null) {
resizedImage.flush();
}
if (w == image.getWidth() && h == image.getHeight() && !mirrored) {
resizedImage = image;
} else {
GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = genv.getDefaultScreenDevice().getDefaultConfiguration();
Graphics2D gr = null;
try {
resizedImage = gc.createCompatibleImage(pw, ph);
gr = resizedImage.createGraphics();
gr.setComposite(AlphaComposite.Src);
for (Map.Entry<RenderingHints.Key, Object> hint : imageRenderingHints.entrySet()) {
gr.setRenderingHint(hint.getKey(), hint.getValue());
}
gr.setBackground(Color.BLACK);
gr.setColor(Color.BLACK);
gr.fillRect(0, 0, pw, ph);
int sx1, sx2, sy1, sy2; // source rectangle coordinates
int dx1, dx2, dy1, dy2; // destination rectangle coordinates
dx1 = x;
dy1 = y;
dx2 = x + w;
dy2 = y + h;
if (mirrored) {
sx1 = iw;
sy1 = 0;
sx2 = 0;
sy2 = ih;
} else {
sx1 = 0;
sy1 = 0;
sx2 = iw;
sy2 = ih;
}
gr.drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
} finally {
if (gr != null) {
gr.dispose();
}
}
}
g2.drawImage(resizedImage, 0, 0, null);
if (isFPSDisplayed()) {
String str = String.format("FPS: %.1f", webcam.getFPS());
int sx = 5;
int sy = ph - 5;
g2.setFont(getFont());
g2.setColor(Color.BLACK);
g2.drawString(str, sx + 1, sy + 1);
g2.setColor(Color.WHITE);
g2.drawString(str, sx, sy);
}
if (isImageSizeDisplayed()) {
String res = String.format("%d\u2A2F%d px", iw, ih);
FontMetrics metrics = g2.getFontMetrics(getFont());
int sw = metrics.stringWidth(res);
int sx = pw - sw - 5;
int sy = ph - 5;
g2.setFont(getFont());
g2.setColor(Color.BLACK);
g2.drawString(res, sx + 1, sy + 1);
g2.setColor(Color.WHITE);
g2.drawString(res, sx, sy);
}
if (isDisplayDebugInfo()) {
if (lastRepaintTime < 0) {
lastRepaintTime = System.currentTimeMillis();
} else {
long now = System.currentTimeMillis();
String res = String.format("DEBUG: repaints per second: %.1f", (double) 1000 / (now - lastRepaintTime));
lastRepaintTime = now;
g2.setFont(getFont());
g2.setColor(Color.BLACK);
g2.drawString(res, 6, 16);
g2.setColor(Color.WHITE);
g2.drawString(res, 5, 15);
}
}
g2.setRenderingHint(KEY_ANTIALIASING, antialiasing);
g2.setRenderingHint(KEY_RENDERING, rendering);
}
我已经尝试了很多东西来优化我的渲染,但没有什么工作。我尝试过的事情:
我考虑更改为OpenGL库来渲染图像,但这将是最后一个选项,因为我对OpenGL一无所知,而且我认为Java2D对于我的应用来说已经足够了。任何人都可以帮我解决这个问题吗?
答案 0 :(得分:1)
经过各种测试,包括使用OpenGL渲染网络摄像头的图像(因为我必须从头开始学习OpenGL,花了太多时间),我找到了解决方案。这些是我做的事情:
之后,我的应用程序CPU百分比从33%下降到11%。希望这会有人和我一起遇到同样的问题。