图片工具类——两张图片合并,小图合并成大图

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.imageio.ImageIO;

/**
 * 图片工具类
 * @author cxp
 *
 */
public final class ImgUtil {

    private ImgUtil() {
    }

    /**
     * 合并图片
     * 
     * @param path1
     *            图片路径1
     * @param path2
     *            图片路径2
     * @param path
     *            新图片路径
     * @throws IOException
     */
    public static void mergeImage(String path1, String path2, String path)
            throws IOException {
        BufferedImage image1 = ImageIO.read(new File(path1));
        BufferedImage image2 = ImageIO.read(new File(path2));

        BufferedImage combined = new BufferedImage(image1.getWidth(),
                image1.getHeight(), BufferedImage.TYPE_INT_RGB);

        // paint both images, preserving the alpha channels
        Graphics g = combined.getGraphics();
        g.drawImage(image1, 0, 0, null);
        g.drawImage(image2, 0, 0, null);

        // Save as new image
        ImageIO.write(combined, "png", new File(path));
    }

    /**
     * 小图片合并为大图片
     * @param path 大图片的输出路径
     * @param imgs 小图片地址
     * @param colCount
     * @param samllWidth
     * @param smallHeigh
     * @param picName
     * @return
     */
    public static String createBigPng(String path, List imgs,
            int colCount, int samllWidth, int smallHeigh, String picName) {
        try {

            File bigImgTxt = new File(path + File.separator + picName + ".txt");
            if (!bigImgTxt.exists()) {
                bigImgTxt.createNewFile();
            }

            FileWriter fileWriter = new FileWriter(bigImgTxt);

            int imgCount = imgs.size();
            int rowCount = imgCount / colCount
                    + (imgCount % colCount == 0 ? 0 : 1); // 小图行数
            int bigWidth = samllWidth * colCount;
            int bigHeight = smallHeigh * rowCount;
            // 按照大图片宽高绘制一个背景图片
            BufferedImage bufImage = new BufferedImage(bigWidth, bigHeight,
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufImage.createGraphics();
            bufImage = g.getDeviceConfiguration().createCompatibleImage(
                    bigWidth, bigHeight, Transparency.TRANSLUCENT);
            g.dispose();
            g = bufImage.createGraphics();
            int x = 0;
            int y = 0; // 纵坐标
            for (int i = 0; i < imgCount; i++) {
                String imgName = imgs.get(i);
                if (i % colCount == 0) {
                    x = 0;
                    if (i > 1) {
                        y += smallHeigh;
                    }
                }

                BufferedImage input = ImageIO.read(new File(imgName));
                Image smallImg = input.getScaledInstance(samllWidth,
                        smallHeigh, Image.SCALE_SMOOTH);

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

                fileWriter.write(imgName.substring(
                        imgName.lastIndexOf("/") + 1, imgName.lastIndexOf("."))
                        + ": ["
                        + x
                        + ","
                        + y
                        + ","
                        + (x + samllWidth)
                        + ","
                        + (y + smallHeigh) + "]\r\n");

                x += samllWidth;

            }
            g.dispose();

            String bigPicPath = path + "/" + picName + ".png";
            ImageIO.write(bufImage, "png", new File(bigPicPath));

            if (fileWriter != null) {
                fileWriter.flush();
                fileWriter.close();
            }
            return bigPicPath;
        } catch (Exception e) {
            System.out.println("createBigJPG Failed!");
            e.printStackTrace();
        }
        return null;
    }

     
    public static void main(String[] args) {

        String path = "C:\\Users\\tt\\Desktop\\tupian";
        File file = new File(path);
        String[] imgsFiles = file.list();
        List imgs = Arrays.asList(imgsFiles);
        createBigPng(path, imgs, 10, 48, 48, "tt.png");

    }
}

你可能感兴趣的:(图片工具类——两张图片合并,小图合并成大图)