Specify the path, old file name, new file name, and n to the method below. Change the multiple to complete the change of image size.
Copy the code code as follows:
package com.qq.client.tools;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class JpgChange {
// path path, old file name, new file name, n change multiple
public void changeImage(String path, String oldimg, String newimg, int n) {
try {
File file = new File(path + oldimg);
Image img = ImageIO.read(file);
// Construct Image object
int wideth = img.getWidth(null); // Get the source image width
int height = img.getHeight(null); // Get the source image length
BufferedImage tag = new BufferedImage(n * width, n * height,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(img, 0, 0, n * width, n * height, null);
FileOutputStream out = new FileOutputStream(path + newimg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); // Near JPEG encoding
out.close();
} catch (IOException e) {
System.out.println("Exception occurred while processing the file");
e.printStackTrace();
}
}
public static void main(String[] args) {
JpgChange jc = new JpgChange();
jc.changeImage("E://", "1.bmp", "2.bmp", 3);
}
}