Access restriction: is not accessible

最近在作關於圖片操做的問題,可是發如今eclipse中,對於某些rt.jar裏面的方法訪問的時候,會出現錯誤提示: Access restriction: XXXXXXXXX  is not accessible due to restriction on required library XXXXX
本例的錯誤信息爲 :  程序包com.sun.image.codec.jpeg不存在
一下所引入的包也有可能會發生問題: 例如  import sun.misc.BASE64Decoder;  import com.sun.image.codec.jpeg.JPEGCodec;
會發現有一些解決方案:

解決辦法一:
1. Open project properties.
2. Select Java Build Path node.
3. Select Libraries tab.
4. Remove JRE System Library 一次
5. 再次 Add Library JRE System Library

解決方案二:
Window-->Preferences-->Java-->Compiler-->Error/Warnings-->Deprecated and Restricted API  改成 warning

其實若是是操做某些非公開的JDK API的話,會出現這些問題,能夠使用上述的解決方案,可是建議使用其餘的方案代替,例以下例所給出的解決方案,用scaleImage2 代替 scaleImage 不使用受保護的方法 java


package com.zy.common.util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;


public class ImagesUtil {

	/**
     * 縮略圖片
     * @param oldpath 原圖片
     * @param newpath 新生成的圖片存放地址
     * @param wdith   縮略後的寬
     * @param height  縮略後的高
     */
    public static void scaleImage(String oldpath, String newpath, int wdith, int height) {
    	// 獲取老的圖片
    	File oldimg = new File(oldpath);
		try {
			BufferedImage bi = ImageIO.read(oldimg);
			Image Itemp = bi.getScaledInstance(wdith, height, BufferedImage.SCALE_SMOOTH);
			BufferedImage thumbnail = new BufferedImage(wdith, height, BufferedImage.TYPE_INT_RGB);
			thumbnail.getGraphics().drawImage(Itemp, 0, 0, null);
			// 縮略後的圖片路徑
			File newimg = new File(newpath);
			//FileOutputStream out = new FileOutputStream(newimg);
			// 繪圖
			//JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			//JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbnail);
			//param.setQuality(1.0f, false);
			//encoder.encode(thumbnail);
			//out.close();
			String formatName = newpath.substring(newpath.lastIndexOf(".") + 1); 
			ImageIO.write(thumbnail,  formatName , new File(newpath) );
			bi.flush();
			bi = null;
		} catch (IOException e) {
			System.out.println(e.getStackTrace());
		}
    }
    
    /**
     * 縮略圖片
     * @param oldpath 原圖片
     * @param newpath 新生成的圖片存放地址
     * @param wdith   縮略後的寬
     * @param height  縮略後的高
     */
    public static void scaleImage2(String oldpath, String newpath, int wdith, int height) {
    	// 獲取老的圖片
    	File oldimg = new File(oldpath);
		try {
			BufferedImage bi = ImageIO.read(oldimg);
			Image Itemp = bi.getScaledInstance(wdith, height, BufferedImage.SCALE_SMOOTH);
			BufferedImage thumbnail = new BufferedImage(wdith, height, BufferedImage.TYPE_INT_RGB);
			thumbnail.getGraphics().drawImage(Itemp, 0, 0, null);
			// 縮略後的圖片路徑
			File newimg = new File(newpath);
			FileOutputStream out = new FileOutputStream(newimg);
			// 繪圖
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbnail);
			param.setQuality(1.0f, false);
			encoder.encode(thumbnail);
			out.close();
			bi.flush();
			bi = null;
		} catch (IOException e) {
			System.out.println(e.getStackTrace());
		}
    }

}
相關文章
相關標籤/搜索