package org.fouram.core.util;

import java.awt.Image;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import net.coobird.thumbnailator.name.Rename;

/**
 * 图片压缩工具类
 */
public class ImageUtil {
	
	/**
	 * 判断是不是图片
	 * 
	 * @param imagePath
	 * @return
	 */
	public static boolean isImage(String imagePath) {
		try {
			Image image = ImageIO.read(new File(imagePath));
			if (image == null) {
				return false;
			} else {
				ImageInputStream iis = ImageIO.createImageInputStream(new File(imagePath));
				Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
				if (!iter.hasNext()) {
					return false;
				}
				iis.close();
			}
		} catch (IOException ex) {
			return false;
		}
		return true;
	}

	/**
	 * 使用给定的图片生成指定大小的图片
	 */
	public static void generateFixedSizeImage(String inImg, String outImg, int width, int height) {
		try {
			Thumbnails.of(inImg).size(width, height).toFile(outImg);
		} catch (IOException e) {
			System.out.println("原因: " + e.getMessage());
		}
	}

	/**
	 * 对原图加水印,然后顺时针旋转90度,最后压缩为80%保存
	 *
	 */
	public static void generateRotationWatermark(String inImg, String outImg, String watermark, int width, int height,
			float quality) {
		try {
			Thumbnails.of(inImg).size(width, height). // 缩放大小
			// rotate(90). // 顺时针旋转90度
					watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(watermark)), 0.5f). // 水印位于右下角,半透明
					outputQuality(quality). // 图片质量压缩百分比
					toFile(outImg);
		} catch (IOException e) {
			System.out.println("原因: " + e.getMessage());
		}
	}

	/**
	 * 转换图片格式,将流写入到输出流
	 */
	public static void generateOutputstream(String inImg, String outImg, int width, int height, String format) {
		try (OutputStream outputStream = new FileOutputStream(outImg)) { // 自动关闭流
			Thumbnails.of(inImg).size(width, height).outputFormat(format). // 转换格式
																			// png,jpg等
					toOutputStream(outputStream); // 写入输出流
		} catch (IOException e) {
			System.out.println("原因: " + e.getMessage());
		}
	}

	/**
	 * 按比例缩放图片
	 */
	public static void generateScale(String inImg, String outImg, float scale, float quality) {
		try {
			Thumbnails.of(inImg).scale(scale). // 图片缩放百分比, 不能和size()一起使用
					outputQuality(quality). // 图片质量压缩百分比
					toFile(outImg);
		} catch (IOException e) {
			System.out.println("原因: " + e.getMessage());
		}
	}

	/**
	 * 生成缩略图到指定的目录
	 */
	public static void generateThumbnail2Directory(float scale, String outPath, String... inImg) {
		try {
			Thumbnails.of(inImg).scale(scale). // 图片缩放百分比, 不能和size()一起使用
					toFiles(new File(outPath), Rename.NO_CHANGE);// 指定的目录一定要存在,否则报错
		} catch (IOException e) {
			System.out.println("原因: " + e.getMessage());
		}
	}

	/**
	 * 将指定目录下所有图片生成缩略图
	 */
	public static void generateDirectoryThumbnail(String inPath, float scale, String outPath) {
		try {
			Thumbnails.of(new File(inPath).listFiles()).scale(scale). // 图片缩放百分比,
																		// 不能和size()一起使用
					toFiles(new File(outPath), Rename.SUFFIX_HYPHEN_THUMBNAIL);// 指定的目录一定要存在,否则报错
		} catch (IOException e) {
			System.out.println("原因: " + e.getMessage());
		}
	}

	// 旋转
	public static void rotate(String sourceImage, String newImage, double degrees) {
		try {
			Thumbnails.of(sourceImage).rotate(degrees)// 旋转度数
					.scale(1)// 缩放比例
					.outputQuality(1f).toFile(newImage);
		} catch (IOException e) {
			LoggerUtil.error(e);
		}
	}

	public static void main(String[] args) {
		ImageUtil.rotate("e://1.JPG", "e://7d5002043c414ce08f8b65870536dde7.JPG", 360);
	}
}