program that compresses windows BMP image file using java
- First of all you need bmp image file format this file in your java file folder .
- Image file name is input and after the run compress.java then automatically generate.bmp
- Then you check file size is very small compare to input size file.
- This is java code for compresses windows BMP image file.
import java.io.*;
import java.util.Iterator;
import javax.imageio.*;
import javax.imageio.stream.*;
public class compress {
public static void main(String[] args) throws IOException {
File input = new File("input.bmp");
BufferedImage image = ImageIO.read(input);
File compressedImageFile = new File("generate.bmp");
OutputStream os = new FileOutputStream(compressedImageFile);
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.05f); // Change the quality value you prefer
writer.write(null, new IIOImage(image, null, null), param);
os.close();
ios.close();
writer.dispose();
}
}
Comments
Post a Comment