java-圖像處理(一、水印文字 二、水印圖標 三、縮略圖 四、裁剪圖像)

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;

/**
 * 圖像處理: 一、水印文字 二、水印圖標 三、縮略圖 四、裁剪圖像
 * 
 * @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();
        }
    }
}
相關文章
相關標籤/搜索