审查视图

air-common/src/main/java/com/teplot/common/ImageUtil.java 7.8 KB
蔺文领 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
package com.teplot.common;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;

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

/**
 * Depiction: 图片处理工具
 * <p>
 * Modify:
 * <p>
 * Author: Kevin Lynn
 * <p>
 * Create Date:2017年4月6日 下午12:35:36
 * 
 */
public class ImageUtil {

	public ImageUtil() {
	}

	public static boolean isImage(String fileName) {
		if (StrKit.isBlank(fileName)) {
			return false;
		}

		fileName = fileName.toLowerCase();
		if (fileName.endsWith(".png") || fileName.endsWith(".jpg") || fileName.endsWith(".jpeg")
				|| fileName.endsWith(".bmp")) {
			return true;
		}

		return false;
	}

	/**
	 * 计算图片尺寸
	 * 
	 * @param imgPath
	 *            图片路径
	 * @return {@link Dimension}
	 */
	public static Dimension getImageSize(String imgPath) {
		Dimension dimen = new Dimension(0, 0);

		File file = new File(imgPath);
		try {
			BufferedImage reader = ImageIO.read(new FileInputStream(file));
			dimen.setSize(reader.getWidth(), reader.getHeight());
		} catch (IOException e) {
			Log.getLog(new Dimension().getClass()).error("getImageSize()-->" + e.toString());
		}

		return dimen;
	}

