イメージを取得し、そのイメージの100×100サムネイルを保存するメソッドを作成しようとしています。しかし、ファイルを保存すると、UbuntuのImageViewerで、 "JPEGイメージファイルの解釈エラー(ステート200のJPEGライブラリへの不適切な呼び出し)"というエラーが表示され、0バイトイメージが読み取れなくなります。私のコードは次の通りです:ImageIO.write()を使用してJPEGを作成すると、0バイトのファイルが作成されます。
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バイトのイメージを作成しているのかわかりません。
AFAIK実際に書き込むためにIOバッファを閉じる必要があります。それは一般的にIOがJavaでどのように動作するのか - 間違っている可能性があります。 – Zyerah
ディスクへの書き込み権限があることと、使用可能なディスク容量があることを確認してください。 – Reimeus
@Reimeus - 書き込み権限があり、使用可能なディスク容量があります。少なくとも私は他のファイル(具体的には.txtファイル)を正常に作成することができると信じています。そして、あなたが「ディスクスペース」の意味を誤解していない限り、私はたくさんあります。 –