Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

java - creating huge BufferedImage

I'm unable to create a huge BufferedImage (lack of memory is not the problem). Does anyone have any ideas?

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I believe this is a limitation of the Raster class. Width * Height needs to be less than Integer.MAX_VALUE

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

As a work around I'd probably split my BufferedImage into sections where width and height are both less than the square root of Integer.MAX_VALUE, so 46,340x46,340 max.

UPDATE: It looks like the PNGJ Library at http://code.google.com/p/pngj/ was created for this purpose.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...