J
jimgardener
hi
i am getting a BufferedImage and i need to extract the r,g,b
components of each pixel.I will not know which type the image is until
runtime(whether type 5(TYPE_3BYTE_BGR),type 1(TYPE_INT_RGB) or type
10(TYPE_BYTE_GRAY).i can get the pixel value using
BufferedImage.getRGB(...) as an int[].I want to know if i can get the
components the following way for any type of image.
BufferedImage img=ImageIO.read(new File("mypic.jpg"));
int wd=img.getWidth();
int ht=img.getHeight();
int[] rgbArray=new int[wd*ht];
img.getRGB(0, 0,wd,ht,rgbArray,0,wd);
int pix=rgbArray[10][12];//some pixel
int alpha=(pix >> 24) & 0x0ff;
int r=(pix>>16)& 0x0ff;
int g=(pix>>8)& 0x0ff;
int b=pix & 0x0ff;
System.out.println("r="+r+" g="+g+" b="+b);
is this valid for any type of image?or is there any byte order issue
in this?
if anyone can point out errors /alternatives it would be helpful
thanks
jim
i am getting a BufferedImage and i need to extract the r,g,b
components of each pixel.I will not know which type the image is until
runtime(whether type 5(TYPE_3BYTE_BGR),type 1(TYPE_INT_RGB) or type
10(TYPE_BYTE_GRAY).i can get the pixel value using
BufferedImage.getRGB(...) as an int[].I want to know if i can get the
components the following way for any type of image.
BufferedImage img=ImageIO.read(new File("mypic.jpg"));
int wd=img.getWidth();
int ht=img.getHeight();
int[] rgbArray=new int[wd*ht];
img.getRGB(0, 0,wd,ht,rgbArray,0,wd);
int pix=rgbArray[10][12];//some pixel
int alpha=(pix >> 24) & 0x0ff;
int r=(pix>>16)& 0x0ff;
int g=(pix>>8)& 0x0ff;
int b=pix & 0x0ff;
System.out.println("r="+r+" g="+g+" b="+b);
is this valid for any type of image?or is there any byte order issue
in this?
if anyone can point out errors /alternatives it would be helpful
thanks
jim