?
Solved

Resize image in Java

Posted on 2005-02-24
4
Medium Priority
?
1,484 Views
Last Modified: 2008-01-09
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;
}

0
Comment
Question by:Lara_tin2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 750 total points
ID: 13390643
not really a bug, it is caused by there being no window environment running on the box (in this case X windows)


you could try using a headless implementation by adding the following VM arg to the JBoss startup option.

-Djava.awt.headless=true

Or an alternative to AWT is the PJA toolkit

http://www.eteks.com/pja/en/
0
 
LVL 2

Accepted Solution

by:
jjerome00 earned 750 total points
ID: 13395698

I'm interested in where your production environment is.  Most hosting companies won't allow you to resize an image this way because it would take too much resources from the Server.  I mention this because I used an image tag lib from Apache for something similar to this, then came across the same issue.

BTW, the image tag library that does some pretty cool image resizing and so forth.  It's still in development, but it works great.  You might want to check it out:
http://jakarta.apache.org/taglibs/sandbox/doc/image-doc/intro.html

0
 
LVL 1

Author Comment

by:Lara_tin2
ID: 13399827
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.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 750 total points
ID: 13400512
> Resize image is a common operation when use image so I think it must have a  java library

It does, the code you have is fine.
Here are some other examples:
http://www.javaalmanac.com/egs/java.awt/TransformImage.html

The error is caused because there is no windows environment running on the box.
0

Featured Post

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
Check out the latest tech news, community articles, and expert highlights in August's newsletter.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question