java加载图片问题
我想做一个自定义缩略图片的按钮,但是在读jpg格式图片时总是抛出异常:Exceptioninthread"main"javax.imageio.IIOException:...
我想做一个自定义缩略图片的按钮,但是在读jpg格式图片时总是抛出异常:
Exception in thread "main" javax.imageio.IIOException: Can't read input file!
具体代码:
//工具类,读取文件在getImage()方法进行
public class ThumbnailatorTool {
private static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
// targetW,targetH分别表示目标长和宽
int type = source.getType();
BufferedImage target = null;
double sx = (double) targetW / source.getWidth();
double sy = (double) targetH / source.getHeight();
if (sx > sy) {
sx = sy;
targetW = (int) (sx * source.getWidth());
} else {
sy = sx;
targetH = (int) (sy * source.getHeight());
}
if (type == BufferedImage.TYPE_CUSTOM) {
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();
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
return target;
}
public static Image getImage(String fromFileStr, int width, int hight) throws Exception {
BufferedImage srcImage;
File fromFile = new File(fromFileStr);
srcImage = ImageIO.read(fromFile);
if (width > 0 || hight > 0) {
srcImage = resize(srcImage, width, hight);
}
return (Image) srcImage;
}
}
——————————————————————————————————————————
//这是另外一个类里面的一个方法,传进的src就是文件地址,当是jpg图片时就抛异常...
private void addButton(String src) throws Exception {
ImageIcon icon = new ImageIcon(ThumbnailatorTool.getImage(src, width, height));
JButton button = new JButton(icon);
button.setOpaque(false);
button.setContentAreaFilled(false);
panel.add(button);
} 展开
Exception in thread "main" javax.imageio.IIOException: Can't read input file!
具体代码:
//工具类,读取文件在getImage()方法进行
public class ThumbnailatorTool {
private static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
// targetW,targetH分别表示目标长和宽
int type = source.getType();
BufferedImage target = null;
double sx = (double) targetW / source.getWidth();
double sy = (double) targetH / source.getHeight();
if (sx > sy) {
sx = sy;
targetW = (int) (sx * source.getWidth());
} else {
sy = sx;
targetH = (int) (sy * source.getHeight());
}
if (type == BufferedImage.TYPE_CUSTOM) {
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();
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
return target;
}
public static Image getImage(String fromFileStr, int width, int hight) throws Exception {
BufferedImage srcImage;
File fromFile = new File(fromFileStr);
srcImage = ImageIO.read(fromFile);
if (width > 0 || hight > 0) {
srcImage = resize(srcImage, width, hight);
}
return (Image) srcImage;
}
}
——————————————————————————————————————————
//这是另外一个类里面的一个方法,传进的src就是文件地址,当是jpg图片时就抛异常...
private void addButton(String src) throws Exception {
ImageIcon icon = new ImageIcon(ThumbnailatorTool.getImage(src, width, height));
JButton button = new JButton(icon);
button.setOpaque(false);
button.setContentAreaFilled(false);
panel.add(button);
} 展开
1个回答
2015-04-25
展开全部
ImageIcon 可以直接读图片文件的呀
检查路径是否正确吧。
检查路径是否正确吧。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询