com.shending.support.CompressPic.java Source code

Java tutorial

Introduction

Here is the source code for com.shending.support.CompressPic.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.shending.support;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.io.FileInputStream;
import java.util.Iterator;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author Administrator
 */
public class CompressPic {

    /**
     * *****************************************************************************
     *  javajpg?bmp?png?gif??? 
     * compressPic(,??,??,????,??,??,?(true))
     */
    private File file = null; // 
    private String inputDir; // 
    private String outputDir; // 
    private String inputFileName; // ??
    private String outputFileName; // ??
    private int outputWidth = 100; // 
    private int outputHeight = 100; // 
    private boolean proportion = true; // ?()

    public void setInputDir(String inputDir) {
        this.inputDir = inputDir;
    }

    public void setOutputDir(String outputDir) {
        this.outputDir = outputDir;
    }

    public void setInputFileName(String inputFileName) {
        this.inputFileName = inputFileName;
    }

    public void setOutputFileName(String outputFileName) {
        this.outputFileName = outputFileName;
    }

    public void setOutputWidth(int outputWidth) {
        this.outputWidth = outputWidth;
    }

    public void setOutputHeight(int outputHeight) {
        this.outputHeight = outputHeight;
    }

    public void setWidthAndHeight(int width, int height) {
        this.outputWidth = width;
        this.outputHeight = height;
    }

    /*
     * ? ? String path 
     */
    public long getPicSize(String path) {
        file = new File(path);
        return file.length();
    }

    // ?
    public String compressPic() {
        try {
            // ?
            file = new File(inputDir);
            //System.out.println(inputDir + inputFileName);
            if (!file.exists()) {
                //throw new Exception("?");
            }
            Image img = ImageIO.read(file);
            // ??
            if (img.getWidth(null) == -1) {
                System.out.println(" can't read,retry!" + "<BR>");
                return "no";
            } else {
                int newWidth;
                int newHeight;
                // ?
                if (this.proportion == true) {
                    // ?
                    double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1;
                    double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1;
                    // ?
                    double rate = rate1 > rate2 ? rate1 : rate2;
                    newWidth = (int) (((double) img.getWidth(null)) / rate);
                    newHeight = (int) (((double) img.getHeight(null)) / rate);
                } else {
                    newWidth = outputWidth; // 
                    newHeight = outputHeight; // 
                }
                BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);

                /*
                 * Image.SCALE_SMOOTH  ?  ?? 
                 */
                tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0,
                        null);
                File f = new File(outputDir);
                if (!f.exists()) {
                    f.mkdirs();
                }
                FileOutputStream out = new FileOutputStream(outputDir + outputFileName);
                // JPEGImageEncoder??
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                encoder.encode(tag);
                out.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return "ok";
    }

    public String compressPic(String outputDir, String inputFileName, String outputFileName) {
        // 
        this.outputDir = outputDir;
        // ??
        this.inputFileName = inputFileName;
        // ??
        this.outputFileName = outputFileName;
        return compressPic();
    }

    public String compressPic(String inputDir, String outputDir, String inputFileName, String outputFileName,
            int width, int height, boolean gp) {
        // 
        this.inputDir = inputDir;
        // 
        this.outputDir = outputDir;
        // ??
        this.inputFileName = inputFileName;
        // ??
        this.outputFileName = outputFileName;
        // 
        setWidthAndHeight(width, height);
        // ? 
        this.proportion = gp;
        return compressPic();
    }

    /**
     * ?
     *
     * @param srcFile
     * @param dstFile
     * @param widthRange
     * @param heightRange
     */
    public static void cutSquare(String srcFile, String dstFile, int widthRange, int heightRange, int width,
            int height) {
        int x = 0;
        int y = 0;
        try {
            ImageInputStream iis = ImageIO.createImageInputStream(new File(srcFile));
            Iterator<ImageReader> iterator = ImageIO.getImageReaders(iis);
            ImageReader reader = (ImageReader) iterator.next();
            reader.setInput(iis, true);
            ImageReadParam param = reader.getDefaultReadParam();
            int oldWidth = reader.getWidth(0);
            int oldHeight = reader.getHeight(0);
            int newWidth, newHeight;
            if (width <= oldWidth && height <= oldHeight) {
                newWidth = oldHeight * widthRange / heightRange;
                if (newWidth < oldWidth) {
                    newHeight = oldHeight;
                    x = (oldWidth - newWidth) / 2;
                } else {
                    newWidth = oldWidth;
                    newHeight = oldWidth * heightRange / widthRange;
                    y = (oldHeight - newHeight) / 2;
                }
                Rectangle rectangle = new Rectangle(x, y, newWidth, newHeight);
                param.setSourceRegion(rectangle);
                BufferedImage bi = reader.read(0, param);
                BufferedImage tag = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_RGB);
                tag.getGraphics().drawImage(bi.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
                File file = new File(dstFile);
                ImageIO.write(tag, reader.getFormatName(), file);
            } else {
                BufferedImage bi = reader.read(0, param);
                BufferedImage tag = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_RGB);
                Graphics2D g2d = tag.createGraphics();
                g2d.setColor(Color.WHITE);
                g2d.fillRect(0, 0, tag.getWidth(), tag.getHeight());
                g2d.drawImage(bi.getScaledInstance(bi.getWidth(), bi.getHeight(), Image.SCALE_SMOOTH),
                        (width - bi.getWidth()) / 2, (height - bi.getHeight()) / 2, null);
                g2d.dispose();
                File file = new File(dstFile);
                ImageIO.write(tag, reader.getFormatName(), file);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void cut(String srcFile, String dstFile, int widthRange, int heightRange) {
        int x = 0;
        int y = 0;
        try {
            ImageInputStream iis = ImageIO.createImageInputStream(new File(srcFile));
            Iterator<ImageReader> iterator = ImageIO.getImageReaders(iis);
            ImageReader reader = (ImageReader) iterator.next();
            reader.setInput(iis, true);
            ImageReadParam param = reader.getDefaultReadParam();
            int oldWidth = reader.getWidth(0);
            int oldHeight = reader.getHeight(0);
            int newWidth, newHeight;
            newWidth = oldHeight * widthRange / heightRange;
            if (newWidth < oldWidth) {
                newHeight = oldHeight;
                x = (oldWidth - newWidth) / 2;
            } else {
                newWidth = oldWidth;
                newHeight = oldWidth * heightRange / widthRange;
                y = (oldHeight - newHeight) / 2;
            }
            Rectangle rectangle = new Rectangle(x, y, newWidth, newHeight);
            param.setSourceRegion(rectangle);
            BufferedImage bi = reader.read(0, param);
            File file = new File(dstFile);
            ImageIO.write(bi, reader.getFormatName(), file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String args[]) throws IOException {
        String s = "f:\\2.png";
        String s2 = "f:\\234.jpg";
    }
}