com.github.houbin217jz.thumbnail.Thumbnail.java Source code

Java tutorial

Introduction

Here is the source code for com.github.houbin217jz.thumbnail.Thumbnail.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.houbin217jz.thumbnail;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;

/**
 * ?Sample
 * ??Java 7 ?
 * Jar????????
 * 
 * ?Library: 
 *   Thumbnailator: https://code.google.com/p/thumbnailator
 *   Common CLI: http://commons.apache.org/proper/commons-cli
 * @author Bin Hou
 *
 */
public class Thumbnail {

    public static void main(String[] args) {

        Options options = new Options();
        options.addOption("s", "src", true,
                "????????????");
        options.addOption("d", "dst", true, "");
        options.addOption("r", "ratio", true, "/??, 30%???0.3????????");
        options.addOption("w", "width", true, "(px)");
        options.addOption("h", "height", true, "?(px)");
        options.addOption("R", "recursive", false, "???????");

        HelpFormatter formatter = new HelpFormatter();
        String formatstr = "java -jar thumbnail.jar " + "[-s/--src <path>] " + "[-d/--dst <path>] "
                + "[-r/--ratio double] " + "[-w/--width integer] " + "[-h/--height integer] " + "[-R/--recursive] ";

        CommandLineParser parser = new PosixParser();
        CommandLine cmd = null;
        try {
            cmd = parser.parse(options, args);
        } catch (ParseException e1) {
            formatter.printHelp(formatstr, options);
            return;
        }

        final Path srcDir, dstDir;
        final Integer width, height;
        final Double ratio;

        //
        if (cmd.hasOption("s")) {
            srcDir = Paths.get(cmd.getOptionValue("s")).toAbsolutePath();
        } else {
            srcDir = Paths.get("").toAbsolutePath(); //??
        }

        //
        if (cmd.hasOption("d")) {
            dstDir = Paths.get(cmd.getOptionValue("d")).toAbsolutePath();
        } else {
            formatter.printHelp(formatstr, options);
            return;
        }

        if (!Files.exists(srcDir, LinkOption.NOFOLLOW_LINKS)
                || !Files.isDirectory(srcDir, LinkOption.NOFOLLOW_LINKS)) {
            System.out.println("[" + srcDir.toAbsolutePath() + "]??????");
            return;
        }

        if (Files.exists(dstDir, LinkOption.NOFOLLOW_LINKS)) {
            if (!Files.isDirectory(dstDir, LinkOption.NOFOLLOW_LINKS)) {
                //????????
                System.out.println("????????");
                return;
            }
        } else {
            //????????
            try {
                Files.createDirectories(dstDir);
            } catch (IOException e) {
                e.printStackTrace();
                return;
            }
        }

        //??
        if (cmd.hasOption("w") && cmd.hasOption("h")) {
            try {
                width = Integer.valueOf(cmd.getOptionValue("width"));
                height = Integer.valueOf(cmd.getOptionValue("height"));
            } catch (NumberFormatException e) {
                System.out.println("??????");
                return;
            }
        } else {
            width = null;
            height = null;
        }

        //?
        if (cmd.hasOption("r")) {
            try {
                ratio = Double.valueOf(cmd.getOptionValue("r"));
            } catch (NumberFormatException e) {
                System.out.println("?????");
                return;
            }
        } else {
            ratio = null;
        }

        if (width != null && ratio != null) {
            System.out.println("??????????????");
            return;
        }

        if (width == null && ratio == null) {
            System.out.println("????????????");
            return;
        }

        //
        int maxDepth = 1;
        if (cmd.hasOption("R")) {
            maxDepth = Integer.MAX_VALUE;
        }

        try {
            //Java 7 ??@see http://docs.oracle.com/javase/jp/7/api/java/nio/file/Files.html
            Files.walkFileTree(srcDir, EnumSet.of(FileVisitOption.FOLLOW_LINKS), maxDepth,
                    new SimpleFileVisitor<Path>() {
                        @Override
                        public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes)
                                throws IOException {

                            //???&???
                            String filename = path.getFileName().toString().toLowerCase();

                            if (filename.endsWith(".jpg") || filename.endsWith(".jpeg")) {
                                //Jpeg??

                                /*
                                 * relative??:
                                 * rootPath: /a/b/c/d
                                 * filePath: /a/b/c/d/e/f.jpg
                                 * rootPath.relativize(filePath) = e/f.jpg
                                 */

                                /*
                                 * resolve??
                                 * rootPath: /a/b/c/output
                                 * relativePath: e/f.jpg
                                 * rootPath.resolve(relativePath) = /a/b/c/output/e/f.jpg
                                 */

                                Path dst = dstDir.resolve(srcDir.relativize(path));

                                if (!Files.exists(dst.getParent(), LinkOption.NOFOLLOW_LINKS)) {
                                    Files.createDirectories(dst.getParent());
                                }
                                doResize(path.toFile(), dst.toFile(), width, height, ratio);
                            }
                            return FileVisitResult.CONTINUE;
                        }
                    });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void doResize(File src, File dst, Integer width, Integer height, Double ratio) {
        try {
            Builder<File> builder = Thumbnails.of(src);
            if ((width == null || height == null) && ratio != null) {
                builder.scale(ratio).toFile(dst);
            }
            if ((width != null && height != null) && ratio == null) {
                builder.size(width, height).toFile(dst);
            }

        } catch (IOException e) {
            System.out.println("Error");
            System.out.println("src:" + src.toString());
            System.out.println("dst:" + dst.toString());
        }
    }
}