IOS图片生成工具(@1X,@2X,@3X)

IOS图片生成工具(@1X,@2X,@3X)

在进行IOS开发过程中,经常需要为了适配不同分辨率的设备而准备不同尺寸的图片。由于本人长期从事后台研发工作,对前端及UI常用工具(比如PS)并不熟悉,于是开发了下面这个工具对一个大尺寸的图片进行裁剪,生成1倍、2倍、3倍尺寸的图片,方便进行IOS开发。

工具介绍

该工具接受两个参数,分别为源图片文件或目录和目标图片存放文件夹。源图片为分辨率高于所有裁剪结果图片的分辨率(@1X,@2X,@3X)。

裁剪结果图片的尺寸写在ResizeImage类开头处,可以根据需要进行修改。

工具代码

package tools;

import common.Base;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Arrays;

/**
 * Genarate @1X, @2X, @3X pictures.
 *
 * @author zjm
 */
public class ResizeImage extends Base {
    private static final Logger LOGGER = LoggerFactory.getLogger(ResizeImage.class);

    /**
     * {width, height}
     */
    //    private static final int[] SIZE_3X = new int[] { 300, 402 };
    //    private static final int[] SIZE_2X = new int[] { 200, 268 };
    //    private static final int[] SIZE_1X = new int[] { 100, 134 };
    private static final int[] SIZE_3X = new int[] { 300, 300 };
    private static final int[] SIZE_2X = new int[] { 200, 200 };
    private static final int[] SIZE_1X = new int[] { 100, 100 };

    private String srcFileOrDirctory;
    private String destDirectory;

    public ResizeImage() {

    }

    public ResizeImage(String srcFileOrDirctory, String destFileOrDirectory) {
        this.srcFileOrDirctory = srcFileOrDirctory;
        this.destDirectory = destFileOrDirectory;
    }

    public String getSrcFileOrDirctory() {
        return srcFileOrDirctory;
    }

    public void setSrcFileOrDirctory(String srcFileOrDirctory) {
        this.srcFileOrDirctory = srcFileOrDirctory;
    }

    public String getDestDirectory() {
        return destDirectory;
    }

    public void setDestDirectory(String destDirectory) {
        this.destDirectory = destDirectory;
    }

    public void resizeImage() throws Exception {
        LOGGER.debug("Resize image parameters: src - {}, dest - {}", srcFileOrDirctory, destDirectory);

        File src = new File(srcFileOrDirctory);

        File dest = new File(destDirectory);
        if (dest.exists() && dest.isFile()) {
            throw new IllegalArgumentException("Not a directory: " + dest);
        }
        dest.mkdirs();

        if (src.exists() && src.isDirectory()) {
            File[] images = src.listFiles();
            for (File image : images) {
                resizeImage(srcFileOrDirctory, destDirectory);
            }
        } else if (src.exists() && src.isFile()) {
            resizeImage(srcFileOrDirctory, destDirectory);
        } else {
            throw new IllegalArgumentException("No such file or directory: " + src);
        }
    }

    private void resizeImage(String src, String dest) throws Exception {
        File destImage;

        destImage = new File(dest, getTypePrefix(src) + "@3X." + getTypeAppendix(src));
        resizeImage(src, destImage.getAbsolutePath(), SIZE_3X);

        destImage = new File(dest, getTypePrefix(src) + "@2X." + getTypeAppendix(src));
        resizeImage(src, destImage.getAbsolutePath(), SIZE_2X);

        destImage = new File(dest, getTypePrefix(src) + "@1X." + getTypeAppendix(src));
        resizeImage(src, destImage.getAbsolutePath(), SIZE_1X);
    }

    private void resizeImage(String src, String dest, int[] size) throws Exception {
        LOGGER.debug("Resizing image {} to {} in size {} ...", src, dest, Arrays.toString(size));

        BufferedImage srcImage = ImageIO.read(new File(src));

        BufferedImage destImage = new BufferedImage(size[0], size[1], srcImage.getType());
        Graphics2D destGraph = destImage.createGraphics();
        destGraph.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        destGraph.drawImage(srcImage, 0, 0, size[0], size[1], 0, 0, srcImage.getWidth(), srcImage.getHeight(), null);
        destGraph.dispose();

        ImageIO.write(destImage, getTypeAppendix(src), new File(dest));

        LOGGER.debug("A new image {} is created.", dest);
    }

    private String getTypeAppendix(String image) {
        int lastIndex = image.lastIndexOf(".");
        return image.substring(lastIndex + 1);
    }

    private String getTypePrefix(String image) {
        int lastSlashIndex = image.lastIndexOf("/");
        int lastDotIndex = image.lastIndexOf(".");
        return image.substring(lastSlashIndex + 1, lastDotIndex);
    }

    public static void main(String[] args) throws Exception {
        if (args.length != 2) {
            LOGGER.error("Invalid arguments. Usage: {}  ",
                    ResizeImage.class.getSimpleName());
            System.exit(1);
        }
        new ResizeImage(args[0], args[1]).resizeImage();
    }
}

你可能感兴趣的:(IOS开发)