getSnapshot() not working in VLCJ?

This forum is about all development around libVLC.
manojkumar
New Cone
New Cone
Posts: 3
Joined: 19 May 2014 11:17

getSnapshot() not working in VLCJ?

Postby manojkumar » 19 May 2014 11:43

I am trying to develop and application using VLCJ.

In my application, I want record a video from web cam and also get snapshot of current frame at certain intervals for processing at the time of recording. When i used :sout=#transcode{ } as mediaPlayer() option for recording video, getSanpshot() method dosen't work it gives error and dosen't get the current frame or image.

This is the code I am using:

Code: Select all

package vlctut; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.Timer; import uk.co.caprica.vlcj.player.MediaPlayerFactory; import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer; import uk.co.caprica.vlcj.player.embedded.videosurface.CanvasVideoSurface; public class TestLoad1 implements ActionListener { private final JFrame frame; private final JPanel contentPane; private final Canvas canvas; private final String [] Vlc_args={"--quiet","--video-filter=motion","--no-overlay"}; private MediaPlayerFactory factory; private EmbeddedMediaPlayer mediaPlayer,mPlayer1; private BufferedImage img1; private CanvasVideoSurface videoSurface; public static void main(String[] args) { Vlctut loadVlcJ = new Vlctut(); loadVlcJ.load_lib(); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new TestLoad1(); } }); } public TestLoad1() { canvas = new Canvas(); canvas.setBackground(Color.black); contentPane = new JPanel(); contentPane.setBackground(Color.black); contentPane.setLayout(new BorderLayout()); contentPane.add(canvas, BorderLayout.CENTER); frame = new JFrame("Capture"); frame.setContentPane(contentPane); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(50, 50); frame.setSize(800, 600); this.mediaPlayerPanel(); this.start("dshow://"); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent ev){ factory.release(); mediaPlayer.release(); } }); } private void mediaPlayerPanel() { factory = new MediaPlayerFactory(Vlc_args); mediaPlayer = factory.newEmbeddedMediaPlayer(); videoSurface = factory.newVideoSurface(canvas); mediaPlayer.setVideoSurface(videoSurface); } private void start(String mrl) { frame.setVisible(true); System.out.println(System.getProperty("user.dir")); File dir = new File(System.getProperty("user.dir"), "Videos"); dir.mkdirs(); DateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss"); String fileName = dir.getAbsolutePath() + "/Capture-" + df.format(new Date()) + ".mpg"; //streaming only video String[] options = {":dshow-vdev=USB Video Device","dshow-size=640x480", ":sout=#transcode{vcodec=mp2v, vb=4096,scale=1, acodec=mpga, ab=128, channels=2, samplerate=44100}" + ":duplicate{dst=file{dst="+ fileName+"}, dst=display}"}; mediaPlayer.playMedia(mrl,options); Timer time=new Timer(5000, this); time.start(); } @Override public void actionPerformed(ActionEvent ae) { img1=mPlayer1.getSnapshot(); File f1=new File("D://xyz.png"); try { ImageIO.write(img1, "png", f1); } catch (IOException ex) { Logger.getLogger(TestLoad1.class.getName()).log(Level.SEVERE, null, ex); } } }
It gives following Error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at vlctut.TestLoad1.actionPerformed(TestLoad1.java:119)
at javax.swing.Timer.fireActionPerformed(Timer.java:271)
at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

sherington
Cone that earned his stripes
Cone that earned his stripes
Posts: 491
Joined: 10 Sep 2008 11:57
VLC version: master
Operating System: Linux

Re: getSnapshot() not working in VLCJ?

Postby sherington » 19 May 2014 13:26

In fact getSnapshot() works perfectly well in vlcj, the problem is in your own code.

Did you actually try looking at the line number in the stacktrace to see where the null pointer might be? It will point directly to the cause of your problem.

I can only guess because there are no line numbers that I can see, but look here:

Code: Select all

img1=mPlayer1.getSnapshot();
You do not initialse "mPlayer1" anywhere in the code you posted, ergo you will get a NullPointerException.

manojkumar
New Cone
New Cone
Posts: 3
Joined: 19 May 2014 11:17

Re: getSnapshot() not working in VLCJ?

Postby manojkumar » 20 May 2014 13:15

Thanks for Quick Reply.

I have changed the code then also I am getting the same Error.

My Problem is very simple: I Want know while recording video from web camera can I get one or two snapshots of the current frame.

Edited Code:

Code: Select all

package vlctut; import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.Timer; import uk.co.caprica.vlcj.player.MediaPlayerFactory; import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer; import uk.co.caprica.vlcj.player.embedded.videosurface.CanvasVideoSurface; public class TestLoad1 implements ActionListener { private final JFrame frame; private final JPanel contentPane; private final Canvas canvas; private final String [] Vlc_args={"--quiet","--video-filter=motion","--no-overlay"}; private MediaPlayerFactory factory; private EmbeddedMediaPlayer mediaPlayer; private BufferedImage img1; private CanvasVideoSurface videoSurface; public static void main(String[] args) { Vlctut loadVlcJ = new Vlctut(); loadVlcJ.load_lib(); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new TestLoad1(); } }); } public TestLoad1() { canvas = new Canvas(); canvas.setBackground(Color.black); contentPane = new JPanel(); contentPane.setBackground(Color.black); contentPane.setLayout(new BorderLayout()); contentPane.add(canvas, BorderLayout.CENTER); frame = new JFrame("Capture"); frame.setContentPane(contentPane); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(50, 50); frame.setSize(800, 600); this.mediaPlayerPanel(); this.start("dshow://"); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent ev){ factory.release(); mediaPlayer.release(); } }); } private void mediaPlayerPanel() { factory = new MediaPlayerFactory(Vlc_args); mediaPlayer = factory.newEmbeddedMediaPlayer(); videoSurface = factory.newVideoSurface(canvas); mediaPlayer.setVideoSurface(videoSurface); } private void start(String mrl) { frame.setVisible(true); System.out.println(System.getProperty("user.dir")); File dir = new File(System.getProperty("user.dir"), "Videos"); dir.mkdirs(); DateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss"); String fileName = dir.getAbsolutePath() + "/Capture-" + df.format(new Date()) + ".mpg"; //streaming only video String[] options = {":dshow-vdev=USB Video Device","dshow-size=640x480", ":sout=#transcode{vcodec=mp2v, vb=4096,scale=1}" + ":duplicate{dst=file{dst="+ fileName+"}, dst=display}"}; mediaPlayer.playMedia(mrl,options); Timer time=new Timer(8000, this); time.start(); } @Override public void actionPerformed(ActionEvent ae) { img1=mediaPlayer.getSnapshot(); File f1=new File("D://xyz.png"); // if(img1!=null) try { ImageIO.write(img1, "png", f1);} catch (IOException ex) {System.out.println(ex);} // else //System.out.println("blank image"); } }
Error:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: im == null!
at javax.imageio.ImageIO.write(ImageIO.java:1457)
at javax.imageio.ImageIO.write(ImageIO.java:1521)
at vlctut.TestLoad1.actionPerformed(TestLoad1.java:124)
at javax.swing.Timer.fireActionPerformed(Timer.java:271)
at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

sherington
Cone that earned his stripes
Cone that earned his stripes
Posts: 491
Joined: 10 Sep 2008 11:57
VLC version: master
Operating System: Linux

Re: getSnapshot() not working in VLCJ?

Postby sherington » 20 May 2014 16:57

You are not getting the same error, it's a different error.

There was a bug in an earlier version of vlcj with that particular snapshot method (it should be fixed in the current release version).

This bug was because getting a snapshot is actually asynchronous - you request the snapshot, then you wait for the "snapshot taken" event which will happen some time later, then you get your snapshot image. The bug was that the method did not wait for the snapshot taken event.

So, check which version of vlcj you are using, and make sure you try with the latest version. You can also listen for the snapshot taken event in your own code if you want.

manojkumar
New Cone
New Cone
Posts: 3
Joined: 19 May 2014 11:17

Re: getSnapshot() not working in VLCJ?

Postby manojkumar » 21 May 2014 08:10

I am currently using Vlcj-3.0.1 Version.

Is their any alternative way to solve my problem.


Return to “Development around libVLC”

Who is online

Users browsing this forum: No registered users and 16 guests