Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BMP compression in JAI

 
prashant gour
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
prashant gour
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic