经常看到有人在问一个问题:我把我的项目打包以后找不到我的图片文件.我要怎么去显示我的图片文件.
今天在这里我就介绍一下我在这个过程中解决该问题的两种方式供大家参考.
我在处理图片的过程中使用了两种方式,第一:将图片文件放到包中,第二:将图片放到包外.
读取第一种方式的方法很简单,需要写一个方法,
参数path的样式:com/westsoft/cuscomponent/image/icon.png
从你项目的起始包的位置一直写到你图片的名字,注意大小写,即 .PNG和.png是不同的.
//声明一个静态的,final JPanel对象
private static final JPanel panel = new JPanel();
/**
* 根据某个URL得到这个URL代表的图片 并且把该图片导入内存
*
* @param path URL
* @return 一个Image对象
*/
public final static Image getImage(String path) {
URL url = ((URLClassLoader) Util.class.getClassLoader()).findResource(path);
Image im = Toolkit.getDefaultToolkit().createImage(url);
try {
MediaTracker mt = new MediaTracker(panel);
mt.addImage(im, 0);
mt.waitForAll();
} catch (Exception exe) {
exe.printStackTrace();
}
return im;
}
第二种方式即把图片文件放到包以外.这样的好处在于可以动态的更新用到的图片.
/**-----------------------------------------------------------------------------
* Project : CustomComponents
* Package : com.westsoft.toolkit
* FileName: LoadImage.java
*
* Created: 2009-4-1 by 語笑 嫣然
*
* Copyright: Copyright WestSoft 2009-2010
*------------------------------------------------------------------------------
* Description:
*
*
*------------------------------------------------------------------------------*/
package com.westsoft.toolkit;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
/**
* 为编程提供一个统一的读取图片文件的编程接口.该类提供了一系列读取图片文件的方法.
* 这个类有别于GetResource类是因为这个类主要操作软件包以外的图片文件资源
* @author 語笑 嫣然
* @version version:1.00 JDK1.4.2以上支持,一下的版本未做测试.
*/
public class LoadImage {
/**
* Returns an image encoded by the specified input stream
*
* @param is InputStream The input stream encoding the image data
* @return Image The image encoded by the specified input stream
*/
private static Image getImage(InputStream is) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte buf[] = new byte[1024 * 4];
while (true) {
int n = is.read(buf);
if (n == -1)
break;
baos.write(buf, 0, n);
}
baos.close();
return Toolkit.getDefaultToolkit().createImage(baos.toByteArray());
} catch (Throwable e) {
return null;
}
}
/**
* 从一个图片文件中获取一个Image对象.
* Warning:注意检查返回值为NULL的情况.
* @param file 图片文件的File对象
* @return Image instance of Image
*/
public static Image getImage(File file) {
if(!file.exists()) {
JOptionPane.showMessageDialog(null,"指定的图片文件不存在!","错误信息",JOptionPane.ERROR_MESSAGE);
return null;
}
Image image;
try {
FileInputStream fis = new FileInputStream(file);
image = getImage(fis);
fis.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null,"文件读取错误!","错误信息",JOptionPane.ERROR_MESSAGE);
return null;
}
return image;
}
/**
* 从一个以String类型表示的filePath文件中获取Image对象
* Warning:注意检查返回值为NULL的情况.
* @param filePath 图片文件的路径
* @return instance of Image
*/
public static Image getImage(String filePath) {
File imageFile;
imageFile = new File(filePath);
return getImage(imageFile);
}
/**
* 从图片文件中取得一个进行缩放过的Image对象
* Warning:注意检查返回值为NULL的情况.
* @param file 图片文件的File对象
* @param targetW 缩放的目标宽度
* @param targetH 缩放的目标高度
* @return Image 从文件中读取的并进行缩放后的Image对象
*/
public static Image getScaleImage(File file, int targetW, int targetH) {
if(!file.exists()) {
JOptionPane.showMessageDialog(null,"指定的图片文件不存在!","错误信息",JOptionPane.ERROR_MESSAGE);
return null;
}
try {
BufferedImage source = ImageIO.read(file);
// targetW,targetH分别表示目标长和宽
int type = source.getType();
BufferedImage target = null;
double sx = (double) targetW / source.getWidth();
double sy = (double) targetH / source.getHeight();
// 这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放
// 则将下面的if else语句注释即可
if (sx > sy) {
sx = sy;
targetW = (int) (sx * source.getWidth());
} else {
sy = sx;
targetH = (int) (sy * source.getHeight());
}
if (type == BufferedImage.TYPE_CUSTOM) { // handmade
ColorModel cm = source.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
target = new BufferedImage(cm, raster, alphaPremultiplied, null);
} else
target = new BufferedImage(targetW, targetH, type);
Graphics2D g = target.createGraphics();
// smoother than exlax:
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
File obj = new File("临时");
ImageIO.write(target, "JPG", obj);
Image image = getImage(obj);
obj.delete();
return image;
} catch (IOException e) {
JOptionPane.showMessageDialog(null,"文件读取错误!","错误信息",JOptionPane.ERROR_MESSAGE);
return null;
}
}
/**
* 从一个以filePath表示的文件中取得一个进行缩放过的Image对象
* Warning:注意检查返回值为NULL的情况.
* @param filePath 图片文件的路径
* @param targetW 缩放的目标宽度
* @param targetH 缩放的目标高度
* @return 一个从filePath表示的文件中读取的进行缩放后的Image对象
*/
public static Image getScaleImage(String filePath,int targetW,int targetH) {
File imageFile;
imageFile = new File(filePath);
return getScaleImage(imageFile,targetW,targetH);
}
/**
* 从一个图片文件中获取ImageIcon
* Warning:注意检查返回值为NULL的情况.
* @param file 图片文件
* @return instance of ImageIcon
*/
public static ImageIcon getIcon(File file) {
return getIcon(getImage(file));
}
/**
* 从以filePath表示的文件中取得一个ImageIcon对象
* Warning:注意检查返回值为NULL的情况.
* @param filePath 图片文件的路径
* @return instance of ImageIcon
*/
public static ImageIcon getIcon(String filePath) {
return getIcon(getImage(filePath));
}
/**
* Returns an icon based on the specified image
* Warning:注意检查返回值为NULL的情况.
* @param image
* Image The original image
* @return Icon The icon based on the image
*/
public static ImageIcon getIcon(Image image) {
if (image == null)
return null;
return new ImageIcon(image);
}
}
以后在别的地方使用该类的静态方法,通过文件对象或者文件相对路径参数就可以返回Image对象供我们使用
下面放一张我的工程的组织结构.图