001    /*
002     * Created on May 1, 2009
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005     * the License. You may obtain a copy of the License at
006     *
007     * http://www.apache.org/licenses/LICENSE-2.0
008     *
009     * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010     * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011     * specific language governing permissions and limitations under the License.
012     *
013     * Copyright @2009-2010 the original author or authors.
014     */
015    package org.fest.swing.image;
016    
017    import static org.fest.swing.image.ImageFileExtensions.PNG;
018    import static org.fest.util.Files.newFile;
019    
020    import java.awt.image.BufferedImage;
021    import java.io.IOException;
022    
023    import javax.imageio.ImageIO;
024    
025    /**
026     * Understands how to write an image as a file in the file system.
027     *
028     * @author Alex Ruiz
029     */
030    public class ImageFileWriter {
031    
032      /**
033       * Writes an image as a PNG file to the file system.
034       * If there is already a <code>File</code> present, its contents are discarded.
035       * @param image a <code>BufferedImage</code> to be written.
036       * @param filePath the path of the image file to create.
037       * @return <code>false</code> if the image could not be saved.
038       * @exception IOException if an error occurs during writing.
039       */
040      public boolean writeAsPng(BufferedImage image, String filePath) throws IOException {
041        return ImageIO.write(image, PNG, newFile(filePath));
042      }
043    }