通过图片demo生成随机图片

发布于:2024-06-14 ⋅ 阅读:(86) ⋅ 点赞:(0)

通过图片demo生成随机图片

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

public class ImageCollageGenerator {

    public static void main(String[] args) {
        // 指定原始图片文件夹和目标文件夹
        String sourceFolder = "E:\\codes\\sample\\javatest\\user\\png";
        String destinationFolder = "E:\\codes\\sample\\javatest\\user\\groups";

        try {
            // 获取所有图片文件路径
            List<String> imagePaths = getAllImagePaths(sourceFolder);

            // 随机选择2-4张图片
            List<String> selectedImages = getRandomImages(imagePaths,  new Random().nextInt(3)+2);

            // 加载并拼接图片, 第二个参数为拼接图片边缘的距离,数字越大,越靠中间,1.2比较合适
            BufferedImage resultImage = combineImages(selectedImages, 1.2);

            // 保存新图片,第三个参数为图片的名字
            saveImage(resultImage, destinationFolder, "10.jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 获取指定文件夹下的所有图片文件路径
    private static List<String> getAllImagePaths(String folderPath) {
        List<String> imagePaths = new ArrayList<>();
        File folder = new File(folderPath);
        File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile() && isImageFile(file.getName())) {
                    imagePaths.add(file.getAbsolutePath());
                }
            }
        }
        return imagePaths;
    }

    // 判断文件是否为图片
    private static boolean isImageFile(String fileName) {
        String[] imageExtensions = { "jpg", "jpeg", "png", "gif", "bmp" };
        for (String extension : imageExtensions) {
            if (fileName.toLowerCase().endsWith(extension)) {
                return true;
            }
        }
        return false;
    }

    // 从列表中随机选择指定数量的图片
    private static List<String> getRandomImages(List<String> imagePaths, int count) {
        Random random = new Random();
        List<String> selectedImages = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            int randomIndex = random.nextInt(imagePaths.size());
            System.out.println(randomIndex);
            selectedImages.add(imagePaths.get(randomIndex));
            System.out.println(imagePaths.get(randomIndex));
        }
        return selectedImages;
    }

    // 加载并拼接图片
    private static BufferedImage combineImages(List<String> imagePaths, double distance) throws IOException {
        // 加载图片
        List<BufferedImage> images = new ArrayList<>();
        for (String path : imagePaths) {
            BufferedImage img = ImageIO.read(new File(path));
            images.add(img);
        }

        // 创建新图片
        int maxWidth = images.get(0).getWidth();
        int maxHeight = images.get(0).getHeight();
        for (BufferedImage img : images) {
            maxWidth = Math.max(maxWidth, img.getWidth());
            maxHeight = Math.max(maxHeight, img.getHeight());
        }
        BufferedImage resultImage = new BufferedImage(maxWidth * 2, maxHeight * 2, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = resultImage.createGraphics();

        // 绘制背景色
        Random random = new Random();
        Color backgroundColor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
        g2d.setColor(backgroundColor);
        g2d.fillRect(0, 0, maxWidth * 2, maxHeight * 2);

        // 随机旋转并拼接图片

        for (BufferedImage img : images) {
            //double pic_rotation = Math.toRadians(random.nextInt(36))*10; // 每强图片的随机旋转角度
            double pic_rotation = Math.toRadians(120);
            g2d.rotate(pic_rotation, maxWidth, maxHeight);
            g2d.drawImage(img, (int)(maxWidth/distance), (int)(maxHeight/distance), null);
            //x += maxWidth;
        }

        g2d.dispose();

        return resultImage;
    }

    // 保存图片
    private static void saveImage(BufferedImage image, String folderPath, String imgName) throws IOException {
        File outputFolder = new File(folderPath);
        if (!outputFolder.exists()) {
            outputFolder.mkdirs();
        }
        File outputFile = new File(outputFolder, imgName);
        ImageIO.write(image, "jpg", outputFile);
    }
}

网站公告

今日签到

点亮在社区的每一天
去签到