com.ms.commons.file.impl.FileServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.ms.commons.file.impl.FileServiceImpl.java

Source

/*
 * Copyright 2011-2016 ZXC.com All right reserved. This software is the confidential and proprietary information of
 * ZXC.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with ZXC.com.
 */
package com.ms.commons.file.impl;

import java.io.*;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IMOperation;
import org.slf4j.Logger;

import com.ms.commons.file.interfaces.FileService;
import com.ms.commons.file.service.ImageUtil;
import com.ms.commons.log.LoggerFactoryWrapper;

/**
 * @author zxc Apr 12, 2013 1:21:34 PM
 */
public class FileServiceImpl implements FileService {

    private static Logger logger = LoggerFactoryWrapper.getLogger(FileServiceImpl.class);

    /**
     * API??
     * 
     * @param url ?url
     * @param widths ??savePaths?????
     * @param savePaths ??widths
     */
    public boolean reduceAllPicture(String url, Double quality, Integer[] widths, String[] savePaths) {
        if (url == null || url.trim().length() == 0 || widths == null || savePaths == null || widths.length == 0
                || widths.length != savePaths.length) {
            return false;
        }
        try {
            Map<String, Integer> imageBasicInfo = ImageUtil.getImageWH(url);
            int originalImageWidth = imageBasicInfo.get(ImageUtil.IMAGE_WIDTH);
            int originalImageHeight = imageBasicInfo.get(ImageUtil.IMAGE_HEIGHT);
            for (int i = 0, len = widths.length; i < len; i++) {
                double widthBo = (double) widths[i] / originalImageWidth;
                int height = (int) Math.ceil(widthBo * originalImageHeight);
                ImageUtil.scaleImage(url, widths[i], height, savePaths[i], quality);
            }
            return true;
        } catch (Exception e) {
            logger.error("", e);
            return false;
        }
    }

    /**
     * ??
     * 
     * @param url
     * @param quality
     * @param widths
     * @param heights
     * @param savePaths
     * @return
     */
    public boolean reduceAllPicture(String url, Double quality, int[] widths, int[] heights, String[] savePaths) {
        if (url == null || url.trim().length() == 0 || widths == null || widths.length == 0 || heights == null
                || heights.length == 0 || savePaths == null || savePaths.length == 0
                || widths.length != savePaths.length || heights.length != savePaths.length) {
            return false;
        }
        try {
            Map<String, Integer> imageBasicInfo = ImageUtil.getImageWH(url);
            int originalImageWidth = imageBasicInfo.get(ImageUtil.IMAGE_WIDTH);
            int originalImageHeight = imageBasicInfo.get(ImageUtil.IMAGE_HEIGHT);
            for (int i = 0, len = widths.length; i < len; i++) {
                int width = widths[i];
                int height = heights[i];
                if (width <= 0 && height <= 0) {
                    continue;
                }
                // ?
                if (width <= 0) {
                    double heightBo = (double) height / originalImageHeight;
                    if (heightBo > 1) {
                        width = originalImageWidth;
                        height = originalImageHeight;
                    } else {
                        width = (int) Math.ceil(heightBo * originalImageWidth);
                    }
                }
                // ?
                else {
                    double widthBo = (double) width / originalImageWidth;
                    if (widthBo > 1) {
                        width = originalImageWidth;
                        height = originalImageHeight;
                    } else {
                        height = (int) Math.ceil(widthBo * originalImageHeight);
                    }
                }
                ImageUtil.scaleImage(url, width, height, savePaths[i], quality);
            }
            return true;
        } catch (Exception e) {
            logger.error("", e);
            return false;
        }
    }

    /**
     * ??
     * 
     * @param inputStream
     * @return
     * @throws IOException
     */
    public void inputStreamToFile(InputStream inputStream, String savePath) throws Exception {
        if (inputStream == null) {
            return;
        }
        int index = savePath.lastIndexOf(File.separator);
        String dirString = savePath.substring(0, index);
        FileOutputStream out = null;
        try {
            File dir = new File(dirString);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            File outFile = new File(savePath);
            if (!outFile.exists()) {
                outFile.createNewFile();
            }
            out = new FileOutputStream(outFile);
            int c;
            byte buffer[] = new byte[1024];
            while ((c = inputStream.read(buffer)) != -1) {
                out.write(buffer, 0, c);
            }
        } catch (Exception e) {
            logger.error("?:" + savePath + ":", e);
            throw e;
        } finally {
            IOUtils.closeQuietly(out);
        }
    }

