使用java / BufferedImage / heap空间绘制大图像

时间:2017-12-28 07:26:54

标签: java bufferedimage

我试图在大图像上绘制指针[约〜9984x9472 (94.6MP)],例如:worldmap image。到目前为止,我已经尝试了java BufferedImageGraphics2D来做到这一点。
现在的问题是堆空间。从我到目前为止所阅读的内容BufferedImage操纵内存中的图像,对于上面所需的图像空间约为9984x9472x4 = 378,273,792 byte。那真是太糟糕了。我尝试使用-Xms512m -Xmx1024m运行以下代码,成功-Xms256m -Xmx512m 失败。所有建议都指向增加堆空间。但是我不确定在用户正在查看实时绘制的这类图像的5-10个网页应用程序的现实情况。还有一个并发用户的问题!

绘制图片的代码

private void drawOnImage() {
    try {
        BufferedImage originalImage = ImageIO.read(new File(this.getClass().getResource("/largeImage.jpg").getFile()));
        BufferedImage markerR = ImageIO.read(new File(this.getClass().getResource("/marker/R.png").getFile()));
        BufferedImage markerG = ImageIO.read(new File(this.getClass().getResource("/marker/G.png").getFile()));
        BufferedImage markerB = ImageIO.read(new File(this.getClass().getResource("/marker/B.png").getFile()));

        BufferedImage newImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D newImageG2d = newImage.createGraphics();
        newImageG2d.drawImage(originalImage, 0, 0, null);

        newImageG2d.drawImage(markerR, 0, originalImage.getHeight() / 2, null);
        newImageG2d.drawImage(markerG, (originalImage.getWidth() / 2 - markerG.getWidth() / 2), originalImage.getHeight() / 2, null);
        newImageG2d.drawImage(markerB, (originalImage.getWidth() - markerG.getWidth()), originalImage.getHeight() / 2, null);

        newImageG2d.dispose();

        File outputFile = File.createTempFile("newImage", ".jpg");
        FileOutputStream outputFileStream = new FileOutputStream(outputFile);
        ImageIO.write(newImage, "jpg", outputFileStream);
        outputFileStream.flush();

        System.out.println("Execution complete. Output file located: " + outputFile.getAbsolutePath());
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

如果有人对git感兴趣:https://github.com/mamunsrdr/pwimage.git

现在我的问题:要做这种工作BufferedImage看起来不是正确的候选人,或者是这样?或者是否有任何库可以在不分配大量内存且占用空间小的情况下执行此操作,因为执行速度可能较慢?

0 个答案:

没有答案