liuyueyi/quick-media

图片合成比较多的时候, BufferedImage会有内存溢出问题

weispring opened this issue · 3 comments

图片合成比较多的时候, BufferedImage会有内存溢出问题

老哥,提供一个case

thanks!比如我现在需要合成图片由三个logo和三个qrCode 组成,就会占用大量内存,此时如果由并发就不行了

@test
public void test() throws IOException {
MemoryUtil.currentMemory();;
BufferedImage logo = ImageLoadUtil.getImageByPath("logo.jpg");
BufferedImage qrCode = ImageLoadUtil.getImageByPath("QrCode.jpg");

    BufferedImage logo1 = ImageLoadUtil.getImageByPath("logo.jpg");
    BufferedImage qrCode1 = ImageLoadUtil.getImageByPath("QrCode.jpg");
    MemoryUtil.currentMemory();;
    List<BufferedImage> images = new ArrayList<>();
    images.add(logo);
    images.add(qrCode);
    images.add(logo);
    images.add(qrCode);
    images.add(logo);
    images.add(qrCode);
    MemoryUtil.currentMemory();;
    int y = 0;
    int w = 0;
    List<IMergeCell> list = new ArrayList<>();
    for (BufferedImage image : images){
        IMergeCell cell = QrCodeCardTemplateBuilder.buildImg(image , y);
        y = y + image.getHeight();
        list.add(cell);
        w = image.getWidth() > w ? image.getWidth() : w;
    }
    MemoryUtil.currentMemory();;
    BufferedImage bg = ImgMergeWrapper.merge(list, w, y);
    MemoryUtil.currentMemory();;
    try {
        ImageIO.write(bg, "jpg", new File("./merge.jpg"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    MemoryUtil.currentMemory();;
}

public static ImgCell buildImg(BufferedImage logo,int y) {
// logo
logo = ImageOperateUtil.makeRoundImg(logo, false, null);
return ImgCell.builder()
.img(logo)
.x(0)
.y(y)
.w(logo.getWidth())
.h(logo.getHeight())
.build();
}