	/**
	 * 根据图片宽度的最大尺寸计算应该缩放的比例
	 * 
	 * @param srcImageFile
	 *            图片源文件
	 * @param limitWidth
	 *            宽度最大尺寸
	 * @return scale
	 */
	public final static double computeScale(String srcImageFile, double limitWidth) {
		double scale = 1.0;
		Image img;
		try {
			img = ImageIO.read(new File(srcImageFile));
			int originalWidth = img.getWidth(null);
			if (originalWidth <= limitWidth) {
				scale = 1.0;
			} else {
				scale = limitWidth / originalWidth;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return scale;
	}

	/**
	 * 缩放图像(根据最大宽度按比例缩放)
	 * 
	 * @param srcImageFile
	 *            源图像文件地址
	 * @param result
	 *            缩放后的图像地址
	 * @limitWidth 最大宽度
	 */
	public final static void scaleImage(String srcImageFile, String newImageFile, double limitWidth) {
		double scale = ImageUtil.computeScale(srcImageFile, limitWidth);
		if (scale > 1.0) {
			scale(srcImageFile, srcImageFile, scale);
		}
	}

	/**
	 * 缩放图像(按比例缩放)
	 * 
	 * @param srcImageFile
	 *            源图像文件地址
	 * @param result
	 *            缩放后的图像地址
	 * @scale 缩放比例,一般是小于1的小数
	 */
	public final static void scale(String srcImageFile, String newImageFile, double scale) {
		try {
			Thumbnails.of(srcImageFile).scale(scale).toFile(newImageFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 缩放图像(按高度和宽度缩放)
	 * 
	 * @param srcImageFile
	 *            源图像文件地址
	 * @param result
	 *            缩放后的图像地址
	 * @param height
	 *            缩放后的高度
	 * @param width
	 *            缩放后的宽度
	 * @throws IOException
	 */
	public final static void scale(String srcImageFile, String newImageFile, int width, int height) throws IOException {
		Image img = ImageIO.read(new File(srcImageFile));
		int newWidth = img.getWidth(null), newHeight = img.getHeight(null);

		// 计算比例
		if (img.getWidth(null) <= width) {
			// 无线调用时,宽度没有压缩宽大,则返回原图
		} else if ((img.getHeight(null) > height) || (img.getWidth(null) > width)) {
			// vst端:原图长/压缩长 > 原图宽/压缩图宽 ? 原图长/压缩长:原图宽/压缩图宽,以比例较大的为基准压缩
			// 压缩比判断方法二
			double ratio_height = (new Integer(height)).doubleValue() / img.getHeight(null);
			double ratio_width = (new Integer(width)).doubleValue() / img.getWidth(null);

			if (ratio_height > ratio_width) {
				newHeight = height;
				newWidth = (int) ((new Integer(height)).doubleValue() / img.getHeight(null) * img.getWidth(null));
			} else {
				newWidth = width;
				newHeight = (int) ((new Integer(width)).doubleValue() / img.getWidth(null) * img.getHeight(null));
			}
		}

		Thumbnails.of(srcImageFile).size(newWidth, newHeight).toFile(newImageFile);

	}

	/**
	 * 添加图片水印
	 * 
	 * @param targetImg
	 *            目标图片路径,如:C:\\myPictrue\\1.jpg
	 * @param waterImg
	 *            水印图片路径,如:C:\\myPictrue\\logo.png
	 * @param positions
	 *            水印图片所在位置
	 * 
	 * @param alpha
	 *            透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明) * @param quality 压缩清晰度
	 *            <b>建议为1.0</b>
	 * @throws IOException
	 */
	public final static void pressImage(String targetImg, File waterImg, Positions positions, float alpha)
			throws IOException {
		Thumbnails.of(targetImg).watermark(positions, ImageIO.read(waterImg), alpha).scale(1)// 缩放比例
				.toFile(targetImg);
	}

	/**
	 * 把图片印刷到图片上
	 * 
	 * @param pressImg
	 *            -- 水印文件
	 * @param targetImg
	 *            -- 目标文件
	 * @param alpha
	 *            透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明) * @param quality 压缩清晰度
	 *            <b>建议为1.0</b>
	 * 
	 * @throws IOException
	 */
	public final static void pressImage(String targetImg, String pressImg, float alpha) throws IOException {
		Thumbnails.of(targetImg).watermark(Positions.TOP_LEFT, ImageIO.read(new File(pressImg)), alpha).outputQuality(1)// 生成质量100%
				.scale(1)// 缩放比例
				.toFile(targetImg);
	}

	/**
	 * 把图片印刷到图片上
	 * 
	 * @param pressImg
	 *            -- 水印文件
	 * @param targetImg
	 *            -- 目标文件
	 * @param position
	 * 
	 * @param alpha
	 *            透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明) * @param quality 压缩清晰度
	 *            <b>建议为1.0</b>
	 * 
	 * @throws IOException
	 */
	public final static void pressImage(String targetImg, String pressImg, Positions position, float alpha)
			throws IOException {
		Thumbnails.of(targetImg).watermark(position, ImageIO.read(new File(pressImg)), alpha).outputQuality(1)// 生成质量100%
				.scale(1)// 缩放比例
				.toFile(targetImg);
	}

	/**
	 * 裁剪图片
	 * 
	 * @throws IOException
	 */
	public static void region(String sourceImg, String newImgPath, int x, int y, int width, int height)
			throws IOException {
		// 指定坐标
		Thumbnails.of(sourceImg).sourceRegion(x, y, width, height)// x轴、y轴,裁剪宽、裁剪高
				.size(width, height)// 裁剪后的图片生成的尺寸
				// 设置是否按比例 false,图片可能会走形 默认true,必须在设置尺寸后设置
				.keepAspectRatio(false).toFile(newImgPath);
	}

	/**
	 * 旋转图片
	 * 
	 * @param sourceImage
	 *            原图片
	 * @param newImage
	 *            生成的新图片
	 * @param degrees
	 *            旋转度数
	 * @throws IOException
	 */
	public static void rorate(String sourceImage, String newImage, double degrees) throws IOException {
		Thumbnails.of(sourceImage).rotate(degrees)// 旋转度数
				.scale(1)// 缩放比例
				.toFile(newImage);
	}

	/**
	 * 转换图片格式
	 * 
	 * @param sourceImg
	 *            原图
	 * @param newImg
	 *            转换后的新图
	 * @param format
	 *            格式
	 * @throws IOException
	 */
	public static void transferImageFormat(String sourceImg, String newImg, String format) throws IOException {
		Thumbnails.of(sourceImg).outputFormat(format).scale(1).toFile(newImg);
	}

	public static void main(String[] args) {
		String srcImageFile = "/Users/Kevin/Downloads/酒图片/1.jpg";
		ImageUtil.scaleImage(srcImageFile, srcImageFile, 100);
	}
}