文章出處

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * @author oucq
 * @version Aug 3, 2016 6:13:55 PM
 * 
 *          該類實現了圖片的合并功能,可以選擇水平合并或者垂直合并。 
 */
public class ImageUtil {

    /**
     * @param fileUrl
     *            文件絕對路徑或相對路徑
     * @return 讀取到的緩存圖像
     * @throws IOException
     *             路徑錯誤或者不存在該文件時拋出IO異常
     */
    public static BufferedImage getBufferedImage(String fileUrl) throws IOException {
        File f = new File(fileUrl);
        return ImageIO.read(f);
    }

    /**
     * @param savedImg
     *            待保存的圖像
     * @param saveDir
     *            保存的目錄
     * @param fileName
     *            保存的文件名,必須帶后綴,比如 "beauty.jpg"
     * @param format
     *            文件格式:jpg、png或者bmp
     * @return
     */
    public static boolean saveImage(BufferedImage savedImg, String saveDir, String fileName, String format) {
        boolean flag = false;

        // 先檢查保存的圖片格式是否正確
        String[] legalFormats = { "jpg", "JPG", "png", "PNG", "bmp", "BMP" };
        int i = 0;
        for (i = 0; i < legalFormats.length; i++) {
            if (format.equals(legalFormats[i])) {
                break;
            }
        }
        if (i == legalFormats.length) { // 圖片格式不支持
            System.out.println("不是保存所支持的圖片格式!");
            return false;
        }

        // 再檢查文件后綴和保存的格式是否一致
        String postfix = fileName.substring(fileName.lastIndexOf('.') + 1);
        if (!postfix.equalsIgnoreCase(format)) {
            System.out.println("待保存文件后綴和保存的格式不一致!");
            return false;
        }

        String fileUrl = saveDir + fileName;
        File file = new File(fileUrl);
        try {
            flag = ImageIO.write(savedImg, format, file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return flag;
    }

    /**
     * 待合并的兩張圖必須滿足這樣的前提,如果水平方向合并,則高度必須相等;如果是垂直方向合并,寬度必須相等。
     * mergeImage方法不做判斷,自己判斷。
     *
     * @param img1
     *            待合并的第一張圖
     * @param img2
     *            帶合并的第二張圖
     * @param isHorizontal
     *            為true時表示水平方向合并,為false時表示垂直方向合并
     * @return 返回合并后的BufferedImage對象
     * @throws IOException
     */
    public static BufferedImage mergeImage(boolean isHorizontal, BufferedImage img1, BufferedImage img2)
            throws IOException {
        int w1 = img1.getWidth();
        int h1 = img1.getHeight();
        int w2 = img2.getWidth();
        int h2 = img2.getHeight();

        // 從圖片中讀取RGB
        int[] ImageArrayOne = new int[w1 * h1];
        ImageArrayOne = img1.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行掃描圖像中各個像素的RGB到數組中
        int[] ImageArrayTwo = new int[w2 * h2];
        ImageArrayTwo = img2.getRGB(0, 0, w2, h2, ImageArrayTwo, 0, w2);

        // 生成新圖片
        BufferedImage DestImage = null;
        if (isHorizontal) { // 水平方向合并
            DestImage = new BufferedImage(w1 + w2, h1, BufferedImage.TYPE_INT_RGB);
            DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 設置上半部分或左半部分的RGB
            DestImage.setRGB(w1, 0, w2, h2, ImageArrayTwo, 0, w2);
        } else { // 垂直方向合并
            DestImage = new BufferedImage(w1, h1 + h2, BufferedImage.TYPE_INT_RGB);
            DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 設置上半部分或左半部分的RGB
            DestImage.setRGB(0, h1, w2, h2, ImageArrayTwo, 0, w2); // 設置下半部分的RGB
        }

        return DestImage;
    }

    /**合并任數量的圖片成一張圖片
     * @param isHorizontal true代表水平合并,fasle代表垂直合并
     * @param imgs 欲合并的圖片數組
     * @return
     * @throws IOException
     */
    public static BufferedImage mergeImage(boolean isHorizontal, BufferedImage... imgs) throws IOException {
        // 生成新圖片
        BufferedImage DestImage = null;

        // 計算新圖片的長和高
        int allw = 0, allh = 0, allwMax = 0, allhMax = 0;
        for (BufferedImage img : imgs) {
            allw += img.getWidth();
            allh += img.getHeight();
            if (img.getWidth() > allwMax) {
                allwMax = img.getWidth();
            }
            ;
            if (img.getHeight() > allhMax) {
                allhMax = img.getHeight();
            }
            ;
        }
        // 創建新圖片
        if (isHorizontal) {
            DestImage = new BufferedImage(allw, allhMax, BufferedImage.TYPE_INT_RGB);
        } else {
            DestImage = new BufferedImage(allwMax, allh, BufferedImage.TYPE_INT_RGB);
        }

        // 合并所有子圖片到新圖片
        int wx = 0, wy = 0;
        for (int i = 0; i < imgs.length; i++) {
            BufferedImage img = imgs[i];
            int w1 = img.getWidth();
            int h1 = img.getHeight();
            // 從圖片中讀取RGB
            int[] ImageArrayOne = new int[w1 * h1];
            ImageArrayOne = img.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行掃描圖像中各個像素的RGB到數組中
            if (isHorizontal) { // 水平方向合并
                DestImage.setRGB(wx, 0, w1, h1, ImageArrayOne, 0, w1); // 設置上半部分或左半部分的RGB
            } else { // 垂直方向合并
                DestImage.setRGB(0, wy, w1, h1, ImageArrayOne, 0, w1); // 設置上半部分或左半部分的RGB
            }
            wx += w1;
            wy += h1;
        }
        return DestImage;
    }

    public static void main(String[] args) {
        // 讀取待合并的文件
        BufferedImage bi1 = null;
        BufferedImage bi2 = null;
        try {
            bi1 = getBufferedImage("src/one.jpg");
            bi2 = getBufferedImage("src/two.jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 調用mergeImage方法獲得合并后的圖像
        BufferedImage destImg = null;
        try {
            destImg = mergeImage(false, bi1, bi2, bi2);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 保存圖像
        saveImage(destImg, "src/", "allv.jpg", "jpg");
        System.out.println("垂直合并完畢!");
        //////////////////////////////////////////////////////////////////////////////////
        System.out.println("下面是水平合并的情況:");

        try {
            bi1 = getBufferedImage("src/one.jpg");
            bi2 = getBufferedImage("src/two.jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 調用mergeImage方法獲得合并后的圖像
        try {
            destImg = mergeImage(true, bi1, bi2, bi2);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 保存圖像
        saveImage(destImg, "src/", "allh.jpg", "jpg");
        System.out.println("水平合并完畢!");

    }

}

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(2) 人氣()