【问题标题】:spliting of an image using java [duplicate]使用java分割图像[重复]
【发布时间】:2016-02-13 14:56:55
【问题描述】:

实际上,我正在使用 JAVA 拆分一个 600x700 .jpg 文件。这里方法的参数宽度和高度与图像的宽度和高度完全相同。但是在执行过程中,我遇到了一个与堆内存相关的异常。

 /*
 * this method will split the secureImage file
 */
private BufferedImage[] getsecureFragments(int width, int height,
        File secureFile)throws IOException {

    FileInputStream secureFileStream = new FileInputStream(secureFile);  
    BufferedImage secureimage = ImageIO.read(secureFileStream);
    int rows    = width;
    int columns = height;
    int chunks = rows *columns;
    int chunkWidth = width/width;
    int chunkHeight=height/height;

    int count = 0;  
    BufferedImage fragmentImgs[] = new BufferedImage[chunks];
    System.out.println(fragmentImgs.length);
    for (int x = 0; x < rows; x++) {  
        for (int y = 0; y < columns; y++) {  
            //Initialize the image array with image chunks  
            fragmentImgs[count] = new BufferedImage(chunkWidth, chunkHeight, secureimage.getType());  

            // draws the image chunk  
            Graphics2D grSecure = fragmentImgs[count++].createGraphics();  
            grSecure.drawImage(secureimage, 0, 0, chunkWidth, chunkHeight, chunkWidth * y, chunkHeight * x, chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, null);  
            grSecure.dispose();  
        }  
    } 

    System.out.println("Splitting done");  

    //writing mini images into image files for test purpose  
    for (int i = 0; i < fragmentImgs.length; i++) {  
        ImageIO.write(fragmentImgs[i], "jpg", new File("F://uploads//fragmentImgs" + i + ".jpg"));  
    }  
    System.out.println("Mini images created");
    return fragmentImgs;
}//end of method getsecureFragments

线程“http-bio-8080-exec-9”java.lang.OutOfMemoryError 中的异常:Java 堆空间

这是因为我的逻辑缺乏?或者我是否需要了解更多关于 JAVA 堆内存的信息。

【问题讨论】:

  • 你能发布错误的堆栈跟踪吗?
  • 是的,我弄错了。谢谢
  • 您在内存中同时打开了 420 000 张图像......我并不惊讶它会耗尽内存。另外,看看BufferedImage.getSubimage——也许有用。

标签: java


【解决方案1】:

您可以运行具有更多堆空间的 JVM,例如启动 JVM 时添加 -Xmx 参数。

您还可以重写代码以复制一个部分,将其写入磁盘,丢弃它,然后执行下一个部分。目前,程序中的逻辑会生成所有的部分,然后保存它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-17
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 2013-01-27
    相关资源
    最近更新 更多