Search code examples
androidimagepixelrgb

How to read images in android


Objective : read an image and get its RGB pixel values .

Problem : thing is that i had created a new folder(img) under project(testing) in eclipse and had pasted a .jpg file(test.jpg) and tried to read it with this code :

tig = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() +"/testing/img/test.jpg"); AlertDialog.Builder bld=new AlertDialog.Builder(this); bld.setTitle("hurray"); int pxl = tig.getPixel(30, 40); bld.setMessage(pxl); bld.show(); but at runtime application used to "Unfortunately close" , then i tried placing image in the asset folder and decodestream , no use .

question 1. is it not possible to create my own folder in project and place files there 2. or where should i place the image to read its pixels .

I need real spoon feeding here , coz im new to android development .

Thanks in advance .


Solution

  • From my understanding, I am guessing this is what you need

    change

    Bitmap src = (BitmapDrawable) this.getResources().getDrawable(R.drawable.yourimagename);
    

    to

    Bitmap src = BitmapFactory.decodeResource(context.getResources(),
                                           R.drawable.yourimagename);
    

    and check again

    Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
    for(int x = 0;x < bitmap.getWidth();x++)
        for(int y = 0;y < bitmap.getHeight();y++)
        {
       // here bitmap.getPixel(x, y)) gets the pixel at the position (x,y)  
       int pixelvalue = bitmap.getPixel(x, y));
        int rValue = color.red(pixelvalue); // gives value of R in "RGB"
        int gValue = color.green(pixelvalue); // gives value of G in "RGB"
        int bValue = color.blue(pixelvalue); // gives value of B in "RGB"
    }
    

    Place your drawable here

    enter image description here

    Happy Coding