I am trying to compress bmp image but that is not showing any result, doing with following code. is there something missing in this
is there any other way which help to compress the bmp image
import java.awt.image.BufferedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.IOException;
import java.io.RandomAccessFile;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import com.sun.media.jai.codec.BMPEncodeParam;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.SeekableOutputStream;
public class ExportImages
{
private ImageEncoder encoder = null;
public static void main(
String args[])
{
new ExportImages(args);
}
// Load the source image.
private PlanarImage loadImage(String imageName)
{
ParameterBlock pb = (new ParameterBlock()).add(imageName);
PlanarImage src1 = JAI.create("fileload", pb);
if (src1 == null)
{
System.out.println("Error in loading image " + imageName);
System.exit(1);
}
return src1;
}
/**
* Create seek output stream for scaling
*
* @param outFile
* @return
*/
private SeekableOutputStream createSeekOutputStream(String outFile)
{
SeekableOutputStream out = null;
try
{
out = new SeekableOutputStream(new RandomAccessFile(outFile, "rws"));
}
catch (IOException e)
{
System.out.println("IOException.");
System.exit(1);
}
return out;
}
/**
*
* @param args
*/
public ExportImages(String args[])
{
String inFile = "C:/Test.bmp";
String outputFile = "C:/Modified_image.bmp";
SeekableOutputStream out2 = createSeekOutputStream(outputFile);
PlanarImage src = loadImage(inFile);
BufferedImage bi = src.getAsBufferedImage();
ParameterBlock pb = new ParameterBlock();
pb.addSource(bi);
pb.add(1.0F);
pb.add(1.0F);
PlanarImage image = JAI.create("scale", pb, null).getRendering();
BMPEncodeParam bmpParam = new BMPEncodeParam();
bmpParam.setVersion(BMPEncodeParam.VERSION_3);
// Doing compress
bmpParam.setCompressed(true);
encoder = ImageCodec.createImageEncoder("BMP", out2, bmpParam);
try
{
encoder.encode(image);
out2.close();
}
catch (IOException e)
{
System.out.println("IOException at encoding..");
System.exit(1);
}
}
}
ThnX