    /**
     * ??defaultWidth*defaultWidth
     * 
     * @param oldpath
     * @param newpath
     */
    public boolean cutImage(String oldpath, String newpath, Double quality, int defaultWidth) {
        if (oldpath == null || newpath == null || defaultWidth <= 0) {
            return false;
        }
        try {

            int x = 0, y = 0, width = 0, height = 0;
            Map<String, Integer> whInfo = ImageUtil.getImageWH(oldpath);
            // 
            int originalImageWidth = whInfo.get(ImageUtil.IMAGE_WIDTH);
            // 
            int originalImageHeight = whInfo.get(ImageUtil.IMAGE_HEIGHT);

            x = originalImageWidth / 2 - defaultWidth / 2;
            y = originalImageHeight / 2 - defaultWidth / 2;
            width = defaultWidth;
            height = defaultWidth;
            cutImage(x, y, width, height, oldpath, newpath, quality, defaultWidth);
            return true;
        } catch (Exception e) {
            logger.error("?", e);
            return false;
        }
    }

    /**
     * 
     * 
     * @param x ?? ()
     * @param y ?? ()
     * @param width ?
     * @param height ?
     * @param oldpath ?
     * @param newpath ??
     */
    public boolean cutImage(int x, int y, int width, int height, String oldpath, String newpath, Double quality,
            int defaultWidth) {
        if (oldpath == null || newpath == null || defaultWidth <= 0) {
            return false;
        }
        // ?????????
        if (width <= defaultWidth) {
            return ImageUtil.cutImage(oldpath, newpath, quality, x, y, width, height);
        } else {
            return ImageUtil.cutAndScaleImage(oldpath, newpath, quality, x, y, width, height, defaultWidth,
                    defaultWidth);
        }
    }

    /**
     * 
     * 
     * @param srcFile ?
     * @param imgWidth ?
     * @param imgHeight ?
     * @param expectedWidth 
     * @param expectedHeight 
     * @return
     */
    public String[] splitImage(File srcFile, Double quality, Integer expectedWidth, Integer expectedHeight) {
        try {
            String srcFileName = srcFile.getPath();
            Map<String, Integer> whInfo = ImageUtil.getImageWH(srcFileName);
            // 
            int imgWidth = whInfo.get(ImageUtil.IMAGE_WIDTH);
            // 
            int imgHeight = whInfo.get(ImageUtil.IMAGE_HEIGHT);

            // 
            int col = 1;
            if (expectedWidth != null) {
                col = imgWidth / expectedWidth;
                // 
                col = (col == 0 ? 1 : col);
                col = (imgWidth % expectedWidth) == 0 ? col : col + 1;
            }

            // 
            int row = 1;
            if (expectedHeight != null) {
                row = (imgHeight / expectedHeight);
                // 
                row = (row == 0 ? 1 : row);
                row = (imgHeight % expectedHeight) == 0 ? row : row + 1;
            }

            // 
            ConvertCmd convert = new ConvertCmd();
            IMOperation op = new IMOperation();
            if (quality != null && quality > 0) {
                op.quality(quality);
            }
            op.addImage(srcFile.getPath());
            op.crop(null, null, null, null, col + "x" + row + "@");

            int index = srcFileName.lastIndexOf('.');
            String prefix = srcFileName.substring(0, index);
            String suffix = srcFileName.substring(index);
            String destFileName = prefix + "_%d" + suffix;
            op.addImage(destFileName);
            convert.run(op);
            String[] files = new String[row];
            for (int i = 0; i < row; i++) {
                files[i] = prefix + "_" + i + suffix;
            }
            return files;
        } catch (Exception e) {
            logger.error("", e);
            return null;
        }
    }

    /**
     * 
     */
    @Override
    public boolean scaleImage(String sourceFilePath, Double quality, int width, int height, String destFilePath) {
        return ImageUtil.scaleImage(sourceFilePath, width, height, destFilePath, quality);
    }
}