图片高保真处理

package file;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;

public class ImageUtil {

	/**
	 * 左上方添加图片
	 * @param bgImagePath
	 * @param fgImagePath
	 * @param overlayImagePath
	 */
	public static String overlayLeftTop(String bgImagePath, String fgImagePath,
			String overlayImagePath) {
		return overlay(bgImagePath, fgImagePath, overlayImagePath, 0, 0);
	}
	/**
	 * 右上方添加图片
	 * @param bgImagePath
	 * @param fgImagePath
	 * @param overlayImagePath
	 */
	public static String overlayRightTop(String bgImagePath, String fgImagePath,
			String overlayImagePath) {
		return overlay(bgImagePath, fgImagePath, overlayImagePath, 1, 0);
	}
	/**
	 * 左下方添加图片
	 * @param bgImagePath
	 * @param fgImagePath
	 * @param overlayImagePath
	 */
	public static String overlayLeftButtom(String bgImagePath,
			String fgImagePath, String overlayImagePath) {
		return overlay(bgImagePath, fgImagePath, overlayImagePath, 0, 1);
	}
	/**
	 * 右下方添加图片
	 * @param bgImagePath
	 * @param fgImagePath
	 * @param overlayImagePath
	 */
	public static String overlayRightButtom(String bgImagePath,
			String fgImagePath, String overlayImagePath) {
		return overlay(bgImagePath, fgImagePath, overlayImagePath, 1, 1);
	}

	/**
	 * 合成图片 x:前置图片所在x轴位置 y:前置图片所在y轴位置
	 */
	public static String overlay(String bgImagePath, String fgImagePath,
			String overlayImagePath, int x, int y) {
		if (!strIsValid(bgImagePath) || !strIsValid(fgImagePath)
				|| !strIsValid(overlayImagePath)) {
			throw new IllegalArgumentException();
		}
		BufferedImage bgImage = readImage(bgImagePath);
		BufferedImage fgImage = readImage(fgImagePath);
		// 合成图片
		BufferedImage overlayedImage = overlayImages(bgImage, fgImage, x, y);

		/** 输出图片 */
		if (overlayedImage != null) {
			File file = new File(overlayImagePath);
			if (file.exists())
				file.delete();
			writeImage(overlayedImage, overlayImagePath, "PNG");
		}
		return overlayImagePath;
	}

	/**
	 * 合成图片
	 * 
	 * @param bgImage
	 * @param fgImage
	 * @return
	 */
	public static BufferedImage overlayImages(BufferedImage bgImage,
			BufferedImage fgImage, int x, int y) {
		if (fgImage.getHeight() > bgImage.getHeight()
				|| fgImage.getWidth() > fgImage.getWidth()) {
			return null;
		}

		Graphics2D g = bgImage.createGraphics();
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g.drawImage(bgImage, 0, 0, null);

		g.drawImage(fgImage, x, y, null);

		g.dispose();
		return bgImage;
	}

	/**
	 * 读取图片
	 * 
	 * @param fileLocation
	 * @return
	 */
	public static BufferedImage readImage(String fileLocation) {
		BufferedImage img = null;
		try {
			img = ImageIO.read(new File(fileLocation));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return img;
	}

	/**
	 * 输出图片
	 * 
	 * @param img
	 * @param fileLocation
	 * @param extension
	 */
	public static void writeImage(BufferedImage img, String fileLocation,
			String extension) {
		try {
			BufferedImage bi = img;
			File outputfile = new File(fileLocation);
			ImageIO.write(bi, extension, outputfile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 检查图片的高度和宽度
	 * 
	 * @param imagePath
	 * @param height
	 * @param width
	 * @return
	 */
	public static boolean checkImageHW(String imagePath, int height, int width) {
		if (StrUtil.isNullOrEmpty(imagePath))
			return false;
		try {
			BufferedImage image = ImageIO.read(new File(imagePath));
			int w = image.getWidth();
			int h = image.getHeight();
			return w == width && h == height;
		} catch (IOException e) {
			return false;
		}
	}

	/**
	 * 图片等比例缩小
	 * 
	 * @param fromFileStr
	 *            : 原图片路径
	 * @param saveToFileStr
	 *            : 缩小后图片的路径
	 * @param width
	 *            : 图片缩小后的高度
	 * @param height
	 *            : 图片缩小后的宽度
	 * @throws Exception
	 */
	public static void shrinkImage(String fromFileStr, String saveToFileStr,
			int width, int height)  {
		if (!strIsValid(fromFileStr) || !strIsValid(fromFileStr)) {
			throw new IllegalArgumentException("image path is null");
		}
		try {
			Thumbnails.of(fromFileStr).size(width,height).toFile(saveToFileStr);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 图片缩小
	 * @param source
	 * @param targetW
	 * @param targetH
	 * @param equalProportion 是否等比缩小
	 * @return
	 */
	public static BufferedImage resize(BufferedImage source, int targetW,
			int targetH, boolean equalProportion) {
		int type = source.getType();
		BufferedImage target = null;
		double sx = (double) targetW / source.getWidth();
		double sy = (double) targetH / source.getHeight();
		if (equalProportion) {
			if (sx > sy) {
				sx = sy;
				targetW = (int) (sx * source.getWidth());
			} else {
				sy = sx;
				targetH = (int) (sx * source.getHeight());
			}
		}
		if (type == BufferedImage.TYPE_CUSTOM) {
			ColorModel cm = source.getColorModel();
			WritableRaster raster = cm.createCompatibleWritableRaster(targetW,
					targetH);
			boolean alphaPremultiplied = cm.isAlphaPremultiplied();
			target = new BufferedImage(cm, raster, alphaPremultiplied, null);
		} else {
			target = new BufferedImage(targetW, targetH, type);
			Graphics2D g = target.createGraphics();
			g.setRenderingHint(RenderingHints.KEY_RENDERING,
					RenderingHints.VALUE_RENDER_QUALITY);
			g.drawRenderedImage(source,
					AffineTransform.getScaleInstance(sx, sy));
			g.dispose();
		}
		return target;
	}

	public static boolean strIsValid(String str) {
		return str != null && !str.trim().equals("");
	}
}

 

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