I am making a photo sorting program. The FileUtils.moveFile(from, to) works fine most of the time. It does not work when I rotate and copy the meta data of an image. If I rotate and copy the data then close the program, restart the program it will work. But if I rotate and copy the meta data then try and move a file I get the cannot delete source file error from the moveFile method. I had this problem before and it was due to me not closing streams. When I wrote the rotate method I thought I covered them all but I guess I am missing one. Here is my 2 methods to rotate and copy the meta data of an image:
private void copyMetaRotateRight(File imageInput, File imageOutput) throws IOException {
ImageReader reader = ImageIO.getImageReadersBySuffix("jpg").next();
reader.setInput(ImageIO.createImageInputStream(imageInput));
IIOMetadata metadata = null;
try {
metadata = reader.getImageMetadata(0);
}catch(IIOException e){
e.printStackTrace();
}
rotateRight(imageInput,imageOutput);
BufferedImage bi = reader.read(0);
ImageOutputStream ios = ImageIO.createImageOutputStream(imageOutput);
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = iter.next();
writer.setOutput(ios);
writer.write(null,new IIOImage(bi, null, metadata), null);
writer.dispose();
reader.dispose();
ios.close();
displayImage(currentIndex);
}
public void rotateRight(File input, File output) {
BufferedImage src = null;
try {
src = read(input);
} catch (IOException e) {
e.printStackTrace();
}
assert src != null;
int width = src.getWidth();
int height = src.getHeight();
BufferedImage dest = new BufferedImage(height, width, src.getType());
Graphics2D graphics2D = dest.createGraphics();
graphics2D.translate((height - width) / 2, (height - width) / 2);
graphics2D.rotate(Math.PI / 2, height / 2, width / 2);
graphics2D.drawRenderedImage(src, null);
try {
ImageInputStream iis = ImageIO.createImageInputStream(input);
Iterator<ImageReader> iterator = ImageIO.getImageReaders(iis);
ImageReader reader = iterator.next();
String format = reader.getFormatName();
ImageIO.write(dest,format,output);
reader.dispose();
graphics2D.dispose();
iis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Okay I took the advice and I tried the try with resource. Here is my new still not working code:
private void copyMetaRotateRight(File imageInput, File imageOutput) throws IOException {
try(ImageOutputStream ios = ImageIO.createImageOutputStream(imageOutput);
ImageInputStream inputStream = ImageIO.createImageInputStream(imageInput);
){
ImageReader reader = ImageIO.getImageReadersBySuffix("jpg").next();
reader.setInput(inputStream);
IIOMetadata metadata = reader.getImageMetadata(0);
rotateRight(imageInput,imageOutput);
BufferedImage bi = reader.read(0);
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = iter.next();
writer.setOutput(ios);
writer.write(null,new IIOImage(bi, null, metadata), null);
displayImage(currentIndex);
}
catch(Exception e){
e.printStackTrace();
}
}
public void rotateRight(File input, File output) {
try(FileInputStream stream = new FileInputStream(input);
ImageInputStream iis = ImageIO.createImageInputStream(input);
){
BufferedImage src = ImageIO.read(stream);
assert src != null;
int width = src.getWidth();
int height = src.getHeight();
BufferedImage dest = new BufferedImage(height, width, src.getType());
Graphics2D graphics2D = dest.createGraphics();
graphics2D.translate((height - width) / 2, (height - width) / 2);
graphics2D.rotate(Math.PI / 2, height / 2, width / 2);
graphics2D.drawRenderedImage(src, null);
Iterator<ImageReader> iterator = ImageIO.getImageReaders(iis);
ImageReader reader = iterator.next();
String format = reader.getFormatName();
ImageIO.write(dest,format,output);
}catch(IOException e){
e.printStackTrace();
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…