java实现图片裁剪,旋转,拼接

图片裁剪,按编号拼接,旋转程序记录

 

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ImageHandle {

    public static final int AMOUNT = 4;

    public static void main(String[] args) throws Exception {
        List fileList = new ArrayList();
        fileList = ImageHandle.cutImageToFile("E:\\Program\\testleetcode\\source", "E:\\Program\\testleetcode\\target", 4);
        //配合拼接方法参数,本来可以修改拼接方法的,但是懒得修改了,
        for (int j = 1; j < 1000; j++) {
            List tmp = new ArrayList();
            for (int i = 0; i < fileList.size(); i++) {
                String suffix = fileList.get(i).getName().substring(0, 1);
                if (suffix.equals(String.valueOf(j)))
                tmp.add(fileList.get(i));
            }
            String[] files = new String[tmp.size()];
            for (int k = 0; k < tmp.size(); k++) {
                files[k] = tmp.get(k).toString();
            }
            BufferedImage img = ImageHandle.mergeImage(files, 1, files.length);
            ImageHandle.rotateImage(img, 90, "E:\\Program\\testleetcode\\result", tmp);
        }
    }




    /**
     * 切割图片
     *
     * @param sourceFilePath 源文件路径
     * @param sourceFilePath 保存文件路径
     * @param index          水平方向等分切割数
     * @return List   File数组
     */
    public static List cutImageToFile(String sourceFilePath, String targetDir, int index) {
        File file = new File(sourceFilePath);
        if (file.exists()) {
            return cutImageToFile(file, targetDir, index);
        } else {
            return null;
        }
    }

    /**
     * 切割图片
     *
     * @param sourceFile 源文件
     * @param index      水平方向等分切割数
     * @return List base64编码数组
     */
    public static List cutImageToFile(File sourceFile, String targetDir, int index) {
        List list = new ArrayList();
        File[] fileList = sourceFile.listFiles();
        for (File file : fileList) {
            int suffixIndex = file.getName().lastIndexOf(".");
            String suffix = file.getName().substring(suffixIndex + 1);
            String name = file.getName().substring(0, suffixIndex);
            try {
                BufferedImage source = ImageIO.read(file);
                int width = source.getWidth(); // 图片宽度
                int height = source.getHeight(); // 图片高度
                if (index > 1) {
                    int cheight = height / index; // 切片高度
                    BufferedImage image = null;
                    File file1 = new File(targetDir);
                    if (!file.exists()) { // 存储目录不存在,则创建目录
                        file.mkdirs();
                    }
                    for (int i = 0; i < index; i++) {
                        // x坐标,y坐标,宽度,高度
                        int ch = i * cheight;
                        image = source.getSubimage(0, ch, width, cheight);
                        String fileName = targetDir + "/" + name + "_" + i + "." + suffix;
                        file = new File(fileName);
                        ImageIO.write(image, "PNG", file);
                        list.add(file);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return list;
    }

    /**
     * @param files 要拼接的文件列表
     * @param type  1 横向拼接, 2 纵向拼接
     * @Description:图片拼接 (注意:必须两张图片长宽一致哦)
     */
    public static BufferedImage mergeImage(String[] files, int type, int times) {
        int len = files.length;
        if (len < 1) {
            throw new RuntimeException("图片数量小于1");
        }
        File[] src = new File[len];
        BufferedImage[] images = new BufferedImage[len];
        int[][] ImageArrays = new int[len][];
        for (int i = 0; i < times; i++) {
            try {
                src[i] = new File(files[i]);
                images[i] = ImageIO.read(src[i]);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            int width = images[i].getWidth();
            int height = images[i].getHeight();
            ImageArrays[i] = new int[width * height];
            ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width);
        }
        int newHeight = 0;
        int newWidth = 0;
        for (int i = 0; i < times; i++) {
            // 横向
            if (type == 1) {
                newHeight = newHeight > images[i].getHeight() ? newHeight : images[i].getHeight();
                newWidth += images[i].getWidth();
            } else if (type == 2) {// 纵向
                newWidth = newWidth > images[i].getWidth() ? newWidth : images[i].getWidth();
                newHeight += images[i].getHeight();
            }
        }
        if (type == 1 && newWidth < 1) {
            return null;
        }
        if (type == 2 && newHeight < 1) {
            return null;
        }

        // 生成新图片
        try {
            BufferedImage ImageNew = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
            int height_i = 0;
            int width_i = 0;
            for (int i = 0; i < times; i++) {
                if (type == 1) {
                    ImageNew.setRGB(width_i, 0, images[i].getWidth(), newHeight, ImageArrays[i], 0,
                            images[i].getWidth());
                    width_i += images[i].getWidth();
                } else if (type == 2) {
                    ImageNew.setRGB(0, height_i, newWidth, images[i].getHeight(), ImageArrays[i], 0, newWidth);
                    height_i += images[i].getHeight();
                }
            }
//            //输出想要的图片
//            String fileName = targetFile + "/" + 1 + "_" + "." + "jpg";
//            File file = new File(fileName);
//            ImageIO.write(ImageNew, "PNG", file);
            return ImageNew;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 旋转图片为指定角度  同时旋转宽高*
     *
     * @param bufferedimage * 目标图像 *
     * @param degree        * 旋转角度 *
     * @return
     */
    public static void rotateImage(final BufferedImage bufferedimage, final int degree, String targetFile, List list) throws Exception {
        int w= bufferedimage.getWidth(); // 得到图片宽度。
        int h= bufferedimage.getHeight();// 得到图片高度。
        BufferedImage img;// 空的图片。

        int x = (h / 2) - (bufferedimage.getWidth() / 2);// 确定原点坐标
        int y = (w / 2) - (bufferedimage.getHeight() / 2);
        AffineTransform at = new AffineTransform();
        at.rotate(Math.toRadians(degree), h / 2, w / 2);// 旋转图象
        at.translate(x, y);
        AffineTransformOp op = new AffineTransformOp(at,
                AffineTransformOp.TYPE_BICUBIC);
        img = new BufferedImage(h, w,bufferedimage.getType());
        img = op.filter(bufferedimage, img);
        File tmp = list.get(0);
        int suffixIndex = tmp.getName().lastIndexOf(".");
        String suffix = tmp.getName().substring(suffixIndex + 1);
        String name = tmp.getName().substring(0, suffixIndex);
        String fileName = targetFile + "/"  + name + "." + suffix;
        File file = new File(fileName);
        ImageIO.write(img, "PNG", file);
    }
}

你可能感兴趣的:(java实现图片裁剪,旋转,拼接)