解决方案 »
- 编程解决各种问题(二) ----------------Flash 经典智力游戏(二) 和尚和鬼的故事
- feed.setEntries(itemList)的所有数据相同
- 读取WORD2007需要openxml4j jar包,哪里有下?
- getHibernateTemplate()为什么需要flush()?
- 如何快速学习weblogic
- Struts中一个JSP一次注册两个用户,如何实现?
- 关于Spring封装Hibernate后怎样条件查询?
- tomcat5.5和tomcat5.0的问题?
- JAVA语法问题??
- JAVA基础问题(一)
- struts2 求帮助
- 数据库连接池中各个属性能否详细解释下
JDK 1.5 图片格式JPG
private static void createSlt(File imageFile, String sltFilePath, int limit) { FileOutputStream fos = null; BufferedImage bi = null; try {
fos = new FileOutputStream(sltFilePath);
bi = ImageIO.read(imageFile); int newWidth = 0;
int newHeight = 0;
int yWidth = bi.getWidth(null);
int yHeight = bi.getHeight(null); if (yWidth > limit || yHeight > limit) {
if (yWidth > yHeight) {
float rate = (float) yWidth / limit;
newWidth = limit;
newHeight = (int) Math.rint(yHeight / rate);
} else {
float rate = (float) yHeight / limit;
yHeight = limit;
newWidth = (int) Math.rint(yWidth / rate);
}
} else {
newWidth = bi.getWidth(null);
newHeight = bi.getHeight(null);
} BufferedImage bufferedImage = new BufferedImage(newWidth,
newHeight, BufferedImage.TYPE_INT_RGB);
bufferedImage.getGraphics().drawImage(
bi.getScaledInstance(newWidth, newHeight, bi.SCALE_SMOOTH),
0, 0, null); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
encoder.encode(bufferedImage); } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}这个是完整的创建缩略图的方法,bi返回NULL
image = ImageIO.read(file);我的image文件大小为2.1MB, 没有问题!
JDK6u20
这里顺便贴出来ImageIO可以读写的图片格式。
用:String[] rf = ImageIO.getReaderFormatNames();
String[] wf = ImageIO.getWriterFormatNames();rf:jpg,rf:BMP,rf:bmp,rf:JPG,rf:jpeg,rf:wbmp,rf:png,rf:JPEG,rf:PNG,rf:WBMP,rf:GIF,rf:gif
wf:BMP,wf:bmp,wf:jpg,wf:JPG,wf:wbmp,wf:jpeg,wf:png,wf:PNG,wf:JPEG,wf:WBMP,wf:GIF,wf:gifImageIO应该不会因为图片太大返回NULL,就算图片太大只会抛出内存溢出的错误。
OK,结贴了。