gpt4 book ai didi

java - 从像素值重新创建整数时出现 ArrayIndexOutOfBounds 异常

转载 作者:行者123 更新时间:2023-12-01 11:45:16 25 4
gpt4 key购买 nike

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

异常(exception)情况是:

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

异常(exception)在行

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

可能出了什么问题?

最佳答案

您似乎有一个拼写错误:

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++)

关于java - 从像素值重新创建整数时出现 ArrayIndexOutOfBounds 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29193128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com