File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get Image Dimensions and Delete as Needed

 
Scott Selikoff
author
Saloon Keeper
Posts: 3890
15
Eclipse IDE Flex Google Web Toolkit
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use the ImageIcon constructor which takes a byte[] instead of a file name; just pass the contents of the file.
 
Scott Selikoff
author
Saloon Keeper
Posts: 3890
15
Eclipse IDE Flex Google Web Toolkit
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear?
 
Scott Selikoff
author
Saloon Keeper
Posts: 3890
15
Eclipse IDE Flex Google Web Toolkit
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:Bear?


Oops! Guess I had Bear on the mind ;) Thanks Ernest!
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could use the ImageInfo class, which knows about a few more file formats.
 
Scott Selikoff
author
Saloon Keeper
Posts: 3890
15
Eclipse IDE Flex Google Web Toolkit
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that the one from this library?

http://commons.apache.org/sanselan/
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scott Selikoff
author
Saloon Keeper
Posts: 3890
15
Eclipse IDE Flex Google Web Toolkit
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic