Link to home
Start Free TrialLog in
Avatar of Lara_tin2
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().getImage(inPath));

        BufferedImage resizedImage = this.scaleImage(sourceImage,targetWidth,targetHeight);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
        try {
            encoder.encode(resizedImage);
        } 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(sourceImage.getSource(),filter);
            Image resizedImage = Toolkit.getDefaultToolkit().createImage(producer);
            return this.toBufferedImage(resizedImage);
        }
    /**
     * convert a Image object to BufferredImage
     * @param image
     * @return
     */
 private BufferedImage toBufferedImage(Image image){
            image = new ImageIcon(image).getImage();
            BufferedImage bufferedImage = new BufferedImage(image.getWidth(null)
                    ,image.getHeight(null),BufferedImage.TYPE_INT_RGB);
            Graphics g = bufferedImage.createGraphics();
            g.setColor(Color.white);
            g.fillRect(0,0,150,150);
            g.drawImage(image,0,0,null);
            g.dispose();
            return bufferedImage;
}

SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lara_tin2
Lara_tin2

ASKER

Thanks for all.
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial