I am following a tutorial on the v4lvj library that allows access to webcams on a linux. link to source I wrote the example program that basically displays the video feed of your webcamera. Everything works just as the program described. The problem comes in when I try to alter the program by adding a green line ontop of the video feed. I simply grab the graphics the same way, set the line color and then draw the line.
@Override
public void nextFrame(VideoFrame frame) {
// This method is called when a new frame is ready.
// Don't forget to recycle it when done dealing with the frame.
// draw the new frame onto the JLabel
label.getGraphics().drawImage(frame.getBufferedImage(), 0, 0, width, height, null);
label.getGraphics().setColor(Color.GREEN); //this line causes exceptions
label.getGraphics().drawLine(0, 0, 640, 480); //this line causes exceptions
// recycle the frame
frame.recycle();
}
When I do that though, I get some exceptions preceded by "It seems your JVM is unable to decode this image. Supported image types: BMP bmp jpg JPG..." and then
javax.imageio.IIOException: Unsupported marker type 0x5a
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImage(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1176)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:984)
at javax.imageio.ImageIO.read(ImageIO.java:1438)
at javax.imageio.ImageIO.read(ImageIO.java:1342)
at au.edu.jcu.v4l4j.JPEGVideoFrame.refreshBufferedImage(Unknown Source)
at au.edu.jcu.v4l4j.BaseVideoFrame.getBufferedImage(Unknown Source)
at SimpleViewer.nextFrame(SimpleViewer.java:143)
at au.edu.jcu.v4l4j.PushSource.run(Unknown Source)
at java.lang.Thread.run(Thread.java:679)
au.edu.jcu.v4l4j.exceptions.V4L4JException: Exception received while grabbing next frame
at au.edu.jcu.v4l4j.PushSource.run(Unknown Source)
at java.lang.Thread.run(Thread.java:679)
Caused by: au.edu.jcu.v4l4j.exceptions.UnsupportedMethod: Unable to decode the image
at au.edu.jcu.v4l4j.JPEGVideoFrame.refreshBufferedImage(Unknown Source)
at au.edu.jcu.v4l4j.BaseVideoFrame.getBufferedImage(Unknown Source)
at SimpleViewer.nextFrame(SimpleViewer.java:143)
... 2 more
Caused by: javax.imageio.IIOException: Unsupported marker type 0x5a
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImage(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1176)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:984)
at javax.imageio.ImageIO.read(ImageIO.java:1438)
at javax.imageio.ImageIO.read(ImageIO.java:1342)
... 5 more
Could anyone tell me why and help me draw a line ontop of a video feed. Thank you.
You should do your painting by overriding
paint(Graphics g)
method ofJLabel
, then do a castGraphics2D g2d = (Graphics2D) g;
and useGraphics2D
for painting.It is incorrect way you use JLabel in your code.
You could use also
BufferedImage
you received from frame, get theirGraphics
and draw on it