Thursday, April 28, 2011

How to obtain pixel's rgb values in an image in android

Hello,

I am working on a little side project (program for Android platform)and I don't exactly have a problem, but I don't really know where to start. The project I am working on has to do with getting the info and manipulating a pixel's RGB values inside an image. I know this is a very basic program (not going to disclose all the information about the project), but I am a beginning android programmer.

I've already developed my algorithm, now I just need to be pushed/"knudged" in the right direction. Could anyone please help me? Any tutorials on this kind of thing or related topics?

Thanks in advance!

From stackoverflow
  • What kind of object do you have? A Bitmap? If so, how about Bitmap.getPixel(int x, int y)?

    mike : OP here, what if it isn't a bitmap? It will be a user inputted file, so it could really be any format, but the most common ones I will be working with are jpg/bmp/png.
    EboMike : Well, what is it? If it's a jpg/bmp/png, then you'll turn it into a bitmap when you load it. You'll need to provide SOME hints on what your code is going to be like.
    mike : Its just basically taking the RGB values of each individual pixel in an image and translating it to another value. Thats all. Are there any commands/libraries I could import for different file extensions? This is not an app to sell on an app store, this was just a project a friend and I are working on for fun.
    adamp : Android's `Bitmap` class is not specific to the .bmp file format, it simply represents bitmapped images in memory. The `BitmapFactory` class will happily parse a number of popular file formats into `Bitmap` objects for you. (.jpg, .png, etc.)
    mike : So what you are saying is that Android's Bitmap class essentially "converts" popular file extensions, like .jpg, into a .bmp and then you are able to use Bitmap.getPixel(int x, int y)? Is there any quality loss?
    EboMike : I think you're still mixing up "Bitmap" and "BMP". "Bitmap" is a generic Android class that contains an image. It's a 2D image based on pixels (versus something curve-and-line based, like SVG), hence Bitmap. How you create this Bitmap is a different issue. You could do it from scratch, or you could load a BMP, JPG, PNG file - as Adam pointed out, the BitmapFactory class has some static members that let you do this with one line of code. And there's no quality loss in decoding. (Obviously, if it's a JPG, there had been quality loss when it was created in the first place.)
    mike : So basically what you're saying is, Bitmap is a class and all I have to do is import that class and can use the .getPixel(int, int) command to basically get a certain pixel at point (x,y) on any popular file format?! Wow, I thought it would be much harder than that. Thanks a lot!
    EboMike : It is as easy as that. Android is awesome, after all :) (Btw, not to be pushy, but if it works out for you, please consider marking the answer as "best answer" to give both of us a better score)
    mike : Ok I checked the green arrow. Thanks for all the help, Ebomike and adamp.

0 comments:

Post a Comment