001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.gui.io;
025    
026    import java.awt.Component;
027    import java.awt.Graphics2D;
028    import java.awt.image.BufferedImage;
029    import java.io.File;
030    import java.util.Iterator;
031    import java.util.Locale;
032    import java.util.logging.Level;
033    import java.util.logging.Logger;
034    
035    import javax.imageio.IIOImage;
036    import javax.imageio.ImageIO;
037    import javax.imageio.ImageWriteParam;
038    import javax.imageio.ImageWriter;
039    import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
040    import javax.imageio.stream.ImageOutputStream;
041    
042    import org.ujmp.core.util.io.FileSelector;
043    
044    public abstract class ExportJPEG {
045    
046            private static final Logger logger = Logger.getLogger(ExportJPEG.class
047                            .getName());
048    
049            public static final File selectFile() {
050                    return selectFile(null);
051            }
052    
053            public static final File selectFile(Component c) {
054                    return FileSelector.selectFile(c, "JPEG Files", ".jpg");
055            }
056    
057            public static final void save(String filename, Component c) {
058                    save(new File(filename), c, c.getWidth());
059            }
060    
061            public static final void save(String filename, Component c, int width) {
062                    save(new File(filename), c, width);
063            }
064    
065            public static final void save(File file, Component c) {
066                    save(file, c, c.getWidth());
067            }
068    
069            public static final void save(File file, Component c, int width) {
070                    if (file == null) {
071                            logger.log(Level.WARNING, "no file selected");
072                            return;
073                    }
074                    if (c == null) {
075                            logger.log(Level.WARNING, "no component provided");
076                            return;
077                    }
078                    double factor = width / c.getWidth();
079                    BufferedImage myImage = new BufferedImage(
080                                    (int) (c.getWidth() * factor), (int) (c.getHeight() * factor),
081                                    BufferedImage.TYPE_INT_RGB);
082                    Graphics2D g2 = myImage.createGraphics();
083                    g2.scale(factor, factor);
084                    c.paint(g2);
085                    try {
086                            ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg")
087                                            .next();
088    
089                            ImageOutputStream ios = ImageIO.createImageOutputStream(file);
090                            writer.setOutput(ios);
091    
092                            ImageWriteParam iwparam = new JPEGImageWriteParam(Locale
093                                            .getDefault());
094                            iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
095                            iwparam.setCompressionQuality(1.0f);
096    
097                            writer.write(null, new IIOImage(myImage, null, null), iwparam);
098    
099                            ios.flush();
100                            writer.dispose();
101                            ios.close();
102    
103                    } catch (Exception e) {
104                            logger.log(Level.WARNING, "could not save jpg image", e);
105                    }
106            }
107    }