2012-09-24 13 views
5

Estoy tratando de capturar la imagen de mi cámara web, pero obtuve el error en mi método saveJPG.Error de restricción de acceso en mi código de Java

ERROR:

Multiple markers at this line 
    - Access restriction: The type JPEGCodec is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar 
    - Access restriction: The method createJPEGEncoder(OutputStream) from the type JPEGCodec is not accessible due to restriction on required library C: 
    \Program Files\Java\jre7\lib\rt.jar 
    - Access restriction: The type JPEGImageEncoder is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar 

Este error se produce en mi método saveJPG en la línea en la que tengo

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi); 
param.setQuality(0.5f,false); 
encoder.setJPEGEncodeParam(param);  

CÓDIGO:

JButton startC = new JButton("Capturar"); 
    startC.setPreferredSize(new Dimension(100,22)); 

    startC.addActionListener(new ActionListener() 
      { 

       @Override 
       public void actionPerformed(ActionEvent e) 
       {     
         // Grab a frame 
         FrameGrabbingControl fgc = (FrameGrabbingControl) 
         player.getControl("javax.media.control.FrameGrabbingControl");  
         buf = fgc.grabFrame();  

         // Convert it to an image 
         btoi = new BufferToImage((VideoFormat)buf.getFormat()); 
         img = btoi.createImage(buf);  

         // show the image 
         //imgpanel.setImage(img);  

         // save image 
         saveJPG(img,"c:\\test.jpg"); 
       } 
      });  



    public static void saveJPG(Image img, String s) 
      { 
      BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB); 
      Graphics2D g2 = bi.createGraphics();  
      g2.drawImage(img, null, null); 
      FileOutputStream out = null; 

      try 
      { 
       out = new FileOutputStream(s);  
      } 
      catch (java.io.FileNotFoundException io)  
      { 
       System.out.println("File Not Found"); 
      } 

      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 

      JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi); 
      param.setQuality(0.5f,false); 
      encoder.setJPEGEncodeParam(param);  

      try 
      { 
       encoder.encode(bi); 
       out.close(); 
      } 
      catch (java.io.IOException io) 
      { 
       System.out.println("IOException");  
      } 
      } 

IMPORTACIONES:

import static com.googlecode.javacv.jna.highgui.cvCreateCameraCapture; 
import static com.googlecode.javacv.jna.highgui.cvGrabFrame; 
import static com.googlecode.javacv.jna.highgui.cvReleaseCapture; 
import com.googlecode.javacv.CanvasFrame; 
import com.googlecode.javacv.FrameGrabber; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import java.io.BufferedInputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.Enumeration; 
import java.util.List; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipFile; 
import javax.swing.JApplet; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JComponent; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 
import com.colorfulwolf.webcamapplet.gui.LabelPanel; 
import com.colorfulwolf.webcamapplet.gui.LoadingScreen; 
import com.googlecode.javacv.jna.highgui.CvCapture; 
import java.awt.BorderLayout; 
import javax.media.*; 
import javax.media.protocol.*; 
import javax.media.util.BufferToImage; 
import javax.swing.*; 
import java.awt.*; 
import java.util.*; 
import javax.media.control.*; 
import javax.media.format.*; 
import com.sun.image.codec.jpeg.*; 
+0

¿Estás utilizando Eclipse? Hace un tiempo tuve algo así también, pero eso fue un error de Eclipse (en índigo, la versión actual Juno tiene una solución para esto). – 11684

+0

@ 11684 Estoy usando Eclipse Juno –

+0

¿Dónde está el contenedor que contiene el paquete 'com.sun.image.codec.jpeg'? – 11684

Respuesta

13

El Eclipse Java compilador intenta impedir el uso de las API que no son públicas. En Java clásico, el concepto de visibilidad es bastante primitivo y, por lo tanto, los diseñadores de bibliotecas a menudo tienen que poner en las clases de espacio público creadas únicamente para uso interno. Este no es el caso con frameworks más evolucionados como OSGi.

Si aún desea acceder a esta clase, puede hacer lo que se describe en this blog post.

10

yo era capaz de deshacerse de este error, siguiendo la sugerencia:

  1. Abra las propiedades del proyecto.
  2. Seleccione el nodo Java Build Path.
  3. Seleccione la pestaña Bibliotecas.
  4. Eliminar JRE System Library.
  5. Agregar biblioteca JRE System Library.
+1

¡Gracias, es trabajos! – Cheung

Cuestiones relacionadas