我尝试使用ImageIO类保存图片,但我发现图片大小很小的问题。代码是这样的:
public class SeamCarving {
static String path="C:/Users/lenovo/Desktop/h4/01.jpg";
public static void main(String[] args)throws Exception {
File file1=new File(path);
System.out.println(file1.length());
BufferedImage image=ImageIO.read(file1);
File file2=new File("d:/02.jpg");
ImageIO.write(image, "jpg",file2);
image.flush();
System.out.println(file2.length());
}
}
手术后,我发现尺寸为4788268和1529534。 所以我无法理解为什么图片的大小很小。
答案 0 :(得分:0)
您可以尝试使用FileInputStream
和FileOutputStream
类:
public class SeamCarving {
static String pathFrom = "C:/Users/lenovo/Desktop/h4/01.jpg";
static String pathTo = "d:/02.jpg";
public static void main(String[] args) throws Exception {
File file1 = new File(pathFrom);
File file2 = new File(pathTo);
FileInputStream fis = new FileInputStream(pathFrom);
FileOutputStream fos = new FileOutputStream(pathTo);
System.out.println(file1.length());
int buffSize;
byte[] buffer = new byte[1024];
while ((buffSize = fis.read(buffer)) > 0) {
fos.write(buffer, 0, buffSize);
}
System.out.println(file2.length());
fis.close();
fos.close();
}
}