读书人

关于JPEGImageEncoder的有关问题

发布时间: 2012-03-12 12:45:33 作者: rapoo

关于JPEGImageEncoder的问题
我在网上下载了如下代码,在myeclipse中运行时,它提示如下错误:

Access restriction: The type JPEGImageEncoder is not accessible due to restriction on required library C:\Java\jre1.6.0_07\lib\rt.jar
Access restriction: The type JPEGCodec is not accessible due to restriction on required library C:\Java\jre1.6.0_07\lib\rt.jar
Access restriction: The method createJPEGEncoder(OutputStream) from the type JPEGCodec is not accessible due to restriction on required library C:\Java\jre1.6.0_07\lib\rt.jar
Access restriction: The method encode(BufferedImage) from the type JPEGImageEncoder is not accessible due to restriction on required library C:\Java\jre1.6.0_07\lib\rt.jar

但C:\Java\jre1.6.0_07\lib\rt.jar是存在的啊,麻烦哪位给你指明一下?


//缩略图实现,将图片(jpg,gif,bmp等等)真实的变成想要的大小
import java.io.*;
import java.util.*;
import com.sun.image.codec.jpeg.*;
import java.awt.image.*;
import java.awt.*;
import java.net.*;
import java.applet.*;
import java.sql.*;
//缩略图类,
//本java类能将jpg图片文件,进行等比或非等比的大小转换。
//具体使用方法
//s_pic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))
public class thumbs{
String InputDir; //输入图路径
String OutputDir; //输出图路径
String InputFileName; //输入图文件名
String OutputFileName; //输出图文件名
int OutputWidth = 80; //默认输出图片宽
int OutputHeight = 80; //默认输出图片高
int rate = 0;
boolean proportion = true; //是否等比缩放标记(默认为等比缩放)

public thumbs() {
//初始化变量
InputDir = "";
OutputDir = "";
InputFileName = "";
OutputFileName = "";
OutputWidth = 80;
OutputHeight = 80;
rate = 0;
}

public void setInputDir(String InputDir) {
this.InputDir = InputDir;
}

public void setOutputDir(String OutputDir) {
this.OutputDir = OutputDir;
}

public void setInputFileName(String InputFileName) {
this.InputFileName = InputFileName;
}

public void setOutputFileName(String OutputFileName) {
this.OutputFileName = OutputFileName;
}

public void setOutputWidth(int OutputWidth) {
this.OutputWidth = OutputWidth;
}

public void setOutputHeight(int OutputHeight) {
this.OutputHeight = OutputHeight;
}

public void setW_H(int width, int height) {
this.OutputWidth = width;
this.OutputHeight = height;
}

public String s_pic() {
BufferedImage image;
String NewFileName;
//建立输出文件对象
File file = new File(OutputDir + OutputFileName);
FileOutputStream tempout = null;
try {
tempout = new FileOutputStream(file);
}
catch (Exception ex) {
System.out.println(ex.toString());
}
Image img = null;
Toolkit tk = Toolkit.getDefaultToolkit();


Applet app = new Applet();
MediaTracker mt = new MediaTracker(app);
try {
img = tk.getImage(InputDir + InputFileName);
mt.addImage(img, 0);
mt.waitForID(0);
}
catch (Exception e) {
e.printStackTrace();
}

if (img.getWidth(null) == -1) {
System.out.println(" can't read,retry!" + "<BR>");
return "no";
}
else {
int new_w;
int new_h;
if (this.proportion == true) { //判断是否是等比缩放.
//为等比缩放计算输出的图片宽度及高度
double rate1 = ( (double) img.getWidth(null)) / (double) OutputWidth +
0.1;
double rate2 = ( (double) img.getHeight(null)) / (double) OutputHeight +
0.1;
double rate = rate1 > rate2 ? rate1 : rate2;
new_w = (int) ( ( (double) img.getWidth(null)) / rate);
new_h = (int) ( ( (double) img.getHeight(null)) / rate);
}
else {
new_w = OutputWidth; //输出的图片宽度
new_h = OutputHeight; //输出的图片高度
}
BufferedImage buffImg = new BufferedImage(new_w, new_h,
BufferedImage.TYPE_INT_RGB);

Graphics g = buffImg.createGraphics();

g.setColor(Color.white);
g.fillRect(0, 0, new_w, new_h);

g.drawImage(img, 0, 0, new_w, new_h, null);
g.dispose();

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(tempout);
try {
encoder.encode(buffImg);
tempout.close();
}
catch (IOException ex) {
System.out.println(ex.toString());
}
}
return "ok";
}

public String s_pic(String InputDir, String OutputDir, String InputFileName,
String OutputFileName) {
//输入图路径
this.InputDir = InputDir;
//输出图路径
this.OutputDir = OutputDir;
//输入图文件名
this.InputFileName = InputFileName;
//输出图文件名
this.OutputFileName = OutputFileName;
return s_pic();
}

public String s_pic(String InputDir, String OutputDir, String InputFileName,
String OutputFileName, int
width, int height, boolean gp) {
//输入图路径
this.InputDir = InputDir;
//输出图路径
this.OutputDir = OutputDir;
//输入图文件名
this.InputFileName = InputFileName;
//输出图文件名
this.OutputFileName = OutputFileName;
//设置图片长宽
setW_H(width, height);
//是否是等比缩放 标记
this.proportion = gp;
return s_pic();
}

public static void main(String[] a)
{


//s_pic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度)
thumbs mypic = new thumbs();
System.out.println(mypic.s_pic("c:\\", "c:\\",
"test.jpg", "new.jpg", 120, 80, true)
);
}

}

