my dog learned polymorphism*
The moose likes Java in General and the fly likes Get Image Dimensions and Delete as Needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Get Image Dimensions and Delete as Needed" Watch "Get Image Dimensions and Delete as Needed" New topic
Author

Get Image Dimensions and Delete as Needed

Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3700
    
    5

I'm trying to write a function that takes a file as an image (JPG, GIG, PNG, etc) and records the dimensions. If the file is not an image, or the dimensions cannot be determined (or otherwise 'bad'), the file is deleted.

One method I found for getting the image dimensions immediately is discussed on Example Depot



Let's say after making this call I want to delete the file for some reason. The call [new File.delete("image.gif")] fails [returns false and does not delete the file] because the above code locks the file.

First off, is there a way to release the lock the above code puts on my file so that it can be safely deleted? Second, I'd take any recommendations for non-Swing libraries that would replace the above code. I'm not fond of using Swing code in what is definitely a non-swing application (also why I didn't post to the Swing forum).


My Blog: Down Home Country Coding with Scott Selikoff
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You could use the ImageIcon constructor which takes a byte[] instead of a file name; just pass the contents of the file.


[Jess in Action][AskingGoodQuestions]
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3700
    
    5

Thanks Bear Ernest, that worked!

Side note: I did call image.flush() after reading the dimensions, but the file lock stayed.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Bear?
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3700
    
    5

Ernest Friedman-Hill wrote:Bear?


Oops! Guess I had Bear on the mind ;) Thanks Ernest!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41034
    
  43
Or you could use the ImageInfo class, which knows about a few more file formats.


Ping & DNS - my free Android networking tools app
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3700
    
    5

Is that the one from this library?

http://commons.apache.org/sanselan/
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41034
    
  43
I was thinking of http://test.javaranch.com/ulf/ImageInfo.java (which doesn't seem to be available on the net any more on whichever site I found it originally), but both seem to cover a similar list of formats.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Scott Selikoff wrote:I'm trying to write a function that takes a file as an image (JPG, GIG, PNG, etc) and records the dimensions. If the file is not an image, or the dimensions cannot be determined (or otherwise 'bad'), the file is deleted.

<snip/>

Let's say after making this call I want to delete the file for some reason. The call [new File.delete("image.gif")] fails [returns false and does not delete the file] because the above code locks the file.

First off, is there a way to release the lock the above code puts on my file so that it can be safely deleted? Second, I'd take any recommendations for non-Swing libraries that would replace the above code. I'm not fond of using Swing code in what is definitely a non-swing application (also why I didn't post to the Swing forum).

The ImageIcon constructor that takes a String argument loads the image using Toolkit, and that may be where the file is getting locked (in the native method).

There's no need for any Swing code. Load the image using ImageIO#read(...) which blocks until the file is completely read.


luck, db
There are no new questions, but there may be new answers.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3700
    
    5

Thanks Ulf.

While I'm at it, anyone have a good method for reading duration/dimensions of an FLV movie? The only solution I've seen on the web is to read the first ~400 bytes and do a text search for the words "duration", "width", "height". It seems awfully buggy, but it actually works. Hoping there was something more elegant though.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Get Image Dimensions and Delete as Needed
 
Similar Threads
How to make sense of my binary data
Serlvet to display image - how to prevent client from "Save As Image" ?
saveing Graphics to files
gif image shows only in black and white
Image Handling in Java