ArrayIndexOutOfBounds从像素值重新创建整数时的异常

时间:2015-03-22 10:13:44

标签: java image-processing indexoutofboundsexception

我正在尝试将图像中的红色,绿色和蓝色值分开,并对阵列进行一些排列。在进行排列之后,我尝试写入图像,但在将像素值写入整数时发现异常。

例外是:

Exception in thread "AWT-EventQueue-0"   java.lang.ArrayIndexOutOfBoundsException: 272

    at encryption.core.receiveFramePath(core.java:203)

    at encryption.fny.jButton3ActionPerformed(fny.java:657)

    at encryption.fny.access$900(fny.java:14)

java代码是:

    public class core {

    Random rand = new Random();

    public void receiveFramePath(String[] fpath, String key)
    {
        System.out.println("Number of frames to be processed are " + fpath.length);
        System.out.println("Key received is " + key);
        int len;

        len = key.length();
        //String part1, part2;
        String part1 = key.substring(0, 2);
        String part2 = key.substring(2, len);

        System.out.println("Message to be splitted is " + key);
        System.out.println("The parts are " + part1 + " and " + part2);

        int fC ;// this is frmaeCount
        fC = fpath.length;
        System.out.println("  No of strings to be processed are " + fC);
        BufferedImage[] img = new BufferedImage[fC];
        int i;// to run the for loop
        for( i = 0; i < fC; i++ )
        {    
                    // Code to read the image pixel values

                    File f = new File(fpath[i]);
                    try {
                                img[i] = ImageIO.read(f);
                        }
                    catch(Exception e)
                        {
                                e.printStackTrace();
                        }
                    int[] RGBarray = null;
                    int c = 0;
                    int r = 0;
                    int [][] alphaPixels = null;
                    int [][] redPixels = null;
                    int [][] greenPixels = null;
                    int [][] bluePixels =null;

                    c = img[i].getWidth();
                    r = img[i].getHeight();

                    //System.out.println(" Width of the image is " + c + " The height of the image is " + r);
                    RGBarray = img[i].getRGB(0,0,c,r,null,0,c);   

                    alphaPixels = new int [r][c];
                    redPixels = new int [r][c];
                    greenPixels = new int [r][c];
                    bluePixels = new int [r][c];
                    int ii = 0;// to run inside seperating a loop

                    for(int row=0; row<r; row++)
                    {
                       for(int col=0; col<c; col++)
                       {
                      //    alphaPixels[row][col] = ((RGBarray[ii]>>24)&0xff);
                          redPixels[row][col] = ((RGBarray[ii]>>16)&0xff);
                          greenPixels[row][col] = ((RGBarray[ii]>>8)&0xff);
                          bluePixels[row][col] = (RGBarray[ii]&0xff);
                          ii++;
                       }
                    }
                    // code to read the image pixel values ends here

                    // Fisher alg starts here
                    // logic for permutation on red pixels
                    for ( int ro = 0; ro < r ; ro++)
                    {
                       int[] arr = new int[c];

                       for (int co = 0; co < c; co++)
                       {
                           arr[co] = redPixels[ro][co];
                       }

                       int max = arr.length - 1;
                       int min = 0;
                       int rnum;
                       int larr = arr.length - 1;
                       int[] parr = new int[arr.length];// to store the permuted array 
                       int flag = 0;// 'flag' to flag the recurring number
                       int plen = parr.length - 1;

                       for(int z = 0 ; z < arr.length ; z++)
                       {
                            rnum = (rand.nextInt((max-min)+1) + min);// roll for the random number
                            parr[plen] = arr[rnum];
                            arr[rnum] = arr[larr];

                            larr--;// to reduce the size of the original array
                            plen--;// to make the parr to act like a stack
                            max--;
                       }
                       for(int z = 0; z<arr.length;z++ )
                       {
                           redPixels[ro][z] = parr[z];
                       }
                    } 
                    // &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& redPixels permuatation ends here
                    // &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& redPixels permuatation ends here
                    // &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& redPixels permuatation ends here

                    // logic for permutation on GREEEEN pixels
                    // logic for permutation on GREEEEN pixels
                    // logic for permutation on GREEEEN pixels

                    for ( int ro = 0; ro < r ; ro++)
                    {
                       int[] arr = new int[c];

                       for (int co = 0; co < c; co++)
                       {
                           arr[co] = greenPixels[ro][co];
                       }

                       int max = arr.length - 1;
                       int min = 0;
                       int rnum;
                       int larr = arr.length - 1;
                       int[] parr = new int[arr.length];// to store the permuted array 
                       int flag = 0;// 'flag' to flag the recurring number
                       int plen = parr.length - 1;

                       for(int z = 0 ; z < arr.length ; z++)
                       {
                            rnum = (rand.nextInt((max-min)+1) + min);// roll for the random number
                            parr[plen] = arr[rnum];
                            arr[rnum] = arr[larr];

                            larr--;// to reduce the size of the original array
                            plen--;// to make the parr to act like a stack
                            max--;
                       }
                       for(int z = 0; z<arr.length;z++ )
                       {
                           greenPixels[ro][z] = parr[z];
                       }
                    } 
                    // &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& greenPixels permuatation ends here
                    // &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& greenPixels permuatation ends here
                    // &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& greenPixels permuatation ends here

                    for ( int ro = 0; ro < r ; ro++)
                    {
                       int[] arr = new int[c];

                       for (int co = 0; co < c; co++)
                       {
                           arr[co] = bluePixels[ro][co];
                       }

                       int max = arr.length - 1;
                       int min = 0;
                       int rnum;
                       int larr = arr.length - 1;
                       int[] parr = new int[arr.length];// to store the permuted array 
                       int flag = 0;// 'flag' to flag the recurring number
                       int plen = parr.length - 1;

                       for(int z = 0 ; z < arr.length ; z++)
                       {
                            rnum = (rand.nextInt((max-min)+1) + min);// roll for the random number
                            parr[plen] = arr[rnum];
                            arr[rnum] = arr[larr];

                            larr--;// to reduce the size of the original array
                            plen--;// to make the parr to act like a stack
                            max--;
                       }
                       for(int z = 0; z<arr.length;z++ )
                       {
                           bluePixels[ro][z] = parr[z];
                       }
                    }
                    // &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& bluePixels permuatation ends here
                    // &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& bluePixels permuatation ends here
                    // &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& bluePixels permuatation ends here

                    int rgba;
                    for(int row=0; row<r; row++)
                    {
                        for(int col=0; col<r; col++)
                        {
                            rgba = (alphaPixels[row][col] & 0xff) << 24 | (redPixels[row][col] & 0xff) << 16 | (greenPixels[row][col] & 0xff) << 8 | (bluePixels[row][col] & 0xff);
                            img[i].setRGB(col, row, rgba);
                        }
                    }
                    try{
                        ImageIO.write(img[i], "png", new File("D:/Projects/Problem domains/Ashwini/encrypted/encr_"+i+".png"));
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
            }// for loop
    }// method ends
}// class ends

例外是在行

 rgba = (alphaPixels[row][col] & 0xff) << 24 | (redPixels[row][col] & 0xff) << 16 | (greenPixels[row][col] & 0xff) << 8 | (bluePixels[row][col] & 0xff);

可能出了什么问题?

1 个答案:

答案 0 :(得分:1)

看起来你有一个错字:

for(int row=0; row<r; row++) {
    for(int col=0; col<r; col++)
                       ^

应该是

for(int row=0; row<r; row++) {
    for(int col=0; col<c; col++)