I got some CPU problems when painting a VolatileImage (created from a BufferedImage) to a JPanel. In my application, I'm reading image data from a webcam. These images have a resolution of 1920*1080 pixels and the logic which is reading from the webcam allows about 30 FPS. This happens in an own thread. In the loop which is reading from the webcam, I get a BufferedImage and draw it into a VolatileImage. Then I call the repaint method of the JPanel which draws the VolatileImage. The webcam output is smooth and fast. The problem is the CPU. It's 95%-100% CPU-usage.
I already tried to NOT repaint every webcam-frame but only every 30 milliseconds. The CPU usage is the same. If I do not repaint the JPanel (so there is no image shown), but only read the frames from the camera and store them in the bufferedImage, the CPU-usage stays low. So the drawing part is the CPU-consuming part. Not the reading of the frames from the webcam.
This is the paintComponent method of my JPanel (nothing special...)
public void paintComponent(Graphics g) {
g.drawImage(cameraModel.volatileImage, 0, 0, null);
}
And this is the method which is requesting the frames from the webcam:
public void run() {
running = true;
while(running){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
dartCamPanel.repaint();
if (camera.read(frame)){
ByteBuffer bb = frame.createBuffer();
bb.get(byteBuffer, 0, bb.capacity());
volatileContext.drawImage(bufferedImage, 0, 0, null);
}
}
}
Am I doing something completely wrong?