Lara_tin2
asked on
Resize image in Java
Hi All!!!
Now I'm developping a web application, which run on a JBoss server with jsp/struts.
In my app, I need resize some image.I use java AWT for this, it's ok in my enviroment. but in the customer enviroment it throws an exception:
java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
after play with google, I found It caused by a AWT bug when run in Linux so I dont want to use AWT to resize the image.
Could anyone show me the solution? or show me what's my mistake when use AWT?
Regards.
************************** ********** ********** ********** *****
My code:
public void doResize (String inPath,OutputStream os, int targetWidth,int targetHeight) throws CBException {
Image sourceImage = (Toolkit.getDefaultToolkit ().getImag e(inPath)) ;
BufferedImage resizedImage = this.scaleImage(sourceImag e,targetWi dth,target Height);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncode r(os);
try {
encoder.encode(resizedImag e);
} catch (IOException e) {
throw new CBException("Exception when encode image to JPG");
}
}
private BufferedImage scaleImage(Image sourceImage, int width, int height){
ImageFilter filter = new ReplicateScaleFilter(width ,height);
ImageProducer producer = new FilteredImageSource(source Image.getS ource(),fi lter);
Image resizedImage = Toolkit.getDefaultToolkit( ).createIm age(produc er);
return this.toBufferedImage(resiz edImage);
}
/**
* convert a Image object to BufferredImage
* @param image
* @return
*/
private BufferedImage toBufferedImage(Image image){
image = new ImageIcon(image).getImage( );
BufferedImage bufferedImage = new BufferedImage(image.getWid th(null)
,image.getHeight(null),Buf feredImage .TYPE_INT_ RGB);
Graphics g = bufferedImage.createGraphi cs();
g.setColor(Color.white);
g.fillRect(0,0,150,150);
g.drawImage(image,0,0,null );
g.dispose();
return bufferedImage;
}
Now I'm developping a web application, which run on a JBoss server with jsp/struts.
In my app, I need resize some image.I use java AWT for this, it's ok in my enviroment. but in the customer enviroment it throws an exception:
java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
after play with google, I found It caused by a AWT bug when run in Linux so I dont want to use AWT to resize the image.
Could anyone show me the solution? or show me what's my mistake when use AWT?
Regards.
**************************
My code:
public void doResize (String inPath,OutputStream os, int targetWidth,int targetHeight) throws CBException {
Image sourceImage = (Toolkit.getDefaultToolkit
BufferedImage resizedImage = this.scaleImage(sourceImag
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncode
try {
encoder.encode(resizedImag
} catch (IOException e) {
throw new CBException("Exception when encode image to JPG");
}
}
private BufferedImage scaleImage(Image sourceImage, int width, int height){
ImageFilter filter = new ReplicateScaleFilter(width
ImageProducer producer = new FilteredImageSource(source
Image resizedImage = Toolkit.getDefaultToolkit(
return this.toBufferedImage(resiz
}
/**
* convert a Image object to BufferredImage
* @param image
* @return
*/
private BufferedImage toBufferedImage(Image image){
image = new ImageIcon(image).getImage(
BufferedImage bufferedImage = new BufferedImage(image.getWid
,image.getHeight(null),Buf
Graphics g = bufferedImage.createGraphi
g.setColor(Color.white);
g.fillRect(0,0,150,150);
g.drawImage(image,0,0,null
g.dispose();
return bufferedImage;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Resize image is a common operation when use image so I think it must have a java library.But I cant find anything :(.
What I want is an easy-to-use and light weight java library to do it.If you know one, please let me know.
objects:and jjerome00 , I will try your comment and feedback later.