我的 Java 代码上的访问限制错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12570992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access restriction error on my Java Code
提问by Lucas_Santos
I'm trying to capture the image from my webcam, but I got the error in my saveJPG
method.
我正在尝试从我的网络摄像头捕获图像,但我的saveJPG
方法出错了。
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
This error occurs in my saveJPG
method in the line where I have
这个错误发生在我的saveJPG
方法中
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(0.5f,false);
encoder.setJPEGEncodeParam(param);
CODE:
代码:
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");
}
}
IMPORTS:
进口:
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.*;
回答by S. Cambon
The Eclipse Java compiler attempts to prevent you from using non-public APIs. In classic Java, the visibility concept is quite primitive and therefore library designers often have to put in the public space classes solely created for internal use. This is not the case with more evolved frameworks such as OSGi.
Eclipse Java 编译器试图阻止您使用非公共 API。在经典 Java 中,可见性概念非常原始,因此库设计者通常不得不将专为内部使用而创建的公共空间类放入。更先进的框架(如 OSGi)并非如此。
If you still want to access this class, you can do as described in this blog post.
如果你仍然想访问这个类,你可以按照这篇博文中的描述进行。
回答by user2542398
I was able to get rid of this error by following the suggestion:
我能够按照建议摆脱这个错误:
- Open project properties.
- Select Java Build Path node.
- Select Libraries tab.
- Remove JRE System Library.
- Add Library JRE System Library.
- 打开项目属性。
- 选择 Java 构建路径节点。
- 选择库选项卡。
- 删除 JRE 系统库。
- 添加库 JRE 系统库。