Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
weispring opened this issue Aug 14, 2019 · 3 comments
Open

Comments

@weispring
Copy link

No description provided.

@liuyueyi
Copy link
Owner

老哥,提供一个case

@weispring
Copy link
Author

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

@weispring
Copy link
Author

@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();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants