D
dmcquay
I am a somewhat novice Java programmer and I am brand-new to JAI. I am
having trouble scaling an image using JAI. It works, but the image
comes out distorted. Below is the function in my code that does the
scaling. I know it is the scale function that causes the problem
because I tried skipping the scale part and just creating a RenderedOp
and then doing the JAI.create("filestore", ... and there were no
problems. You can see some of the images I scaled with this code at:
http://www.hairstylesformal.com/749.jpg
http://www.hairstylesformal.com/771.jpg
I have put a lot of work into this app and it is practically done
except for this glitch. Any help would be greatly appreciated.
Thanks,
Dustin
********
CODE
********
public File alterImage(File imgFile)
{
try{
ParameterBlock pb;
InputStream is = new FileInputStream(imgFile);
SeekableStream s = SeekableStream.wrapInputStream(is, true);
RenderedOp objImage = JAI.create("stream", s);
((OpImage)objImage.getRendering()).setTileCache(null);
// Determine image orientation
if(objImage.getWidth() > objImage.getHeight()) {
currImgIsPortrait = false;
} else {
currImgIsPortrait = true;
}
/*
* If the image is too large, scale it down.
*/
if(objImage.getWidth() > MAX_IMG_SIZE || objImage.getHeight() >
MAX_IMG_SIZE) {
float scale = MAX_IMG_SIZE / (float) (currImgIsPortrait ?
objImage.getHeight() : objImage.getWidth());
pb = new ParameterBlock();
pb.addSource(objImage);
pb.add(scale);//x scale factor
pb.add(scale);//y scale factor
pb.add(0.0F);//x translate
pb.add(0.0F);//y translate
pb.add(new InterpolationNearest());//interpolation method
objImage = JAI.create("scale", pb);
}
/*
* Save the image as a temp file
*/
File temp = File.createTempFile("mf_temp_img", ".jpg");
JPEGEncodeParam encodeParam = new JPEGEncodeParam();
encodeParam.setQuality(JPEG_QUALITY);
encodeParam.setHorizontalSubsampling(0,1);
encodeParam.setHorizontalSubsampling(1,1);
encodeParam.setHorizontalSubsampling(2,1);
encodeParam.setVerticalSubsampling(0,1);
encodeParam.setVerticalSubsampling(1,1);
encodeParam.setVerticalSubsampling(2,1);
JAI.create("filestore", objImage, temp.getAbsolutePath(), "jpeg",
encodeParam);
return temp;
} catch(Exception e) {
e.printStackTrace();
return null;
}
}
having trouble scaling an image using JAI. It works, but the image
comes out distorted. Below is the function in my code that does the
scaling. I know it is the scale function that causes the problem
because I tried skipping the scale part and just creating a RenderedOp
and then doing the JAI.create("filestore", ... and there were no
problems. You can see some of the images I scaled with this code at:
http://www.hairstylesformal.com/749.jpg
http://www.hairstylesformal.com/771.jpg
I have put a lot of work into this app and it is practically done
except for this glitch. Any help would be greatly appreciated.
Thanks,
Dustin
********
CODE
********
public File alterImage(File imgFile)
{
try{
ParameterBlock pb;
InputStream is = new FileInputStream(imgFile);
SeekableStream s = SeekableStream.wrapInputStream(is, true);
RenderedOp objImage = JAI.create("stream", s);
((OpImage)objImage.getRendering()).setTileCache(null);
// Determine image orientation
if(objImage.getWidth() > objImage.getHeight()) {
currImgIsPortrait = false;
} else {
currImgIsPortrait = true;
}
/*
* If the image is too large, scale it down.
*/
if(objImage.getWidth() > MAX_IMG_SIZE || objImage.getHeight() >
MAX_IMG_SIZE) {
float scale = MAX_IMG_SIZE / (float) (currImgIsPortrait ?
objImage.getHeight() : objImage.getWidth());
pb = new ParameterBlock();
pb.addSource(objImage);
pb.add(scale);//x scale factor
pb.add(scale);//y scale factor
pb.add(0.0F);//x translate
pb.add(0.0F);//y translate
pb.add(new InterpolationNearest());//interpolation method
objImage = JAI.create("scale", pb);
}
/*
* Save the image as a temp file
*/
File temp = File.createTempFile("mf_temp_img", ".jpg");
JPEGEncodeParam encodeParam = new JPEGEncodeParam();
encodeParam.setQuality(JPEG_QUALITY);
encodeParam.setHorizontalSubsampling(0,1);
encodeParam.setHorizontalSubsampling(1,1);
encodeParam.setHorizontalSubsampling(2,1);
encodeParam.setVerticalSubsampling(0,1);
encodeParam.setVerticalSubsampling(1,1);
encodeParam.setVerticalSubsampling(2,1);
JAI.create("filestore", objImage, temp.getAbsolutePath(), "jpeg",
encodeParam);
return temp;
} catch(Exception e) {
e.printStackTrace();
return null;
}
}