Changeset 17363 in josm for trunk


Ignore:
Timestamp:
2020-11-26T22:24:24+01:00 (3 months ago)
Author:
simon04
Message:

see #20141 - Extract BufferedImageCacheEntry.pngEncoded

Location:
trunk/src/org/openstreetmap/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/cache/BufferedImageCacheEntry.java

    r11331 r17363  
    44import java.awt.image.BufferedImage;
    55import java.io.ByteArrayInputStream;
     6import java.io.ByteArrayOutputStream;
    67import java.io.IOException;
     8import java.io.UncheckedIOException;
    79
    810import javax.imageio.ImageIO;
     
    2830    public BufferedImageCacheEntry(byte[] content) {
    2931        super(content);
     32    }
     33
     34    /**
     35     * Encodes the given image as PNG and returns a cache entry
     36     * @param img the image
     37     * @return a cache entry for the PNG encoded image
     38     * @throws UncheckedIOException if an I/O error occurs
     39     */
     40    public static BufferedImageCacheEntry pngEncoded(BufferedImage img) {
     41        try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
     42            ImageIO.write(img, "png", output);
     43            return new BufferedImageCacheEntry(output.toByteArray());
     44        } catch (IOException e) {
     45            throw new UncheckedIOException(e);
     46        }
    3047    }
    3148
  • trunk/src/org/openstreetmap/josm/gui/layer/geoimage/ThumbsLoader.java

    r16398 r17363  
    99import java.awt.geom.AffineTransform;
    1010import java.awt.image.BufferedImage;
    11 import java.io.ByteArrayOutputStream;
    1211import java.io.File;
    1312import java.io.IOException;
     13import java.io.UncheckedIOException;
    1414import java.util.ArrayList;
    1515import java.util.Collection;
    16 
    17 import javax.imageio.ImageIO;
    1816
    1917import org.apache.commons.jcs3.access.behavior.ICacheAccess;
     
    165163
    166164        if (!cacheOff && cache != null) {
    167             try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
    168                 ImageIO.write(scaledBI, "png", output);
    169                 cache.put(cacheIdent, new BufferedImageCacheEntry(output.toByteArray()));
    170             } catch (IOException e) {
     165            try {
     166                cache.put(cacheIdent, BufferedImageCacheEntry.pngEncoded(scaledBI));
     167            } catch (UncheckedIOException e) {
    171168                Logging.warn("Failed to save geoimage thumb to cache");
    172169                Logging.warn(e);
Note: See TracChangeset for help on using the changeset viewer.