我有200张照片来改变大小,但我能做的是1比1 ...任何1可以帮我解决迭代目录问题 [英] I got 200 photo to change size , but what I can do is 1 by 1... Any 1 can help me solve iterate directory problem
问题描述
import javax.imageio.ImageIO;
import java.awt。*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ResizeImageExample {
public static void main(String ... args)抛出IOException {
文件输入=新文件(/ Users / Brantley / Desktop / pt / latest / 2.jpg);
BufferedImage image = ImageIO.read(输入);
调整BufferedImage大小=调整大小(图像,28,28);
文件输出=新文件(/ Users / Brantley / Desktop / pt / jpg / 2.jpg);
ImageIO。写(调整大小,png,输出);
}
private static BufferedImage resize(BufferedImage img,int height,int width){
Image tmp = img.getScaledInstance(width,height,Image.SCALE_SMOOTH);
BufferedImage resized = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = resized.createGraphics();
g2d.drawImage(tmp,0,0,null);
g2d.dispose();
返回调整大小;
}
我是什么尝试过:
i得到200张照片来改变大小,但我能做的是1比1 ...任何1都可以帮我解决迭代目录问题
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ResizeImageExample {
public static void main(String... args) throws IOException {
File input = new File("/Users/Brantley/Desktop/pt/latest/2.jpg");
BufferedImage image = ImageIO.read(input);
BufferedImage resized = resize(image, 28, 28);
File output = new File("/Users/Brantley/Desktop/pt/jpg/2.jpg");
ImageIO.write(resized, "png", output);
}
private static BufferedImage resize(BufferedImage img, int height, int width) {
Image tmp = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage resized = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = resized.createGraphics();
g2d.drawImage(tmp, 0, 0, null);
g2d.dispose();
return resized;
}
What I have tried:
i got 200 photo to change size , but what i can do is 1 by 1... any 1 can help me solve iterate directory problem
推荐答案
请参阅创建和阅读目录(The Java™Tutorials>基本课程>基本I / O) [ ^ ]有关如何使用newDirectoryStream
方法枚举目录中文件的示例。
你也可以使用文件(Java Platform SE 7)之一: listFiles() [ ^ ]方法。
See Creating and Reading Directories (The Java™ Tutorials > Essential Classes > Basic I/O)[^] for examples on how to enumerate files in a directory using thenewDirectoryStream
method.
You may also use one of the File (Java Platform SE 7 ): listFiles()[^] methods.
这篇关于我有200张照片来改变大小,但我能做的是1比1 ...任何1可以帮我解决迭代目录问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!