1 2 3 Previous Next 36 Replies Latest reply: May 14, 2011 11:33 AM by 858788 RSS

    How can I run an applet for accessing the PC camera

    858788
      Hi,
      I'm working on my master thesis. I have as subject -face recognition. The subjects should be identified in a webcam/PC camera, locally installed. I mean an USB camera. The application that I developed so far is a web application and I have all the necessary code accessing the camera in Swing, in a JFrame.
      Even so I'm not able to port this code to a working applet inside my web app.
      I've dug into this for few weeks. The best result I had was a blank applet frame.
      The others reported different exceptions, either missing classes under JMF framework, or ( my best friend ) the applet class is not found.
      The applet code I have is

      import java.applet.Applet;
      import java.awt.BorderLayout;
      import java.awt.Dimension;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;

      import javax.media.Format;
      import javax.media.Manager;
      import javax.media.MediaLocator;
      import javax.media.Player;
      import javax.media.control.FormatControl;
      import javax.media.format.VideoFormat;
      import javax.media.protocol.CaptureDevice;
      import javax.media.protocol.DataSource;
      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JPanel;


      public class WebcamSimpleApplet extends Applet {
           /**
           *
           */
           private static final long serialVersionUID = 1L;
           int width, height;

           public void init()
           {
           super.init();
           width = getSize().width;
           height = getSize().height;
           JFrame f = new JFrame();
           setSize(400, 300);
           // f.setSize(500, 400);
           add( f.getContentPane().add(new WebcamPlayer()));
           }
      }

      class WebcamPlayer extends JPanel implements ActionListener {

           /**
           *
           */
           private static final long serialVersionUID = 1L;
           
           private static String PLAY_COMMAND = "play";
           private static String STOP_COMMAND = "stop";
           private static String EXIT_COMMAND = "exit";

           
           protected Dimension imageSize = null;
           protected Player player = null;
           DataSource ds = null;
           
           public WebcamPlayer( ){
           super(new BorderLayout());
           System.out.println("MyPlayer()");
           getCam();
           createGUI();
           }
           
           public void getCam() {
                try {

                     MediaLocator ml = new MediaLocator("vfw:Microsoft WDM Image Capture (Win32)://0");
                     ds = Manager.createDataSource(ml);
                     requestFormatResolution(ds);
                     player = Manager.createRealizedPlayer(ds);

                     
                } catch (Exception e) {
                     
                          ds.disconnect();
                          player.close();
                     player.deallocate();

                     e.printStackTrace();
                }finally {

                }
           } // getCam     
                
           private void createGUI ( ) {

                if (player.getVisualComponent() != null)
                     this.add(player.getVisualComponent(), BorderLayout.CENTER);
                
                //Buttons
                JButton play = new JButton(WebcamPlayer.PLAY_COMMAND);
                play.setBorderPainted(true);
                play.setBounds(1, 1, 20, 10);
                play.setActionCommand(PLAY_COMMAND);
                play.addActionListener(this);
                
                JButton stop = new JButton(WebcamPlayer.STOP_COMMAND);
                stop.setBorderPainted(true);
                stop.setBounds(2, 1, 20, 10);
                stop.setActionCommand(STOP_COMMAND);
                stop.addActionListener(this);
                
                JButton exit = new JButton(WebcamPlayer.EXIT_COMMAND);
                exit.setBorderPainted(true);
                exit.setBounds(3, 1, 20, 10);
                exit.setActionCommand(EXIT_COMMAND);
                exit.addActionListener(this);
                
                JPanel southPanel = new JPanel();
                southPanel.add(play);
                southPanel.add(stop);
                
                this.add(southPanel, BorderLayout.SOUTH);
                
                this.setVisible(true);
                this.setSize(320, 240);                    
           }

           private boolean requestFormatResolution(DataSource ds) {
                if (ds instanceof CaptureDevice) {
                     FormatControl[] fcs = ((CaptureDevice) ds).getFormatControls();
                     for (FormatControl fc : fcs) {
                          Format[] formats = ((FormatControl) fc).getSupportedFormats();
                          for (Format format : formats) {
                               if ((format instanceof VideoFormat)
                                         && (((VideoFormat) format).getSize().getHeight() <= 240)
                                         && (((VideoFormat) format).getSize().getWidth() <= 320)) {
                               if (format instanceof VideoFormat) {
                                    ((FormatControl) fc).setFormat(format);
                                    imageSize = ((VideoFormat) format).getSize();
                                    return true;
                               }
                          }
                     }
                }
                }
                
                return false;
           }
           
           public void actionPerformed(ActionEvent e) {
                
                if ( e.getActionCommand().equalsIgnoreCase(EXIT_COMMAND) ){
                     System.out.println("EXIT_COMMAND");
                     //deallocate resources
                     ds.disconnect();               
                     player.close();
                player.deallocate();          
                player = null;
                System.exit(0);          
                }
                
                if ( e.getActionCommand().equalsIgnoreCase(PLAY_COMMAND)){
                     System.out.println("PLAY_COMMAND");
                     player.start();          
                try {
                          Thread.currentThread();
                          // delay in initalization process
                          Thread.sleep(1000);
                          
                     } catch (InterruptedException e1) {
                          // TODO Auto-generated catch block
                          e1.printStackTrace();
                     }
                }
                
                if ( e.getActionCommand().equalsIgnoreCase(STOP_COMMAND)){
                     
                     System.out.println("STOP_COMMAND");
                     player.stop();
                }

           }
      }

      and works fine if I run it in Eclipse ->Run as Java applet.

      The question is:
      Could you point me to a good tutorial or show me something working?
      I'll really appreciate if you can give something for dummies or no-brain webcam Java applet programmer.

      Thank you
        • 1. Re: How can I run an applet for accessing the PC camera
          793415
          I'm working on my master thesis ..
          Uh-huh.
          ..no-brain webcam Java applet programmer.
          Don't waste time. Give up now.
          • 2. Re: How can I run an applet for accessing the PC camera
            858788
            Thank you for your suggestion.
            • 3. Re: How can I run an applet for accessing the PC camera
              858788
              i know i should package the applet into a jar file and sign it, but i haven't still had any luck. It reported the WebcamSimpleApplet.class is not found.
              @Andrew Thompson
              i've just asked for an advice, no matter the way I've used it.

              Edited by: user985724 on May 1, 2011 7:48 AM

              Edited by: user985724 on May 1, 2011 7:49 AM

              Edited by: user985724 on May 1, 2011 9:02 AM
              • 4. Re: How can I run an applet for accessing the PC camera
                baftos
                user985724 wrote:
                @Andrew Thompson
                I'm used with the superiority american attitude, because i'm working daily with american people, beside this attitude they have nothing.
                I was about to help, but forget it. Replace 'american' with whatever nation, race or religion you wish and this would be considered abuse. By the way, he's not american, nor am I.
                • 5. Re: How can I run an applet for accessing the PC camera
                  858788
                  ok,
                  To be honest I was disappointed by the Andrew's response and, sure, I'll not give up.
                  Everything has a start (even camera capturing applets).
                  • 6. Re: How can I run an applet for accessing the PC camera
                    DarrylBurke
                    Cross posted
                    http://www.coderanch.com/t/536311/Applets/java/run-applet-accessing-PC-camera
                    http://www.java-forums.org/java-applets/43195-how-can-i-run-webcam-applet.html

                    db
                    • 7. Re: How can I run an applet for accessing the PC camera
                      858788
                      Alright, I got it. But, I'll REALLY appreciate any advice.
                      Thank you.
                      • 8. Re: How can I run an applet for accessing the PC camera
                        sabre150
                        user985724 wrote:
                        Alright, I got it. But, I'll REALLY appreciate any advice.
                        You said earlier "i know i should package the applet into a jar file and sign it, but i haven't still had any luck." which implies you are trying to execute it without signing it (peculiar since you posted this in the 'Signed Applets' forum which implies that you know you needed to sign it) . You will have to sign the Applet if any Jo is going to be able to run it without making configuration changes that will adversely affect the general security of his computer and you can only sign jar files and not single class files. So my advice - spend some time learning about how to jar your Applet and how to sign the jar file.

                        P.S. For test purposes you can 'self sign' .
                        • 9. Re: How can I run an applet for accessing the PC camera
                          858788
                          @sabre150
                          First of all, thank you for your advice. I really appreciate it. The expression I used 'no-brain applet programmer' came after days of tries (whatever). Not making bad jokes is the solution.
                          After I tried today the following commands:
                          keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass pass -validity 360 -keysize 2048
                          jarsigner -keystore keystore.jks camera.jar selfsigned
                          jarsigner -verify camera.jar
                          keytool -export -keystore keystore.jks -alias selfsigned -file mycer.cer (i imported the certificate in IE)
                          i've returned to the same result: a blank applet frame.
                          The ugliest part is that nothing says nothing. Neither the Firefox Java Console, nor the Tomcat's console... nothing.
                          As a theory, I know what I should do, but something is missing me.
                          After that, I found this reference
                          http://www.roseindia.net/java/example/java/applet/WriteFile.shtml
                          which seems to be a good start, since the console should tell more if I'll be wrong.
                          Thank you again for your guideline.

                          Edited by: user985724 on May 2, 2011 11:53 AM

                          Edited by: user985724 on May 2, 2011 11:54 AM
                          • 10. Re: How can I run an applet for accessing the PC camera
                            sabre150
                            That RoseIndia site is giving bad advice since it will compromise the security of the client machine. In general don't trust anything you read on Rose India.

                            Instead of jumping in and trying to get your current Applet signed, create an "Hello World" Applet and jar then deploy that (without signing). Then Sign the jar and re-deploy. Then modify the Applet so that it reads and writes from and to the client hard disk. Only when all these stages work flawlessly is it worth considering working with your current Applet.

                            What I am hinting at is that start with something very very simple and then gradually expand it. Don't just use the big bang approach; it rarely works.
                            • 11. Re: How can I run an applet for accessing the PC camera
                              858788
                              @ sabre150
                              Thank you.
                              I followed your advice. I've finally built a signed applet which is able to write on local file system. I stepped forward and I tried a simple code for accessing the PC camera and not I'm receiving in Java Console
                              Caused by: java.lang.NoClassDefFoundError: javax/media/protocol/DataSource
                                   at java.lang.Class.getDeclaredMethods0(Native Method)
                                   at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
                                   at java.lang.Class.getDeclaredMethod(Unknown Source)
                                   at java.awt.Component.isCoalesceEventsOverriden(Unknown Source)
                                   at java.awt.Component.access$500(Unknown Source)
                                   at java.awt.Component$3.run(Unknown Source)
                                   at java.awt.Component$3.run(Unknown Source)
                                   at java.security.AccessController.doPrivileged(Native Method)
                                   at java.awt.Component.checkCoalescing(Unknown Source)
                                   at java.awt.Component.<init>(Unknown Source)
                                   at java.awt.Container.<init>(Unknown Source)
                                   at java.awt.Panel.<init>(Unknown Source)
                                   at java.awt.Panel.<init>(Unknown Source)
                                   at java.applet.Applet.<init>(Unknown Source)
                                   at javax.swing.JApplet.<init>(Unknown Source)
                                   at CameraApplet.<init>(CameraApplet.java:16)

                              even the content of META-INF/MANIFEST.MF file, inside the applet's jar file is :
                              Manifest-Version: 1.0
                              Class-Path: lib/customizer.jar lib/jmf.jar lib/mediaplayer.jar lib/multiplayer.jar lib/sound.jar
                              Created-By:

                              Name: CameraApplet.class
                              SHA1-Digest: 5qL9X/1Ytx+oE6vtpreLb8pATKw=

                              Name: lib/customizer.jar
                              SHA1-Digest: 7ZFgadNn+DLK+rdxy3KYUrG2eiU=

                              Name: lib/sound.jar
                              SHA1-Digest: tEQ0jTdnxykKj1C5wMTyA3oz1pM=

                              Name: lib/multiplayer.jar
                              SHA1-Digest: xGYPz9UURkpyfxvaHVfEFO89u3E=

                              Name: lib/jmf.jar
                              SHA1-Digest: DzvmhRMD8JojdcNflC80FmdEO2g=

                              Name: lib/mediaplayer.jar
                              SHA1-Digest: RsyWMAhw8d9jELM4XghTqPEsEaM=

                              Name: lib/jmf.properties
                              SHA1-Digest: bU5N1VReF1tXThUEkz+LHOtlxiQ=

                              and the lib directory is inside of jar file, because I created with
                              jar cfm signedApplet.jar Manifest.txt CameraApplet.class lib
                              where the content of Manifest.txt is
                              Manifest-Version: 1.0
                              Class-Path: lib/customizer.jar lib/jmf.jar lib/mediaplayer.jar lib/multiplayer.jar lib/sound.jar
                              Created-By:

                              But i guess this is out of this thread scope. I have to dig in how to package jar files with dependencies.
                              Thank you, man.
                              Your advice was really helpful.
                              • 12. Re: How can I run an applet for accessing the PC camera
                                sabre150
                                For an Applet the Class-Path entry in the manifest is irrelevant. It looks to me like you don't have the dependent jars available; they certainly should not be packaged inside the main jar. You need to look at the ARCHIVE tag - see http://download.oracle.com/javase/1.4.2/docs/guide/misc/applet.html .
                                • 13. Re: How can I run an applet for accessing the PC camera
                                  858788
                                  Thank you, man.
                                  Well... I've just all the needed jar files under JRE_HOME/lib/ext, the same which I set it for my Firefox, since the all story is happening on my laptop.
                                  Also, I have more that the JMF jars, i have dependencies for both face detection and face recognition. And, sure it worked.
                                  But, for my proposes. it's more than a dream. In the limit of my free time I'll explore more the way you suggested me.
                                  Because I don't have experience on this side, I've been stacked several weeks, obviously, working after job (few hours on day, not all days).
                                  I don't know how to thank you. Really, your advices helped me much.
                                  Thanks.
                                  • 14. Re: How can I run an applet for accessing the PC camera
                                    858788
                                    Hi,
                                    I made it work on the system I have at work - an XP professional, but I'm getting some issues on the laptop from home.
                                    In the Java Console I'm getting :
                                    network: Cache entry not found [url: http://localhost:8080/SimpleAppletWeb/javax/media/protocol/vfw/DataSource.class, version: null]
                                    network: Connecting http://localhost:8080/SimpleAppletWeb/javax/media/protocol/vfw/DataSource.class with proxy=DIRECT
                                    network: Connecting http://localhost:8080/SimpleAppletWeb/javax/media/protocol/vfw/DataSource.class with cookie "JSESSIONID=8A862A4060B5834F114A07F75C894D41"
                                    java.lang.RuntimeException: No permission to capture from applets
                                    network: Cache entry not found [url: http://localhost:8080/SimpleAppletWeb/media/protocol/vfw/DataSource.class, version: null]
                                    network: Connecting http://localhost:8080/SimpleAppletWeb/media/protocol/vfw/DataSource.class with proxy=DIRECT
                                    .....................
                                    then
                                    ......................
                                    network: Connecting http://localhost:8080/SimpleAppletWeb/javax/media/protocol/vfw/DataSource.class with cookie "JSESSIONID=8A862A4060B5834F114A07F75C894D41"
                                    java.lang.RuntimeException: No permission to capture from applets
                                    javax.media.NoPlayerException: Error instantiating class: com.sun.media.protocol.vfw.DataSource : java.lang.RuntimeException: No permission to capture from applets
                                         at javax.media.Manager.createPlayerForContent(Manager.java:1362)
                                         at javax.media.Manager.createPlayer(Manager.java:417)
                                         at CameraApplet$1.run(CameraApplet.java:48)

                                    I'm getting these even I followed the same steps as at work.
                                    The only difference could be the OS. On my personal system I have Windows 7.
                                    The jar is signed in the same way. Also, I added some permissions in java.policy file of the JRE which the FF is using.
                                    grant {
                                    permission java.io.FilePermission "<<ALL FILES>>","read,write";
                                    };
                                    // Standard extensions get all permissions by default
                                    grant codeBase "http://localhost:8080/*" {
                                    permission java.security.AllPermission;
                                    };

                                    grant principal javax.security.auth.x500.X500Principal "cn=name_got_from_java_control_panel_certificates" {
                                    permission java.security.AllPermission;
                                    };
                                    but still nothing.

                                    I have checked the Allow capture from applets in JMFRegistry.
                                    1 2 3 Previous Next