java+生成jpg文件,java – 使用ImageIO.write()创建JPEG创建一个0字节的文件

我正在尝试编写一个拍摄图像的方法,并保存该图像的100 x 100缩略图.但是,当我保存文件时,它是一个不可读的0字节图像(错误解释“错误解释JPEG图像文件(在状态200中对JPEG库的错误调用)”)在Ubuntu的

ImageViewer中.我的代码如下:

public boolean scale(){

String file = filename.substring(filename.lastIndexOf(File.separator)+1);

File out = new File("data"+File.separator+"thumbnails"+File.separator+file);

if( out.exists() ) return false;

BufferedImage bi;

try{

bi = ImageIO.read(new File(filename));

}

catch(IOException e){

return false;

}

Dimension imgSize = new Dimension(bi.getWidth(), bi.getHeight());

Dimension bounds = new Dimension(100, 100);

int newHeight = imgSize.height;

int newWidth = imgSize.width;

if( imgSize.width > bounds.width ){

newWidth = bounds.width;

newHeight = (newWidth*imgSize.height)/imgSize.width;

}

if( imgSize.height > bounds.width ){

newHeight = bounds.height;

newWidth = (newHeight*imgSize.width)/imgSize.height;

}

Image img = bi.getScaledInstance(newWidth, newHeight, BufferedImage.SCALE_SMOOTH);

BufferedImage thumb = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_4BYTE_ABGR);

Graphics2D g2d = thumb.createGraphics();

g2d.drawImage(img, 0, 0, null);

g2d.dispose();

try{

ImageIO.write(thumb, "jpg", out);

}

catch(IOException e){

return false;

}

return true;

}

其中“filename”是容纳此方法的类的全局变量,表示原始图像的路径.我的主要问题是我不明白为什么我要创建一个0字节的图像.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值