javment
javment

Reputation: 368

Java2d thumbnails. Can I get thumbnail from OS

I develop an application in which I want to display a grid with a list of images. For each image I create an instance of a class myImage. MyImage class, extends JCompoment and create an thumbnail and after draw it with overide thepaintCompoment(Graphics g).

All is ok, but in big size images I have a lot of delay to create the thumbnail.

Now I think to when I scan the folders for image(to create the list I said above to create an thumbanail of each image and save it to disc. For each image I will have a database record to save the image path and thumbnail path.So is this a good solution of the problem? Is there a way to get the thumbnails of the system creates for each image, in file manager. Or a more effective solution than I try.

Thank you!!

Upvotes: 2

Views: 652

Answers (3)

Avrom
Avrom

Reputation: 5027

I haven't used it for the creation of thumbnails, but you may also want to take a look at the ImageIO API.

ImageIO

Upvotes: 0

trashgod
trashgod

Reputation: 205885

One way to avoid OS dependance is to use getScaledInstance(), as shown in this example. See the cited articles for certain limitations. If it's taking too long, use a SwingWorker to do the load and scale in the background.

Upvotes: 1

Reverend Gonzo
Reverend Gonzo

Reputation: 40871

Your best bet is to use something like imagemagick to convert the image and create the thumbnail. There's a project called JMagick which provides JNI hooks into Imagemagick, but running a process work too.

Imagemagick is heavily optimized C code for manipulating images. It will also be able to handle images that Java won't and with much less memory usage.

I work for a website where we let users upload art and create thumbnails on the fly, and it absolutely needs to be fast, so that's what we use.

The following is Groovy code, but it can modified to Java code pretty easily:

public boolean createThumbnail(InputStream input, OutputStream output){        

    def cmd = "convert -colorspace RGB -auto-orient -thumbnail 125x125 -[0] jpg:-"

    Process p = cmd.execute()
    p.consumeProcessErrorStream(System.out)
    p.consumeProcessOutputStream(output)
    p.out << input
    p.out.close()

    p.waitForOrKill(8000)
    return p.exitValue()==0
}

This creates a thumbnail using pipes without actually writing any data to disk. The outputStream can be to a file if you wanted to immediately write it as well.

Upvotes: 2

Related Questions