Search in sources :

Example 21 with FileImageOutputStream

use of javax.imageio.stream.FileImageOutputStream in project triplea by triplea-game.

the class ImageShrinker method runInternal.

private void runInternal(final String[] args) throws IOException {
    handleCommandLineArgs(args);
    JOptionPane.showMessageDialog(null, new JLabel("<html>" + "This is the ImageShrinker, it will create a smallMap.jpeg file for you. " + "<br>Put in your base map or relief map, and it will spit out a small scaled copy of it." + "<br>Please note that the quality of the image will be worse than if you use a real painting program." + "<br>So we suggest you instead shrink the image with paint.net or photoshop or gimp, etc, then clean it " + "up before saving." + "</html>"));
    final File mapFile = new FileOpen("Select The Large Image", mapFolderLocation, ".gif", ".png").getFile();
    if (mapFile == null || !mapFile.exists()) {
        throw new IllegalStateException(mapFile + "File does not exist");
    }
    if (mapFolderLocation == null) {
        mapFolderLocation = mapFile.getParentFile();
    }
    final String input = JOptionPane.showInputDialog(null, "Select scale");
    final float scale = Float.parseFloat(input);
    final Image baseImg = ImageIO.read(mapFile);
    final int thumbWidth = (int) (baseImg.getWidth(null) * scale);
    final int thumbHeight = (int) (baseImg.getHeight(null) * scale);
    // based on code from
    // http://www.geocities.com/marcoschmidt.geo/java-save-jpeg-thumbnail.html
    // draw original image to thumbnail image object and
    // scale it to the new size on-the-fly
    final BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
    final Graphics2D graphics2D = thumbImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(baseImg, 0, 0, thumbWidth, thumbHeight, null);
    // save thumbnail image to OUTFILE
    final File file = new File(new File(mapFile.getPath()).getParent() + File.separatorChar + "smallMap.jpeg");
    try (ImageOutputStream out = new FileImageOutputStream(file)) {
        final ImageWriter encoder = ImageIO.getImageWritersByFormatName("JPEG").next();
        final JPEGImageWriteParam param = new JPEGImageWriteParam(null);
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality((float) 1.0);
        encoder.setOutput(out);
        encoder.write(null, new IIOImage(thumbImage, null, null), param);
    }
    ToolLogger.info("Image successfully written to " + file.getPath());
}
Also used : FileOpen(tools.image.FileOpen) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) ImageWriter(javax.imageio.ImageWriter) JLabel(javax.swing.JLabel) IIOImage(javax.imageio.IIOImage) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) IIOImage(javax.imageio.IIOImage) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream)

Aggregations

FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)21 File (java.io.File)18 ImageWriter (javax.imageio.ImageWriter)14 IOException (java.io.IOException)13 IIOImage (javax.imageio.IIOImage)13 ImageWriteParam (javax.imageio.ImageWriteParam)10 BufferedImage (java.awt.image.BufferedImage)9 ImageOutputStream (javax.imageio.stream.ImageOutputStream)8 Rectangle (java.awt.Rectangle)4 Iterator (java.util.Iterator)3 ImageReader (javax.imageio.ImageReader)3 FileImageInputStream (javax.imageio.stream.FileImageInputStream)3 Test (org.junit.Test)3 Graphics2D (java.awt.Graphics2D)2 Robot (java.awt.Robot)2 BufferedInputStream (java.io.BufferedInputStream)2 URLConnection (java.net.URLConnection)2 DataFormatException (java.util.zip.DataFormatException)2 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)2 ImageWriterSpi (javax.imageio.spi.ImageWriterSpi)2