创建巨大的BufferedImage

时间:2012-02-01 01:48:58

标签: java image swing awt

我无法创建巨大的BufferedImage(缺少内存不是问题)。有没有人有任何想法?

1。 new BufferedImage(10000,1000000,BufferedImage.TYPE_3BYTE_BGR);

Exception in thread "main" java.lang.NegativeArraySizeException
    at java.awt.image.DataBufferByte.<init>(DataBufferByte.java:42)
    at java.awt.image.Raster.createInterleavedRaster(Raster.java:253)
    at java.awt.image.BufferedImage.<init>(BufferedImage.java:368)

2。 new BufferedImage(10000,1000000,BufferedImage.TYPE_INT_RGB);

Exception in thread "main" java.lang.IllegalArgumentException: Dimensions (width=10000 height=1000000) are too large    
at java.awt.image.SampleModel.<init>(SampleModel.java:112)
    at java.awt.image.SinglePixelPackedSampleModel.<init>(SinglePixelPackedSampleModel.java:124)
    at java.awt.image.Raster.createPackedRaster(Raster.java:770)
    at java.awt.image.Raster.createPackedRaster(Raster.java:466)
    at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015)
    at java.awt.image.BufferedImage.<init>(BufferedImage.java:315)

1 个答案:

答案 0 :(得分:15)

我认为这是Raster课程的限制。 Width * Height需要小于Integer.MAX_VALUE

http://docs.oracle.com/javase/7/docs/api/java/awt/image/Raster.html

作为一种解决方法,我可能会将我的BufferedImage分成宽度和高度均小于Integer.MAX_VALUE的平方根的部分,因此最大值为46,340x46,340。

更新:看起来http://code.google.com/p/pngj/处的 PNGJ 库就是为此而创建的。