Package janesimageresizer

Source Code of janesimageresizer.JanesImageResizerApp

/*
* JanesImageResizerApp.java
*/

package janesimageresizer;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;
/**
* The main class of the application.
*/
public class JanesImageResizerApp extends SingleFrameApplication {

    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
        show(new JanesImageResizerView(this));
    }
   
    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }
    /**
     * @param srcImage
     * @param destWidth
     * @param destHeight
     * @param destFile
     * @throws IOException
     */
    public static boolean ResizeImages(String srcImage,double percentReduce,String destFile,String imageFormat)
            throws IOException
    {
        try{
           
            BufferedImage src = ImageIO.read(new File(srcImage));
            int imgHeight = src.getHeight();
            int imgWidth = src.getWidth();
            double reduceTo = 100 - percentReduce;
            int newWidth =  (int)(reduceTo * imgWidth)/100;
            int newHeight = (int)(reduceTo*imgHeight)/100;
           
            BufferedImage destImage = new BufferedImage(newWidth,newHeight,BufferedImage.TYPE_INT_RGB);
            Graphics2D g = destImage.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                               RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            AffineTransform at = AffineTransform.getScaleInstance((double)newWidth/src.getWidth(), (double)newHeight/src.getHeight());
            g.drawRenderedImage(src, at);
            ImageIO.write(destImage,imageFormat, new File(destFile));
            return true;
        }catch(Exception e)
        {
            return false;
        }
    }
    /**
     * @param dir
     * @param targetFiles
     * @return
     * @throws IOException
     */
    public static ArrayList<File> getFilesInDir(String dir,ArrayList<File> targetFiles)throws IOException
    {
        File sourceFiles = new File(dir);
        for (File file:sourceFiles.listFiles())
        {
            if(file.isDirectory())
            {
                getFilesInDir(file.getAbsolutePath(),targetFiles);
            }
            else
            {
                targetFiles.add(file);
            }
        }      
        return targetFiles;
    }
    /**
     * A convenient static getter for the application instance.
     * @return the instance of JanesImageResizerApp
     */
    public static JanesImageResizerApp getApplication() {
        return Application.getInstance(JanesImageResizerApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        launch(JanesImageResizerApp.class, args);
    }
}
TOP

Related Classes of janesimageresizer.JanesImageResizerApp

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.