java-图像处理(1、水印文字 2、水印图标 3、缩略图 4、裁剪图像)

package imgUtil;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

/**
 * 图像处理: 1、水印文字 2、水印图标 3、缩略图 4、裁剪图像
 * 
 * @author Weirdo-world
 */
public class ImgProcessing {
    private File file = new File(""); // 原文件或目录
    private String text = "Weirdo-world"; // 水印文字
    private Color color = new Color(255, 255, 255); // 水印字体颜色及透明度
    private Font font = new Font("黑体", Font.BOLD, 30); // 水印字体
    private String dirl = "e:/"; // 水印图片存放目录
    private int position = 9;// 水印位置
    private File icon = new File("");// 水印图标

    public ImgProcessing() {
    }

    /**
     * @param file
     *            原图
     * @param dirl
     *            存放目录
     */
    public ImgProcessing(File file, String dirl) {
        this.file = file;
        this.dirl = dirl;
    }

    /**
     * @return 获取原文件
     */
    public File getFile() {
        return file;
    }

    /**
     * @param file
     *            设置原图片或目录
     */
    public void setFile(File file) {
        this.file = file;
    }

    /**
     * @return 获取文字
     */
    public String getText() {
        return text;
    }

    /**
     * @param text
     *            水印文字设置
     */
    public void setText(String text) {
        this.text = text;
    }

    /**
     * @return 获取颜色
     */
    public Color getColor() {
        return color;
    }

    /**
     * @param color
     *            设置水印字体颜色及透明度
     */
    public void setColor(Color color) {
        this.color = color;
    }

    /**
     * @return 获取字体
     */
    public Font getFont() {
        return font;
    }

    /**
     * @param font
     *            水印字体设置
     */
    public void setFont(Font font) {
        this.font = font;
    }

    /**
     * @return 获取存放目录
     */
    public String getDirl() {
        return dirl;
    }

    /**
     * @param dirl
     *            设置水印图片存放目录
     */
    public void setDirl(String dirl) {
        this.dirl = dirl;
    }

    /**
     * @return 获取位置
     */
    public int getPosition() {
        return position;
    }

    /**
     * @param position
     *            设置水印位置
     */
    public void setPosition(int position) {
        this.position = position;
    }

    /**
     * @return 获取图标
     */
    public File getIcon() {
        return icon;
    }

    /**
     * @param icon
     *            水印图标
     */
    public void setIcon(File icon) {
        this.icon = icon;
    }

