图片压缩

public class PictureUtils {

    private Logger log = LoggerFactory.getLogger(PictureUtils.class);

    @Autowired
    private FileStoreConfigure fileStoreConfigure;

    public ThumbnaiFile compressedPicture(MultipartFile jobFile, String fileName) {
        ThumbnaiFile thumbnaiFile = new ThumbnaiFile();

        File file = null;
        try {
            file = File.createTempFile("tmp", null);
            jobFile.transferTo(file);
            file.deleteOnExit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        int[] results = getImgWidth(file);
        int widthdist = (int) (results[0] * 0.1);
        int heightdist = (int) (results[1] * 0.1);

        thumbnaiFile.setThumbnailWidth(String.valueOf(widthdist));
        thumbnaiFile.setThumbnailHeight(String.valueOf(heightdist));
        thumbnaiFile.setByteSize(String.valueOf(file.length()));

        Image src = null;
        try {
            src = javax.imageio.ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        BufferedImage tag = new BufferedImage((int) widthdist,
                (int) heightdist, BufferedImage.TYPE_INT_RGB);

        tag.getGraphics().drawImage(
                src.getScaledInstance(widthdist, heightdist,
                        Image.SCALE_SMOOTH), 0, 0, null);

        File localFile = tmpLocalSave(tag, fileName);

        thumbnaiFile.setFile(localFile);

        return thumbnaiFile;
    }

    private  File tmpLocalSave(BufferedImage tag, String fileName) {

        FileOutputStream fos = null;
        BufferedOutputStream buffStream = null;

        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(tag, "JPG", out);
            byte[] bytes = out.toByteArray();
            String dir = (null != fileStoreConfigure && !StringUtils.isEmpty(fileStoreConfigure.getDir())) ? fileStoreConfigure.getDir() : SystemParameter.FILE_DIR_TMP;
            File dirFile = new File(dir);
            if(!dirFile.exists()) {
                dirFile.mkdir();
            }
            File dataFile = new File(dir + File.separator + fileName + ".JPG");
            fos = new FileOutputStream(dataFile);
            buffStream =  new BufferedOutputStream(fos);
            buffStream.write(bytes);
            buffStream.flush();
            return dataFile;
        } catch (Exception e) {
            fileName = StringUtils.isEmpty(fileName) ? "" : fileName;
            log.error("文件["+ fileName +"]保存失败,失败原因:" + e.getMessage(),e);
            return null;
        } finally {
            if(null != fos){
                try {
                    fos.close();
                } catch (IOException e) {
                    log.error("关闭文件失败",e);
                }
            }
            if(null != buffStream){
                try {
                    buffStream.close();
                } catch (IOException e) {
                    log.error("关闭文件失败",e);
                }
            }
        }
    }

    /**
     * 获取图片宽度
     *
     * @param file
     *            图片文件
     * @return 宽度
     */
    public static int[] getImgWidth(File file) {
        InputStream is = null;
        BufferedImage src = null;
        int result[] = { 0, 0 };
        try {
            is = new FileInputStream(file);
            src = javax.imageio.ImageIO.read(is);
            result[0] = src.getWidth(null); // 得到源图宽
            result[1] = src.getHeight(null); // 得到源图高
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

你可能感兴趣的:(图片压缩)