1

Java 2D を使用して作成できる画像の最大サイズは?

Windows 7 Pro 64 ビット OS と JDK 1.6.0_33、64 ビット バージョンを使用しています。最大 5 MB のサイズの BufferedImage を作成できます。それを超えると、OutOfMemoryError が発生します。

Java 2D または JAI を使用してより大きなサイズの画像を作成する方法を教えてください。

これが私の試みです。

import java.awt.Graphics2D;    
import java.awt.image.BufferedImage;     
import java.io.File;    
import javax.imageio.ImageIO;    

public class CreateBiggerImage
{
private String fileName = "images/107.gif";
private String outputFileName = "images/107-Output.gif";

public CreateBiggerImage()
{
    try
    {
        BufferedImage image = readImage(fileName);
        ImageIO.write(createImage(image, 9050, 9050), "GIF", new File(System.getProperty("user.dir"), outputFileName));
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
}

private BufferedImage readImage(String fileName) throws Exception
{
    BufferedImage image = ImageIO.read(new File(System.getProperty("user.dir"), fileName));
    return image;
}

private BufferedImage createImage(BufferedImage image, int outputWidth, int outputHeight) throws Exception
{
    int actualImageWidth = image.getWidth();
    int actualImageHeight = image.getHeight();

    BufferedImage imageOutput = new BufferedImage(outputWidth, outputHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = imageOutput.createGraphics();
    for (int width = 0; width < outputWidth; width += actualImageWidth)
    {
        for (int height = 0; height < outputHeight; height += actualImageHeight)
        {
            g2d.drawImage(image, width, height, null);
        }
    }
    g2d.dispose();

    return imageOutput;
}

public static void main(String[] args)
{
    new CreateBiggerImage();
}
}
4

1 に答える 1

2

Java 2D で作成できる画像の「​​最大サイズ」は、さまざまな要因に依存します...したがって、ここでいくつかの仮定を立てます (間違っている場合は訂正してください)。

  • 「サイズ」とは、メモリ消費量ではなく、寸法 (幅 x 高さ) を意味します。
  • 「イメージ」というと、BufferedImage

これらの仮定により、理論上の制限は(width * height * bits per pixel / bits in transfer type) == Integer.MAX_VALUE(つまり、作成できる最大の配列) で与えられます。たとえば、TYPE_INT_RGBまたはTYPE_INT_ARGBの場合、1 ピクセルあたり 32 ビットを使用し、転送タイプも 32 ビットです。TYPE_3BYTE_RGBピクセルあたり 24 ビットを使用しますが、転送タイプは 8 ビットのみであるため、最大サイズは実際には小さくなります。

理論的にはさらに大きなタイル を作成できる可能性がありますRenderedImage。または、カスタムRasterを複数のバンド (複数の配列) で使用します。

いずれにせよ、制限要因は利用可能な連続メモリになります。

これを克服するために、メモリー・マップ・ファイルを使用してイメージ・データを JVM ヒープの外部に保管するDataBuffer 実装を作成しました。これは完全に実験的なものですが、BufferedImages where の作成に成功しましたwidth * height ~= Integer.MAX_VALUE / 4。パフォーマンスは高くありませんが、特定のアプリケーションでは許容できる場合があります。

于 2013-08-27T20:24:38.053 に答える