    /**
     * 添加水印文字
     */
    public void imgText() {
        try {
            if (!file.canRead()) {
                System.out.println("请定义一个原图文件");
                return;
            } else if (!file.getName().toLowerCase().endsWith(".jpg")) {
                if (!file.getName().toLowerCase().endsWith(".png")) {
                    System.out.println("请先定义要添加水印的图片文件,格式为:jpg、png");
                    return;
                }
            }
            BufferedImage bf = ImageIO.read(file); // 读取文件
            Graphics g = bf.getGraphics();// 创建画笔
            g.setFont(font); // 设置字体
            g.setColor(color); // 设置颜色
            FontMetrics f = g.getFontMetrics(); // 创建字体规格
            int fw = f.stringWidth(text); // 获取字体宽度
            int fh = f.getHeight(); // 获取字体高度
            int x = 0, y = 0;
            Random r = new Random();
            switch (position) {
            case 1: // 左上
                x = 20;
                y = 20 + font.getSize();
                break;
            case 2: // 中上
                x = (int) ((bf.getWidth() - fw) / 2f);
                y = 20 + font.getSize();
                break;
            case 3: // 右上
                x = bf.getWidth() - fw - 20;
                y = 20 + font.getSize();
                break;
            case 4: // 左中
                x = 20;
                y = (int) ((bf.getHeight() - fh) / 2f) + font.getSize();
                break;
            case 5: // 中心
                x = (int) ((bf.getWidth() - fw) / 2f);
                y = (int) ((bf.getHeight() - fh) / 2f) + font.getSize();
                break;
            case 6: // 右中
                x = bf.getWidth() - fw - 20;
                y = (int) ((bf.getHeight() - fh) / 2f) + font.getSize();
                break;
            case 7: // 左下
                x = 20;
                y = bf.getHeight() - fh - 20 + font.getSize();
                break;
            case 8: // 中下
                x = (int) ((bf.getWidth() - fw) / 2f);
                y = bf.getHeight() - fh - 20 + font.getSize();
                break;
            case 9: // 右下
                x = bf.getWidth() - fw - 20;
                y = bf.getHeight() - fh - 20 + font.getSize();
                break;
            default: // 随机
                x = r.nextInt(bf.getWidth() - fw - 20) + 20;
                y = r.nextInt(bf.getHeight() - fh - 20) + font.getSize();
                break;
            }
            g.drawString(text, x, y);// 写入文字
            g.dispose();// 关闭画笔
            String ext = file.getName().substring(file.getName().lastIndexOf(".") + 1);
            File dir = new File(dirl + file.getName().substring(0, file.getName().lastIndexOf(".")) + "_text." + ext);
            if (!dir.getParentFile().exists()) {
                System.out.println("指定存放目录不存在!!");
                return;
            }
            ImageIO.write(bf, "jpg", dir);// 写入图片
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加水印图标
     */
    public void imgIcon() {
        try {
            if (!file.canRead() | !icon.canRead()) {
                System.out.println("1.请定义一个原图文件和水印图标文件");
                return;
            } else if (!file.getName().toLowerCase().endsWith(".jpg")) {
                if (!file.getName().toLowerCase().endsWith(".png")) {
                    System.out.println("2.原图文件格式不对,格式应为:jpg、png");
                    return;
                }
            } else if (!icon.getName().toLowerCase().endsWith(".jpg")) {
                if (!icon.getName().toLowerCase().endsWith(".png")) {
                    System.out.println("3.水印图标文件格式不对,格式应为:jpg、png");
                    return;
                }
            }
            BufferedImage bf = ImageIO.read(file); // 读取原图像
            int bfw = bf.getWidth();
            int bfh = bf.getHeight();
            BufferedImage ic = ImageIO.read(icon); // 读取水印图标
            int icw = ic.getWidth();
            int ich = ic.getHeight();
            Graphics g = bf.getGraphics();
            int x = 0, y = 0;
            Random r = new Random();
            switch (position) {
            case 1: // 左上
                x = 20;
                y = 20;
                break;
            case 2: // 中上
                x = (int) ((bfw - icw) / 2d);
                y = 20;
                break;
            case 3: // 右上
                x = bfw - icw - 20;
                y = 20;
                break;
            case 4: // 左中
                x = 20;
                y = (int) ((bfh - ich) / 2d);
                break;
            case 5: // 中心
                x = (int) ((bfw - icw) / 2d);
                y = (int) ((bfh - ich) / 2d);
                break;
            case 6: // 右中
                x = bfw - icw - 20;
                y = (int) ((bfh - ich) / 2d);
                break;
            case 7: // 左下
                x = 20;
                y = bfh - ich - 20;
                break;
            case 8: // 中下
                x = (int) ((bfw - icw) / 2d);
                y = bfh - ich - 20;
                break;
            case 9: // 右下
                x = bfw - icw - 20;
                y = bfh - ich - 20;
                break;
            default: // 随机
                x = r.nextInt(bfw - icw - 20) + 20;
                y = r.nextInt(bfh - ich - 40) + 20;
                break;
            }
            g.drawImage(ic, x, y, icw, ich, null);
            g.dispose();// 关闭画笔
            String ext = file.getName().substring(file.getName().lastIndexOf(".") + 1);
            File dir = new File(dirl + file.getName().substring(0, file.getName().lastIndexOf(".")) + "_icon." + ext);
            if (!dir.getParentFile().exists()) {
                System.out.println("指定存放目录不存在!!");
                return;
            }
            ImageIO.write(bf, "jpg", dir);// 写入图片
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据百分比生成缩略图
     * 
     * @param thum
     *            指定缩略图的百分比
     */
    public void imgThumbnail(double thum) {
        try {
            if (!file.canRead()) {
                System.out.println("请定义一个原图文件");
                return;
            } else if (!file.getName().toLowerCase().endsWith(".jpg")) {
                if (!file.getName().toLowerCase().endsWith(".png")) {
                    System.out.println("请先定义原图片文件,格式为:jpg、png");
                    return;
                }
            }
            BufferedImage i = ImageIO.read(file);
            int iw = i.getWidth();
            int ih = i.getHeight();
            int w = (int) (iw * thum);
            int h = (int) (ih * thum);
            BufferedImage di = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            Graphics g = di.getGraphics();
            g.drawImage(i, 0, 0, w, h, null);
            g.dispose();
            String ext = file.getName().substring(file.getName().lastIndexOf(".") + 1);
            File dir = new File(dirl + file.getName().substring(0, file.getName().lastIndexOf(".")) + "_p_thum." + ext);
            if (!dir.getParentFile().exists()) {
                System.out.println("指定存放目录不存在!!");
                return;
            }
            ImageIO.write(di, "jpg", dir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据宽生成缩略图
     * 
     * @param thum
     *            指定缩略图的宽
     */
    public void imgThumbnail(int thum) {
        try {
            if (!file.canRead()) {
                System.out.println("请定义一个原图文件");
                return;
            } else if (!file.getName().toLowerCase().endsWith(".jpg")) {
                if (!file.getName().toLowerCase().endsWith(".png")) {
                    System.out.println("请先定义原图片文件,格式为:jpg、png");
                    return;
                }
            }
            BufferedImage i = ImageIO.read(file);
            int iw = i.getWidth();
            int ih = i.getHeight();
            int w = thum;
            int h = (int) ((double) thum / iw * ih);
            BufferedImage di = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            Graphics g = di.getGraphics();
            g.drawImage(i, 0, 0, w, h, null);
            g.dispose();
            String ext = file.getName().substring(file.getName().lastIndexOf(".") + 1);
            File dir = new File(dirl + file.getName().substring(0, file.getName().lastIndexOf(".")) + "_w_thum." + ext);
            if (!dir.getParentFile().exists()) {
                System.out.println("指定存放目录不存在!!");
                return;
            }
            ImageIO.write(di, "jpg", dir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 裁剪图像
     * 
     * @param w
     *            剪切的宽
     * @param h
     *            剪切的高
     * @param x
     *            指定从原图的宽开始
     * @param y
     *            指定从原图的高开始
     */
    public void imgCrop(int w, int h, int x, int y) {
        try {
            if (!file.canRead()) {
                System.out.println("请定义一个原图文件");
                return;
            } else if (!file.getName().toLowerCase().endsWith(".jpg")) {
                if (!file.getName().toLowerCase().endsWith(".png")) {
                    System.out.println("请先定义原图片文件,格式为:jpg、png");
                    return;
                }
            }
            BufferedImage bi = ImageIO.read(file);
            BufferedImage ni = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            Graphics g = ni.getGraphics();
            int nw = w + x;
            int nh = h + y;
            g.drawImage(bi, 0, 0, w, h, x, y, nw, nh, null);
            g.dispose();
            String ext = file.getName().substring(file.getName().lastIndexOf(".") + 1);
            File dir = new File(dirl + file.getName().substring(0, file.getName().lastIndexOf(".")) + "_crop." + ext);
            if (!dir.getParentFile().exists()) {
                System.out.println("指定存放目录不存在!!");
                return;
            }
            ImageIO.write(ni, "jpg", dir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

posted @ 2018-09-23 16:57  Weirdo-world  阅读(477)  评论(0编辑  收藏  举报