trying to get an array of every pixel of an image, why is it not working???

File img = new File("daisy.png");
BufferedImage buffImg = new BufferedImage(570, 760, BufferedImage.TYPE_INT_ARGB);
try {
buffImg = ImageIO.read(img);
}
catch (IOException e) { }

int[][] pixelArray = new int[570][760];

pixelArray = get2DPixelArraySlow(buffImg);
File img = new File("daisy.png");
BufferedImage buffImg = new BufferedImage(570, 760, BufferedImage.TYPE_INT_ARGB);
try {
buffImg = ImageIO.read(img);
}
catch (IOException e) { }

int[][] pixelArray = new int[570][760];

pixelArray = get2DPixelArraySlow(buffImg);
public static int[][] get2DPixelArraySlow(BufferedImage sampleImage) {
int width = sampleImage.getWidth();
int height = sampleImage.getHeight();
int[][] result = new int[height][width];

for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
// System.out.println(row);
// System.out.println(col);
result[row][col] = sampleImage.getRGB(col, row);
}
}

return result;
}
public static int[][] get2DPixelArraySlow(BufferedImage sampleImage) {
int width = sampleImage.getWidth();
int height = sampleImage.getHeight();
int[][] result = new int[height][width];

for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
// System.out.println(row);
// System.out.println(col);
result[row][col] = sampleImage.getRGB(col, row);
}
}

return result;
}
when i print pixelArray its just many many 0's. the image file daisy.png is in the same folder as app.java
32 Replies
JavaBot
JavaBot5mo ago
This post has been reserved for your question.
Hey @mochatitan! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st5mo ago
doN't catch and ignore exceptions exceptions have important information on what went wrong
mochatitan
mochatitanOP5mo ago
[Ljava.lang.StackTraceElement;@2d8e6db6 what does that mean Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 570 out of bounds for length 570 this message wont stop even when i increase array size or decrease for loop
Can't read input file!
Can't read input file!
thanks ill try to figure that out
JavaBot
JavaBot5mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
mochatitan
mochatitanOP5mo ago
No description
mochatitan
mochatitanOP5mo ago
not quite sure why it cant read it though @dan1st | Daniel maybe the png file is corrupted or something?
dan1st
dan1st5mo ago
that means you did System.out.println(e.getStackTrace()); instead of e.printStackTrace() or similar this means you are accessing a nonexisting element
mochatitan
mochatitanOP5mo ago
alright
File img = new File("daisy.png");
BufferedImage buffImg = new BufferedImage(570, 760, BufferedImage.TYPE_INT_ARGB);
buffImg = ImageIO.read(img);
File img = new File("daisy.png");
BufferedImage buffImg = new BufferedImage(570, 760, BufferedImage.TYPE_INT_ARGB);
buffImg = ImageIO.read(img);
the error came from the last line (the only one in the try catch) im wondering if ther was some issue converting it into buffered image
dan1st
dan1st5mo ago
What is your current code and what is the stack trace?
mochatitan
mochatitanOP5mo ago
No description
mochatitan
mochatitanOP5mo ago
@dan1st | Daniel
dan1st
dan1st5mo ago
Where exactly is the daisy.png file? Where exactly is it located? Is it a valid PNG file?
mochatitan
mochatitanOP5mo ago
No description
dan1st
dan1st5mo ago
Is there more text in the stack trace?
mochatitan
mochatitanOP5mo ago
no that is all
dan1st
dan1st5mo ago
the file would need to be outside the src folder
mochatitan
mochatitanOP5mo ago
ah
dan1st
dan1st5mo ago
or you need to access it as a resource
mochatitan
mochatitanOP5mo ago
then do i need to do ../daisy.png or something
dan1st
dan1st5mo ago
File is for external files, not things that are part of the program
mochatitan
mochatitanOP5mo ago
ah how would i access it now that it is out of the src directory
dan1st
dan1st5mo ago
Stack Overflow
Reading a resource file from within jar
I would like to read a resource from within my jar like so: File file; file = new File(getClass().getResource("/file.txt").toURI()); BufferedReader reader = new BufferedReader(new FileRea...
dan1st
dan1st5mo ago
Stack Overflow
How do I load a file from resource folder?
My project has the following structure: /src/main/java/ /src/main/resources/ /src/test/java/ /src/test/resources/ I have a file in /src/test/resources/test.csv and I want to load the file from a ...
mochatitan
mochatitanOP5mo ago
thank you
JavaBot
JavaBot5mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
mochatitan
mochatitanOP5mo ago
im not using any build tools or eclipse im just rawdogging it in vscode i should probably restart in a proper environment
dan1st
dan1st5mo ago
doesn't matter for this issue if it's in the src folder, you can do ImageIO.read(App.class.getResource("daisy.png"));
mochatitan
mochatitanOP5mo ago
hydra
mochatitan
mochatitanOP5mo ago
one bug fixes 2 another one appears
No description
mochatitan
mochatitanOP5mo ago
ill try to take it from here though are you cool if i ping you if i cant figure it out by tomorrow i have 3 hours of freetime to try
dan1st
dan1st5mo ago
yeah, I'm ok with that If this post here is marked dormant, it will be reopened if you just send another message here
JavaBot
JavaBot5mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?