试图创建一个bufferedimages数组

时间:2012-03-10 17:23:20

标签: java image

我正在尝试创建一个缓冲图像数组,其中包含较大图像的裁剪图像,但我一直收到此错误:

Exception in thread "main" java.awt.image.RasterFormatException: (y + height) is outside of Raster
at sun.awt.image.ByteInterleavedRaster.createWritableChild(ByteInterleavedRaster.java:1233)
at java.awt.image.BufferedImage.getSubimage(BufferedImage.java:1156)
at com.search.visual.ImageChunks.createImageArrays(ImageChunks.java:95)
at com.search.visual.ImageChunks.calculateChunks(ImageChunks.java:122)
at com.search.visual.ImageChunks.<init>(ImageChunks.java:59)
at com.search.visual.Driver.main(Driver.java:10)

这是我正在使用的代码:

private BufferedImage[][]  createImageArrays()
{
    BufferedImage[][] bitmapsArray = new BufferedImage[3][3];
    //bitmap bmap = bitmapfactory.decoderesource(getresources(), image);
    //bufferedimage bmapscaled = bufferedimage.createscaledbitmap(bmp, 240, 240, true);


    //Image imageMap = scaledImage;
    bitmapsArray[0][0] = bmp.getSubimage(0,0, 80, 80);
    bitmapsArray[0][1] = bmp.getSubimage(80,0, 80, 80);
    bitmapsArray[0][2] = bmp.getSubimage( 160, 0, 80, 80);
    bitmapsArray[1][0] = bmp.getSubimage( 0, 80, 80, 80);
    bitmapsArray[1][1] = bmp.getSubimage( 80, 80, 80, 80);
    bitmapsArray[1][2] = bmp.getSubimage( 160, 80, 80, 80);
    bitmapsArray[2][0] = bmp.getSubimage( 0, 160, 80, 80);
    bitmapsArray[2][1] = bmp.getSubimage( 80, 160, 80, 80);
    bitmapsArray[2][2] = bmp.getSubimage( 160, 160, 80, 80);

    return bitmapsArray;


}


/*
 *  1. split the complete image/bitmaps into several smaller images/bitmaps 
 *  2. calculate the average RGB for each of these smaller images/bitmaps
 *  3. fill up the RGB[][] rgbList
 *  
 */

private void calculateChunks()
{

    // 1. split the complete image/bitmaps into several smaller images/bitmaps 

    // this is a real con, as I've only implemented it as 3x3 of of 80x80
    //BufferedImage[][] bmpList = createImageArrays();


    // 2. calculate the average RGB for each of these smaller images/bitmaps
    BufferedImage[][] bmpList = createImageArrays();
    System.out.print(bmpList);
    RGB rgb_temp = null;
    BufferedImage bmp_temp = null;

    int rgb = 0;
    int red = 0;
    int green = 0;
    int blue = 0;

    int running_total_red = 0;
    int running_total_blue = 0;
    int running_total_green = 0;

    RGB temp_rgb = null;

    for(int I=0; I < 3; I++)
        for(int j=0; j < 3; j++)
        {
            bmp_temp = bmpList[I][j];

            red = 0;
            green = 0;
            blue = 0;

            for(int k=0; k < 80; k++)
                for(int l=0; l < 80; l++)
                {

                    temp_rgb = getPixelRGBvalues( bmp_temp.getRGB(k, l) );

/*                      rgb    = bmp_temp.getPixel(k, l);   //gets the values of the pixel at point k and l
                    red    += (rgb & 0x00ff0000) >> 16; //stores only the red values of rgb to red
                    green  += (rgb & 0x0000ff00) >> 8;  //stores only the green values of rgb to green 
                    blue   += rgb & 0x000000ff;         //stores only the blue values of rgb to blue                                
*/                  
                    running_total_red += temp_rgb.getR();
                    running_total_blue += temp_rgb.getB();
                    running_total_green += temp_rgb.getG();


                }



            // normalise these values


            red     = running_total_red/ (80*80);
            green   = running_total_green/ (80*80);
            blue    = running_total_blue/ (80*80);




            // 3. fill up the RGB[][] rgbList
            rgbList[I][j] = new RGB(red, green, blue);
        }
}

为什么会这种情况继续发生?

1 个答案:

答案 0 :(得分:4)

您的源图像小于240 * 240像素。您尝试获取的子图像不在原始图像中。