Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > de.comp.lang.java > #12903

Re: Java ImageIO; mal lahm, mal flott

From Thomas Richter <thor@math.tu-berlin.de>
Newsgroups de.comp.lang.java
Subject Re: Java ImageIO; mal lahm, mal flott
Date 2016-01-21 10:40 +0100
Organization University of Stuttgart, FRG
Message-ID <n7q92m$8as$1@news2.informatik.uni-stuttgart.de> (permalink)
References <1452884893.626603@alpaka.in-berlin.de>

Show all headers | View raw


On 15.01.2016 20:08, Peter wrote:
> Hallo,

> "Here is something very simple and handy.
> 
> BufferedImage bimg = ImageIO.read(new File(filename));
> int width          = bimg.getWidth();
> int height         = bimg.getHeight();"
> 
> Funktioniert tatsächlich; hat nur einen schweren Nachteil:
> Diese "Löschung" dauert eeeeeehhhhhhhhwig!
> Ca. 10 Sekunden für ein Image!
> 
> Ich habe nämlich nicht nur viele Images, sie sind auch recht groß (8k x
> 8k); insgesamt ca. 50GB.
> 
> Wiedermal eine OO-Sternstunde: Es wird ein 10 Tonnen schweres Objekt
> instanziiert, nur um es dann nach seine Größe zu fragen :-(
> 
> Aber wenn man bei obiger Website etwas nach unten scrollt, findet man
> noch einen Vorschlag:
> 
> /**
>  * Gets image dimensions for given file
>  * @param imgFile image file
>  * @return dimensions of image
>  * @throws IOException if the file is not a known image
>  */
> public static Dimension getImageDimension(File imgFile) throws IOException {
>   int pos = imgFile.getName().lastIndexOf(".");
>   if (pos == -1)
>     throw new IOException("No extension for file: " +
> imgFile.getAbsolutePath());
>   String suffix = imgFile.getName().substring(pos + 1);
>   Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
>   if (iter.hasNext()) {
>     ImageReader reader = iter.next();
>     try {
>       ImageInputStream stream = new FileImageInputStream(imgFile);
>       reader.setInput(stream);
>       int width = reader.getWidth(reader.getMinIndex());
>       int height = reader.getHeight(reader.getMinIndex());
>       return new Dimension(width, height);
>     } catch (IOException e) {
>       log.warn("Error reading: " + imgFile.getAbsolutePath(), e);
>     } finally {
>       reader.dispose();
>     }
>   }
> 
>   throw new IOException("Not a known image file: " +
> imgFile.getAbsolutePath());
> }
> 
> Und siehe da, alles flutscht quasi in Nullzeit.

Der Unterschied zwischen beiden Lösungen ist, dass die erstere Lösung
das Bild vollständig decodiert, während die zweite nur den Header
abfragt. Kann man, sofern JPEG gesetzt ist, auch problemlos "von Hand"
erledigen. Die Bildgröße steht im SOF-Marker, der im Binärstrom durch
die Bytefolge 0xff 0xc0 gekennzeichnet ist. Einfach da rausziehen und
gut ist...

Grüße,
	Thomas

Back to de.comp.lang.java | Previous | NextPrevious in thread | Find similar


Thread

Java ImageIO; mal lahm, mal flott Peter <peter@localhost.com> - 2016-01-15 20:08 +0100
  Re: Java ImageIO; mal lahm, mal flott Thomas Richter <thor@math.tu-berlin.de> - 2016-01-21 10:40 +0100

csiph-web