java 生成缩略图

package com.hfmx.utils;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

/**
 * 图片工具类, 图片水印,文字水印,缩放,补白等
 * 
 * @author mjsh
 * 
 */
public final class ImageUtils {
    /**
     * 图片保存格式
     */
    private static final String PICTRUE_FORMATE_JPG = "jpg";

    /**
     * 添加图片水印
     * 
     * @param targetImg
     *            目标图片路径,如:C://myPictrue//1.jpg
     * @param waterImg
     *            水印图片路径,如:C://myPictrue//logo.png
     * @param x
     *            水印图片右边距离目标图片右边的偏移量
     * @param y
     *            水印图片下边距离目标图片下边的偏移量
     * @param alpha
     *            透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明)
     */
    public final static void pressImage(String targetImg, String waterImg, int x, int y, float alpha) {
        try {
            // 目标文件
            File file = new File(targetImg);
            System.out.println("测试1:" + file.getPath());
            Image image = ImageIO.read(file);
            int width = image.getWidth(null);
            int height = image.getHeight(null);
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(image, 0, 0, width, height, null);
            // 水印文件
            Image waterImage = ImageIO.read(new File(waterImg)); // 水印文件
            int width_1 = waterImage.getWidth(null);
            int height_1 = waterImage.getHeight(null);
            System.out.println("测试2:" + width_1 + "|" + height_1);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            // 计算位置
            System.out.println("测试3:" + width + "|" + height);
            int water_x = width - width_1 - x;
            int water_y = height - height_1 - x;
            System.out.println("测试4:" + water_x + "|" + water_y);
            g.drawImage(waterImage, water_x, water_y, width_1, height_1, null);
            g.dispose();
            // 保存图片
            file.delete();
            ImageIO.write(bufferedImage, PICTRUE_FORMATE_JPG, new File(targetImg));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 图片缩放+补白(如果需要补白,那么最终的图片将是width*height;如果不补白,那么最终的图片是等比例的,不过高度最大height,宽度最大width)
     * 
     * @param targetPath
     *            原文件图片路径
     * @param newFilePath
     *            缩放后的图片路径
     * @param width
     *            缩放后的宽度
     * @param height
     *            缩放后的高度
     * @param bb
     *            比例不对时是否需要补白
     */
    public static void resize(String targetPath, String newFilePath, int width, int height, boolean bb) {
        try {
            // 原始图片
            BufferedImage originalImg = ImageIO.read(new File(targetPath));
            Image itemp;
            // 计算比例
            if ((originalImg.getHeight() > height) || (originalImg.getWidth() > width)) {
                double ratio = 0;// 缩放比例
                if (originalImg.getHeight() > originalImg.getWidth()) {
                    ratio = (new Integer(height)).doubleValue() / originalImg.getHeight();
                } else {
                    ratio = (new Integer(width)).doubleValue() / originalImg.getWidth();
                }
                AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
                itemp = op.filter(originalImg, null);
            } else {
                itemp = originalImg;
            }
            if (bb) {
                // 补白
                BufferedImage newImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                Graphics2D g = newImg.createGraphics();
                g.setColor(Color.white);
                g.fillRect(0, 0, width, height);
                int start_x = 0, start_y = 0;
                if (width == itemp.getWidth(null)) {
                    // 上下补白
                    start_x = 0;
                    start_y = (height - itemp.getHeight(null)) / 2;
                    g.drawImage(itemp, start_x, start_y, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
                } else if (height == itemp.getHeight(null)) {
                    // 左右补白
                    start_x = (width - itemp.getWidth(null)) / 2;
                    start_y = 0;
                } else {
                    // 上下左右补白
                    start_x = (width - itemp.getWidth(null)) / 2;
                    start_y = (height - itemp.getHeight(null)) / 2;
                }
                g.drawImage(itemp, start_x, start_y, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
                g.dispose();
                itemp = newImg;
            }
            ImageIO.write((BufferedImage) itemp, PICTRUE_FORMATE_JPG, new File(newFilePath));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(深入理解java)