-3

I want to read the image selected by user using JFileChooser and then be able to get the color Channels(R,G,B) and the width and height of the image.

Is this the right approach to read the selected image file.

File im1 = new File(chooser.getSelectedFile(), null);       
BufferedImage buff =ImageIO.read(im1);

Or is there a better way to read the image file in order to get the values of its separate color channels and get its separate values.

Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
Deepak kumar Jha
  • 524
  • 5
  • 16

2 Answers2

2

Your code looks okay. Just keep going with width, height and RGB.

File im1 = chooser.getSelectedFile();    
BufferedImage buff = ImageIO.read(im1);

if (buff != null) {
    System.out.println(buff.getWidth() + " " + buff.getHeight());
    System.out.println(buff.getRGB(0, 0));
}
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
0

I haven't found any 'better' way to load the image so I believe you're doing it right.

To answer your whole question, here's an example how to get specific color channels out of the image.

Color c = new Color(image.getRGB());
int red = c.getRed();
int green = c.getGreen();
int blue = c.getBlue();
Lenymm
  • 879
  • 1
  • 6
  • 27