[解决办法]
参考下:

Java code
import java.awt.Graphics2D;import java.awt.Image;import java.awt.Toolkit;import java.awt.image.BufferedImage;import java.io.BufferedOutputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGEncodeParam;import com.sun.image.codec.jpeg.JPEGImageEncoder;/** * 创建图片缩略图 */public class SmallImage{    /**     * @param filename:文件路径     * @param thumbWidth:缩略图的宽     * @param thumbHeight:缩略图的高     * @param quality:图片质量     * @param outFilename:输出文件的路径     * @throws InterruptedException     * @throws FileNotFoundException     * @throws IOException     */    private void createThumbnail(String filename, int thumbWidth,            int thumbHeight, int quality, String outFilename)            throws InterruptedException, FileNotFoundException, IOException    {        // 加载图片        Image image = Toolkit.getDefaultToolkit().getImage(filename);        // 确定缩略图大小的宽度和高度        double thumbRatio = (double) thumbWidth / (double) thumbHeight;        int imageWidth = image.getWidth(null);        int imageHeight = image.getHeight(null);        double imageRatio = (double) imageWidth / (double) imageHeight;        if (thumbRatio < imageRatio)        {            thumbHeight = (int) (thumbWidth / imageRatio);        } else        {            thumbWidth = (int) (thumbHeight * imageRatio);        }        BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight,                BufferedImage.TYPE_INT_RGB);        Graphics2D graphics2D = thumbImage.createGraphics();        graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);        // 保存图像        BufferedOutputStream out = new BufferedOutputStream(                new FileOutputStream(outFilename));        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);        JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);        quality = Math.max(0, Math.min(quality, 100));        param.setQuality((float) quality / 100.0f, false);        encoder.setJPEGEncodeParam(param);        encoder.encode(thumbImage);        out.close();    }    public static void main(String[] args)    {        SmallImage s = new SmallImage();        try        {            s.createThumbnail("D:/1.jpg", 50, 50, 100, "D:/new.jpg");            System.out.println("OK");        } catch (FileNotFoundException e)        {            e.printStackTrace();        } catch (InterruptedException e)        {            e.printStackTrace();        } catch (IOException e)        {            e.printStackTrace();        }    }} 

读书人网 >J2SE开发

热点推荐