Get the FULL version

Android: How to return RGB values from an image file

Android: How to return RGB values from an image file thumbnail

Surprisingly, it’s quite easy to get the RGB value from an image file in Android. It’s certainly a lot easier than retrieving the pixel values from the Camera Preview. All that is required is to load the image file into a Bitmap object and them call the getPixel() method from the loaded bitmap.

It’s also possible to call this method to create an array of alpha and RGB values (ARGB) without calling the copyPixelsToBuffer() method, avoiding the use of buffers in Android which make things more complicated than it should be. As an example, the following code prints the color values of the pixels from the 4 corners of the image in LogCat and creates a RGBA array that stores each pixel color value:

  1. package fortyonepost.com.iapa;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.BitmapFactory;  
  6. import android.os.Bundle;  
  7. import android.util.Log;  
  8.   
  9. public class ImageAsPixelArray extends Activity  
  10. {  
  11.     //a Bitmap that will act as a handle to the image  
  12.     private Bitmap bmp;  
  13.   
  14.     //an integer array that will store ARGB pixel values  
  15.     private int[][] rgbValues;  
  16.   
  17.     /** Called when the activity is first created. */  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState)  
  20.     {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.   
  24.         //load the image and use the bmp object to access it  
  25.         bmp = BitmapFactory.decodeResource(getResources(), R.drawable.four_colors);  
  26.   
  27.         //define the array size  
  28.         rgbValues = new int[bmp.getWidth()][bmp.getHeight()];  
  29.   
  30.         //Print in LogCat's console each of one the RGB and alpha values from the 4 corners of the image  
  31.         //Top Left  
  32.         Log.i("Pixel Value""Top Left pixel: " + Integer.toHexString(bmp.getPixel(00)));  
  33.         //Top Right  
  34.         Log.i("Pixel Value""Top Right pixel: " + Integer.toHexString(bmp.getPixel(310)));  
  35.         //Bottom Left  
  36.         Log.i("Pixel Value""Bottom Left pixel: " + Integer.toHexString(bmp.getPixel(031)));  
  37.         //Bottom Right  
  38.         Log.i("Pixel Value""Bottom Right pixel: " + Integer.toHexString(bmp.getPixel(3131)));  
  39.   
  40.         //get the ARGB value from each pixel of the image and store it into the array  
  41.         for(int i=0; i < bmp.getWidth(); i++)  
  42.         {  
  43.             for(int j=0; j < bmp.getHeight(); j++)  
  44.             {  
  45.                 //This is a great opportunity to filter the ARGB values  
  46.                 rgbValues[i][j] = bmp.getPixel(i, j);  
  47.             }  
  48.         }  
  49.           
  50.         //Do something with the ARGB value array  
  51.     }  
  52. }  

There isn’t much to say about this code except that the a Bitmap object was created at line 12 and line 15 creates a 2 dimensional Integer array to store the pixels from the image. The image is loaded at line 25 and right after that, the size of the array is defined (line 28). Then, lines 32 through 38 output the value of each one of the pixels at the four corners of the image. Note that the getPixel() method requires two parameters: the X and Y coordinates of the pixel inside the image. These coordinates are the usual ones when working with image files: their axis goes from left to right and top to bottom.

Another detail is that the aforementioned method returns hexadecimal Integer values. That’s the reason why they are being converted with the toHexString() method before getting outputted to LogCat. This hexadecimal integer is composed by four value pairs that range from 00 to FF. The first two correspond to the alpha value of the image, where 00 is transparent and FF is completely opaque. The other three pairs determine the red, green and blue values, in that particular order.

Finally, the rgbValues array is filled with pixel color values (from line 41 to 48).

So, if this image is loaded by the above code:

Image with 4 colors

It would output the following:

  1. Top Left pixel: ffff0000  
  2. Top Right pixel: ff00ff00  
  3. Bottom Left pixel: ff0000ff  
  4. Bottom Right pixel: ffffffff  

Comments? Suggestions? Please use the Comment field!

3 Comments to “Android: How to return RGB values from an image file”

  1. Oscar says:

    Hi, I’m trying to implement your code but i cant seem to figure out what four_colors is. sorry if it’s obvious but im really new into android developing.
    Thanks a lot in advance for your help!

Leave a Reply to Oscar

Post Comments RSS