Java media framework - 64 bits

6 respostas
V

Olá pessoal,

Estou com o seguinte problema. Tenho um applet que roda no windows XP e quando trocaram o PC aqui do trabalho para o Windows 7 64 bits a mesma parou de funcionar. A imagem da webcam não carrega.

Ocorre o seguinte erro:
Exception on commit = java.lang.SecurityException: commit: Permission denied
java.lang.RuntimeException: No permission to capture from applets
java.lang.RuntimeException: No permission to capture from applets

Estou com o Java Media Framework 2.1.1e e jdk-6u22 instalados.

Obrigado.

6 Respostas

T

Na verdade o que parece é que o applet não está assinado. Sem assinar o applet você não consegue acessar o hardware da máquina.

E

Duvido também que esse applet funcione em um Windows 7 de 32 bits, a menos que você inicie o Internet Explorer como “Administrador”.

Provavelmente esse applet terá de ser revisado para rodar em Windows 7.

V

Será que tenho que fazer alguma alteração na classe para que funcione no Windows?

package cam;

import javax.swing.*;

public class JWebCam extends JApplet

{

protected final static int MIN_WIDTH  = 320;

protected final static int MIN_HEIGHT = 240;

protected static int shotCounter = 1;
protected JLabel statusBar = null;
protected JPanel visualContainer = null;
public Component visualComponent = null;
protected JToolBar toolbar = null;
protected MyToolBarAction formatButton    = null;
protected MyToolBarAction captureButton   = null;

public Player player = null;
protected CaptureDeviceInfo webCamDeviceInfo = null;
protected MediaLocator ml = null;
protected Dimension imageSize = null;
protected FormatControl formatControl = null;

protected VideoFormat currentFormat = null;
protected Format[] videoFormats = null;
protected MyVideoFormat[] myFormatList = null;

protected boolean initialised = false;
public String tipo_foto=null;
public String action_php=null;
public JApplet meuap = null;

public JWebCam ( String frameTitle )
{
    statusBar = new JLabel ("");
    statusBar.setBorder ( new EtchedBorder() );
}

public boolean initialise ( )
    throws Exception
{
    return ( initialise ( autoDetect() ) );
}

public boolean initialise ( CaptureDeviceInfo _deviceInfo )
    throws Exception
{
    statusBar.setText ( "Initialising...");
    webCamDeviceInfo = _deviceInfo;

    if ( webCamDeviceInfo != null )
    {
        statusBar.setText ( "Connecting to : " + webCamDeviceInfo.getName() );

        try
        {
            setUpToolBar();
          
            ml = webCamDeviceInfo.getLocator();
            if ( ml != null )
            {
                player = Manager.createRealizedPlayer ( ml );
                if ( player != null )
                {
                    String  cam = webCamDeviceInfo.getName();
                    videoFormats = webCamDeviceInfo.getFormats();
                    
                    int num_player=0;
                    for(int cont=0;cont<videoFormats.length;cont++){
                        if(videoFormats[cont].toString().indexOf("640x480")!=-1){
                        num_player=cont;
                        }
                    }
                    
                    formatControl = (FormatControl)player.getControl ( "javax.media.control.FormatControl" );
                    formatControl.setFormat(webCamDeviceInfo.getFormats()[num_player]);

                    player.start();
                    visualComponent = player.getVisualComponent();
                    if ( visualComponent != null )
                    {
                        
                        visualComponent.setSize(640,480);
                        
                        myFormatList = new MyVideoFormat[videoFormats.length];
                        for ( int i=0; i<videoFormats.length; i++ )
                        {
                            myFormatList[i] = new MyVideoFormat ( (VideoFormat)videoFormats[i] );
                        }

                        Format currFormat = formatControl.getFormat();
                        if ( currFormat instanceof VideoFormat )
                        {
                             currentFormat = (VideoFormat)currFormat;
                             imageSize = currentFormat.getSize();
                         }
                        return ( true );
                    }
                    else
                    {
                        return ( false );
                    }
                }
                else
                {
                    statusBar.setText ( "Cannot create player" );
                    return ( false );
                }
            }
            else
            {
                statusBar.setText ( "No Media Locator for : " + webCamDeviceInfo.getName() );
                return ( false );
            }
        }
        catch ( IOException ioEx )
        {
            statusBar.setText ( "Connecting to : " + webCamDeviceInfo.getName() );

            return ( false );
        }
        catch ( NoPlayerException npex )
        {
            statusBar.setText ("Cannot create player");
            playerClose();
            return ( false );
        }
        catch ( CannotRealizeException nre )
        {
            statusBar.setText ( "Cannot realize player");
            playerClose();
            return ( false );
        }
    }
    else
    {
        return ( false );
    }
}

public void setFormat ( VideoFormat selectedFormat )
{
    if ( formatControl != null )
    {
        player.stop();

        imageSize = selectedFormat.getSize();
        formatControl.setFormat ( selectedFormat );

        player.start();

        statusBar.setText ( "Format : " + selectedFormat );

        currentFormat = selectedFormat;
        visualContainer.setPreferredSize ( currentFormat.getSize() );

        setSize ( imageSize.width, imageSize.height + statusBar.getHeight() + toolbar.getHeight() );
    }
    else
    {
        statusBar.setText ( "Visual component cannot change format" );
    }
}



public VideoFormat getFormat ( )
{
    return ( currentFormat );
}


protected void setUpToolBar ( )
{
    toolbar = new JToolBar();

    formatButton    = new MyToolBarAction ( "Resolution", "BtnFormat.jpg" );
    captureButton   = new MyToolBarAction ( "Capture",    "BtnCapture.jpg" );

    toolbar.add ( formatButton );
    toolbar.add ( captureButton );

}

public void captura ()
    {
        Image photo = grabFrameImage ( );
        if ( photo != null )
        {
            MySnapshot snapshot = new MySnapshot ( photo, new Dimension ( imageSize ) );
        }
  }


protected void toolbarHandler ( MyToolBarAction actionBtn )
{
    if ( actionBtn == formatButton )
    {
        Object selected = JOptionPane.showInputDialog (this,
                                                       "Select Video format",
                                                       "Capture format selection",
                                                       JOptionPane.INFORMATION_MESSAGE,
                                                       null,        //  Icon icon,
                                                       myFormatList, // videoFormats,
                                                       currentFormat );
        if ( selected != null )
        {
            setFormat ( ((MyVideoFormat)selected).format );
        }
    }
    else if ( actionBtn == captureButton )
    {
        Image photo = grabFrameImage ( );
        if ( photo != null )
        {
            MySnapshot snapshot = new MySnapshot ( photo, new Dimension ( imageSize ) );
        }
    }
}

public CaptureDeviceInfo autoDetect ( )
{
    Vector list = CaptureDeviceManager.getDeviceList ( null );
    CaptureDeviceInfo devInfo = null;

    if ( list != null )
    {
        String name;

        for ( int i=0; i<list.size(); i++ )
        {
            devInfo = (CaptureDeviceInfo)list.elementAt ( i );
            name = devInfo.getName();

            if ( name.startsWith ("vfw:") )
            {
                break;
            }
        }

        if ( devInfo != null && devInfo.getName().startsWith("vfw:") )
        {
            return ( devInfo );
        }
        else
        {
            for ( int i = 0; i < 10; i++ )
            {
                try
                {
                    name = VFWCapture.capGetDriverDescriptionName ( i );
                    if (name != null && name.length() > 1)
                    {
                        devInfo = com.sun.media.protocol.vfw.VFWSourceStream.autoDetect ( i );
                        if ( devInfo != null )
                        {
                            return ( devInfo );
                        }
                    }
                }
                catch ( Exception ioEx )
                {
                    statusBar.setText ( "AutoDetect failed : " + ioEx.getMessage() );
                }
            }
            return ( null );
        }
    }
    else
    {
        return ( null );
    }
    
}

public void deviceInfo ( )
{
    if ( webCamDeviceInfo != null )
    {
        Format[] formats = webCamDeviceInfo.getFormats();

        for ( int i=0; i<formats.length; i++ )
        {
            Format aFormat = formats[i];
            if ( aFormat instanceof VideoFormat )
            {
                Dimension dim = ((VideoFormat)aFormat).getSize();
            }
        }
    }
}

public Buffer grabFrameBuffer ( )
{
    if ( player != null )
    {
        FrameGrabbingControl fgc = (FrameGrabbingControl)player.getControl ( "javax.media.control.FrameGrabbingControl" );
        if ( fgc != null )
        {
            return ( fgc.grabFrame() );
        }
        else
        {
            return ( null );
        }
    }
    else
    {
        return ( null );
    }
}

public Image grabFrameImage ( )
{
    Buffer buffer = grabFrameBuffer();
    if ( buffer != null )
    {
        BufferToImage btoi = new BufferToImage ( (VideoFormat)buffer.getFormat() );
        if ( btoi != null )
        {
            Image image = btoi.createImage ( buffer );
            if ( image != null )
            {
                return ( image );
            }
            else
            {
                return ( null );
            }
        }
        else
        {
            return ( null );
        }
    }
    else
    {
        return ( null );
    }
}

public void playerClose ( )
{
    if ( player != null )
    {
        player.close();
    }
}

public void windowClosing ( WindowEvent e )
{
    System.exit ( 1 );
}

public void componentResized ( ComponentEvent e )
{
    Dimension dim = getSize();
    boolean mustResize = false;

    if ( dim.width < MIN_WIDTH )
    {
         dim.width = MIN_WIDTH;
         mustResize = true;
    }

    if ( dim.height < MIN_HEIGHT )
    {
        dim.height = MIN_HEIGHT;
        mustResize = true;
    }

    if ( mustResize )
         setSize ( dim );
}

public void windowActivated ( WindowEvent e )   {   }
 public void windowClosed ( WindowEvent e )      {   }
 public void windowDeactivated ( WindowEvent e ) {   }
 public void windowDeiconified ( WindowEvent e ) {   }
 public void windowIconified ( WindowEvent e )   {   }
 public void windowOpened ( WindowEvent e )      {   }
 public void componentHidden(ComponentEvent e)   {   }
 public void componentMoved(ComponentEvent e)    {   }
 public void componentShown(ComponentEvent e)    {   }

protected void finalize ( ) throws Throwable
{
}

class MyToolBarAction extends AbstractAction
{
    public MyToolBarAction ( String name, String imagefile )
    {
        super ( name );
    }

