aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes BMP compression in JAI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "BMP compression in JAI" Watch "BMP compression in JAI" New topic
Author

BMP compression in JAI

prashant gour
Ranch Hand

Joined: Feb 07, 2006
Posts: 45
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
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41820
    
  62
that is not showing any result

What does this mean? No file is being created? Is the image encoder actually being run? Are there any error messages? If so, post the stack trace.


Ping & DNS - my free Android networking tools app
prashant gour
Ranch Hand

Joined: Feb 07, 2006
Posts: 45
there is no error and image is being created but image does not get compressed even i use method setCompressed(true).

is there any thoughts???
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41820
    
  62
JAI does not support compressed BMPs: JAI tutorial, section 13.4.

The newer javax.imageio package supports compression for BMPs. Here's a relevant article.
[ February 16, 2007: Message edited by: Ulf Dittmer ]
prashant gour
Ranch Hand

Joined: Feb 07, 2006
Posts: 45
I have gone through with given link and did try the given example in link but image does not get compressed, image is being created only with BI_RGB
Not with remaining option of compression type but size does not get decreased.


Did I make any mistake? Looking for your response.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BMP compression in JAI