Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading - Resizing - Writing Images

 
Mike DeStefano
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been banging my head against the wall (literally) trying to figure out how to do something that should be very easy.
I want to reading in an image file - "logo.jpg".
I want to resize the width and height to 150%.
I want to write the resized image to "logoResized.jsp".
First thing I tried was to read the image file into a BufferedImage. I can scale it using the method getScaledInstance(). This gives me an Image instance. The problem here is I can't see a way of writing the image to a file. ImageWriter wants a to write a RenderedImage, which I no longer have.
I have tried using an ImageReadParam to setSourceRenderSize(dimension) but I get an Exception thrown back because every reader I get has canSetSourceRenderSize() == false.

Here's the code I'm currently using which doesn't work to read, resize, and write a resized image.
Has anyone had success altering and writing images?
String fileBase = "C:/images";
String src = "/logo.gif";
String fileTy = src.substring(src.lastIndexOf('.')+1);
File file = new File(fileBase + src);
ImageInputStream iis = ImageIO.createImageInputStream(file);
ImageReader reader = null;
ImageReadParam param = null;
//for (Iterator readers = ImageIO.getImageReaders(iis); readers.hasNext() {
for (Iterator readers = ImageIO.getImageReadersByFormatName(fileTy); readers.hasNext() {
reader = (ImageReader)readers.next();
param = reader.getDefaultReadParam();
System.out.println(param.canSetSourceRenderSize());
if (param.canSetSourceRenderSize()) {
break;
}
}
int imageIndex = 0;
double widthMod = 1.5;
double heightMod = 1.5;
double baseWidth = 800;
double baseHeight = 600;
int picWidth = 181;
int picHeight = 78;
int outWidth = (int)(picWidth * widthMod + .5);
int outHeight = (int)(picHeight * heightMod + .5);

BufferedImage bi = null;
//Rectangle rect = new Rectangle(0, 0, outWidth, outHeight);
//param.setSourceRegion(rect);
param.setSourceRenderSize(new Dimension(outWidth, outHeight));
//param.setDestination(bi);
reader.setInput(iis, true);
bi = reader.read(imageIndex, param);
System.out.println(bi.getWidth() + "x" + bi.getHeight());
//Image im = bi.getScaledInstance(outWidth, outHeight, BufferedImage.SCALE_DEFAULT);
File outFile = new File(fileBase + src.substring(0,src.lastIndexOf('.')) + "Resized." + fileTy);
ImageIO.write(bi, fileTy, outFile);
 
Warren Bell
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the Java Advance Imaging API or JAI for short. Below is a sample of JAI code that will do what you stated in your post.
/*****begins*****/
javax.media.jai.PlanarImage image;
// Load your JPEG image here
image = javax.media.jai.JAI.create("fileload", "logo.jpg");
// Scale your JPEG image here
float scaleFactor = 0.5F; // Scale to one half of the original size
javax.media.jai.ParameterBlockJAI pb = new javax.media.jai.ParameterBlockJAI("scale");
pb.addSource(image);
pb.setParameter("xScale", scaleFactor);
pb.setParameter("yScale", scaleFactor);
pb.setParameter("interpolation", javax.media.jai.Interpolation.getInstance(javax.media.jai.Interpolation.INTERP_BILINEAR));
image = javax.media.jai.JAI.create("scale", pb);
// Save and encode your JPEG image here
FileOutputStream out = new FileOutputStream(new File("logoResized.jsp"));
JPEGEncodeParam encodeParam = new JPEGEncodeParam();
encodeParam.setQuality(0.6F); // Resolution
encodeParam.setHorizontalSubsampling(0, 1);
encodeParam.setHorizontalSubsampling(1, 2);
encodeParam.setHorizontalSubsampling(2, 2);
encodeParam.setVerticalSubsampling(0, 1);
encodeParam.setVerticalSubsampling(1, 1);
encodeParam.setVerticalSubsampling(2, 1);
encodeParam.setRestartInterval(64);
encodeImage(image, out);
/*****ends*****/
There are other ways of doing the same thing. Check out the JAI home page at Sun.
Let me know if this helps.
Warren Bell
Netricks
warren@netricks.net
 
LOURDIN Julien
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JAI seems to be powerful : I want to try.
But, I've got some problems :
- I went to java.sun.com to obtain this library
- I find executable install files, but just need the library
- The download of these files seems to be impossible
- I found 1 jar files but it doesn't work with jar command

To be more exact and may be if someone knows, I also ask the following questions :
- I want to resize JPEG files => no problem with the code above
- Is this really impossible to resize JPEG with an appropriate ImageWriter (in javax.imageio library) ?
- I want also to resize GIF => is there a solution since GIF is not licensed (7 jul. 2004 according to an article in this forum)

Thanks a lot.


Originally posted by Warren Bell:
Try the Java Advance Imaging API or JAI for short. Below is a sample of JAI code that will do what you stated in your post.

/*****begins*****/

javax.media.jai.PlanarImage image;

// Load your JPEG image here

image = javax.media.jai.JAI.create("fileload", "logo.jpg");

// Scale your JPEG image here

float scaleFactor = 0.5F; // Scale to one half of the original size

javax.media.jai.ParameterBlockJAI pb = new javax.media.jai.ParameterBlockJAI("scale");
pb.addSource(image);
pb.setParameter("xScale", scaleFactor);
pb.setParameter("yScale", scaleFactor);
pb.setParameter("interpolation", javax.media.jai.Interpolation.getInstance(javax.media.jai.Interpolation.INTERP_BILINEAR));
image = javax.media.jai.JAI.create("scale", pb);

// Save and encode your JPEG image here

FileOutputStream out = new FileOutputStream(new File("logoResized.jsp"));

JPEGEncodeParam encodeParam = new JPEGEncodeParam();

encodeParam.setQuality(0.6F); // Resolution

encodeParam.setHorizontalSubsampling(0, 1);
encodeParam.setHorizontalSubsampling(1, 2);
encodeParam.setHorizontalSubsampling(2, 2);
encodeParam.setVerticalSubsampling(0, 1);
encodeParam.setVerticalSubsampling(1, 1);
encodeParam.setVerticalSubsampling(2, 1);
encodeParam.setRestartInterval(64);
encodeImage(image, out);

/*****ends*****/

There are other ways of doing the same thing. Check out the JAI home page at Sun.

Let me know if this helps.

Warren Bell
Netricks
warren@netricks.net
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!