Search code examples
javawebcamvlclibvlcvlcj

Using Webcam with VLCJ not working correctly while using the same options in VLC do work:


I am somewhat lost here:

I try to integrate my Webcam in a Java App using VLCJ. When i open VLC and use the settings shown in the screenshot below everything works well.

enter image description here

But when I then try to use the exact same settings from within my VLCJ App i get the wrong image like VLCJ is using the wrong Webcam device (System default which is a virtual cam and thus not the correct one!).

This is my testcode:

public class Camera_Demo {

      private final EmbeddedMediaPlayerComponent mediaPlayerComponent;

      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {      
          public void run() {
            new Camera_Demo();
          }
        });
       }

      private Camera_Demo() {

         NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:/Program Files (x86)/VideoLAN/VLC");
         Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);       

         JFrame frame = new JFrame("vlcj Tutorial");
         mediaPlayerComponent = new EmbeddedMediaPlayerComponent();    
         frame.setContentPane(mediaPlayerComponent);    
         frame.setLocation(100, 100);       
         frame.setSize(1280, 720);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);

         String [] options =  {" :dshow-vdev=HP HD Webcam [Fixed]"
                             + " :dshow-adev=none"
                             + " :dshow-size=1280x720"
                             + " :dshow-aspect-ratio=16\\:9"
                             + " :live-caching=200"};
         mediaPlayerComponent.getMediaPlayer().startMedia("dshow://", options);
      }
    }

Using the quoted code i get a JFrame showing a blue Cyberlink "please start your webcam" logo. This happens in VLC, too, when i use the Virtual Webcam device ("CyberLink Webcam Sharing Manager"). That is why I use "dshow-vdev=HP HD Webcam [Fixed]". That is the excat name of the physical device (see screenshot above). So i have NO idea at all why this is working in VLC but not in VLCJ....

Any ideas?


Solution

  • The options need to be split, not concatenated.

    You need to convert this:

    String [] options =  {" :dshow-vdev=HP HD Webcam [Fixed]"
                             + " :dshow-adev=none"
                             + " :dshow-size=1280x720"
                             + " :dshow-aspect-ratio=16\\:9"
                             + " :live-caching=200"};
    

    Into an actual array of strings like this:

    String[] options =  {":dshow-vdev=HP HD Webcam [Fixed]",
                         ":dshow-adev=none",
                         ":dshow-size=1280x720",
                         ":dshow-aspect-ratio=16\\:9",
                         ":live-caching=200"};
    

    The examples in the vlcj test sources do similar things.