Java changing image size sample share
- 2020-04-01 03:04:50
- OfStack
Specify a path to the following method, old file name, new file name, and n change multiples to complete the change of image size
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, 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 graph length
BufferedImage tag = new BufferedImage(n * wideth, n * height,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(img, 0, 0, n * wideth, n * height, null);
FileOutputStream out = new FileOutputStream(path + newimg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); //Nearly JPEG coding
out.close();
} catch (IOException e) {
System.out.println(" An exception occurred in the processing file ");
e.printStackTrace();
}
}
public static void main(String[] args) {
JpgChange jc = new JpgChange();
jc.changeImage("E:\", "1.bmp", "2.bmp", 3);
}
}