Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Updated 23-Apr-18 1:23am

1 solution

See Creating and Reading Directories (The Java™ Tutorials > Essential Classes > Basic I/O)[^] for examples on how to enumerate files in a directory using the newDirectoryStream method.

You may also use one of the File (Java Platform SE 7 ): listFiles()[^] methods.
 
Share this answer
 
Comments
BrantleyOng 23-Apr-18 23:13pm    
Sorry bro... i don't really understand how to combine it.. i know what the code say/mean but i have JAVA coding problem every time.
Jochen Arndt 24-Apr-18 2:40am    
With both functions you have a loop providing a single file name. Use that to create the output path and call your existing code.
BrantleyOng 23-Apr-18 23:13pm    
any advice for me to learn faster....
Jochen Arndt 24-Apr-18 2:42am    
There is no advice to learn faster. Learning programming requires time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900