0

I am trying to read an Image pixel by pixel and want to save the pixel RGB value in arrays. but when trying to do so I am getting an exception in the statement
R[i]=redValue;
Someone please suggest what I might doing wrong?

please find the exception stack trace here -

paste.ubuntu.com/6216208

public class AnalyzeImage extends Activity  
{   
    int []R;  
    int []G;  
    int []B;  
    int width, height, i=0;  
    int a=1;  
    public int analyzeImagefunc(Bitmap bitmap)
    {            
    try{ 
     width  =  bitmap.getWidth();
     height = bitmap.getHeight();
     i = 0;
     for(int x = 0;x < width;x++)
     {
      for(int y = 0;y < height;y++)
      {
      int pixel = bitmap.getPixel(x,y);         
      int redValue = Color.red(pixel);
      int blueValue = Color.blue(pixel);
      int greenValue = Color.green(pixel);
      a=5;
      R[i]=redValue; 
      a=6;
      i++;
       }
      } 
     }

    catch(Exception e)
        {
            //nothing
        }
     return a;
 }  
} 
1
  • Array is not initialized Commented Oct 10, 2013 at 6:24

4 Answers 4

3

Your arrays need to be initialzed first before using. You have jsut declared the arrays:

int []R;  
int []G;  
int []B;  

but not initialized them.So if you try to access any array element it will throw null pointer exception. Try to initialized your array like:

int []R = new int[10];  
int []G = new int[10];  
int []B = new int[10];  

You can change the size of arrays as per your need.

Sign up to request clarification or add additional context in comments.

Comments

2

int []R;
int []G;
int []B;

R, G, B is not initialized any where.

initialize the R

 width  =  bitmap.getWidth();
 height = bitmap.getHeight();
 i = 0;
 int size = height * width;
 R = new int[size]; // initialize the size of array R   
 for(int x = 0;x < width;x++)

Comments

2

Array need initialization with fix size. so if array's size is not fix then you should go with ArrayList. and also you can cast ArrayList to array if needed for final output.

    public class AnalyzeImage extends Activity {
        int[] R;
        int[] G;
        int[] B;
        ArrayList<Integer> R_Lst = new ArrayList<Integer>();
        ArrayList<Integer> G_Lst = new ArrayList<Integer>();
        ArrayList<Integer> B_Lst = new ArrayList<Integer>();

        int width, height, i = 0;
        int a = 1;

        public int analyzeImagefunc(Bitmap bitmap) {
            try {
                width = bitmap.getWidth();
                height = bitmap.getHeight();
                i = 0;
                for (int x = 0; x < width; x++) {
                    for (int y = 0; y < height; y++) {
                        int pixel = bitmap.getPixel(x, y);
                        int redValue = Color.red(pixel);
                        int blueValue = Color.blue(pixel);
                        int greenValue = Color.green(pixel);
                        a = 5;
//                      R[i] = redValue;
                        R_Lst.add(redValue);
                        B_Lst.add(blueValue);
                        G_Lst.add(greenValue);
                        a = 6;
                        i++;
                    }
                }
            }

            catch (Exception e) {
                // nothing
            }
            R=convertIntegers(R_Lst);
            return a;
        }
        public int[] convertIntegers(List<Integer> integers)
        {
            int[] ret = new int[integers.size()];
            for (int i=0; i < ret.length; i++)
            {
                ret[i] = integers.get(i).intValue();
            }
            return ret;
        }

//      (Note that this will throw a NullPointerException if either integers or any element within it is null.)

1 Comment

want to save the pixel RGB value in arrays. He want to store the RGB values in Array
1
width = bitmap.getWidth();
height = bitmap.getHeight();
int n = width * height;
R = new int[n];  
G = new int[n];  
B = new int[n];
i = 0;

By implication, you also require:

R[i] = redValue;
G[i] = greenValue;
B[i] = blueValue;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.