    public void actionPerformed ( ActionEvent event )
    {
        toolbarHandler ( this );
    }
};


class MyVideoFormat
{
    public VideoFormat format;

    public MyVideoFormat ( VideoFormat _format )
    {
        format = _format;
    }

    public String toString ( )
    {
        Dimension dim = format.getSize();
        return ( format.getEncoding() + " [ " + dim.width + " x " + dim.height + " ]" );
    }
};


class MySnapshot extends JFrame
{
    protected Image photo = null;
    protected int shotNumber;

    public MySnapshot ( Image grabbedFrame, Dimension imageSize)
    {

        shotNumber = shotCounter++;

        photo = grabbedFrame;

        int imageHeight = photo.getWidth  ( this );
        int imageWidth  = photo.getHeight ( this );

        setSize ( imageSize.width, imageSize.height );

        final FileDialog saveDialog = new FileDialog ( this, "Save JPEG", FileDialog.SAVE );
        final JFrame thisCopy = this;
        saveDialog.setFile ( "Photo" + shotNumber );

        addWindowListener ( new WindowAdapter()
            {
                public void windowClosing ( WindowEvent e )
                {
                }
            }
        );

        setVisible ( true );
        
       int i= JOptionPane.showConfirmDialog(null,"Deseja salvar a foto?","Salvar Foto", 2);
       if(i==0){
            try {

				httpConn con = new httpConn();
                con.SendImage(photo, action_php);
                AppletContext browser = meuap.getAppletContext();
                browser.showDocument(new URL("javascript:rodarAjax();"));
                this.setVisible(false);
            } catch (MalformedURLException ex) {
                Logger.getLogger(JWebCam.class.getName()).log(Level.SEVERE, null, ex);
            }
       }
       else if(i==2){
           this.setVisible(false);
       }

    }

    public void paint ( Graphics g )
    {
        g.drawImage ( photo, 0, 0, getWidth(), getHeight(), this );
    }

    public boolean saveJPEG ( String filename )
    {
        boolean saved = false;
        BufferedImage bi = new BufferedImage ( photo.getWidth(null),
                                               photo.getHeight(null),
                                               BufferedImage.TYPE_INT_RGB );
        Graphics2D g2 = bi.createGraphics();
        g2.drawImage ( photo, null, null );
        FileOutputStream out = null;

        try
        {
            out = new FileOutputStream ( filename );
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder ( out );
            JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam ( bi );
            param.setQuality ( 1.0f, false );   // 100% high quality setting, no compression
            encoder.setJPEGEncodeParam ( param );
            encoder.encode ( bi );
            out.close();
            saved = true;
        }
        catch ( Exception ex )
        {

        }

        return ( saved );
    }

}   // of MySnapshot

}

V

Flex é a parada !!! Resolvido.

L

Também estou tentando rodar uma câmera pelo windows 7 com jmf e não tem jeito…

V

Use flex fiz rapido.

Criado 14 de setembro de 2011
Ultima resposta 21 de set. de 2011
Respostas 6
Participantes 4