Search code examples
javaandroidimageviewandroid-cameragetpixel

camera image getpixel colour for android app issue


Im pretty new to JAVA and this is my first stackoverflow post so I will try and be specific!

Well im trying to create an android application where the user can take a photo using the phones camera.the photo gets returned and placed with in an image view and the user can press a button to tell you what colour the image is. its working fine so far however I placed a picture of a green box within the image view for testing purposes and even tho the green box is being replaced with the new photo after you take a picture im still getting RGB values for the colour green despite the contents being replaced!

----my code----

private ImageView img;
private Bitmap bmp;
private Bitmap operation;
Button button;

ImageView imgFavorite;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imgFavorite = (ImageView)findViewById(R.id.imageView1);
    imgFavorite.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            open();
        }
    });

    img = (ImageView)findViewById(R.id.imageView1);
    button = (Button) findViewById(R.id.button);
    BitmapDrawable abmp = (BitmapDrawable)img.getDrawable();
    bmp = abmp.getBitmap();

    button.setOnClickListener(new View.OnClickListener() {
        @Override`enter code here`
        public void onClick(View v) {
            pix();
        }
    });
}

public void open(){
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, 0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    Bitmap bp = (Bitmap) data.getExtras().get("data");
    imgFavorite.setImageBitmap(bp);
}

public void pix(){
    operation= Bitmap.createBitmap(bmp.getWidth(),
            bmp.getHeight(),bmp.getConfig());

    int height = bmp.getHeight();
    int width = bmp.getWidth();
    int p = bmp.getPixel(height / 2, width / 2);

    int r = Color.red(p);
    int g = Color.green(p);
    int b = Color.blue(p);

    Toast.makeText(this, String.valueOf(r) + String.valueOf(g) + String.valueOf(b), Toast.LENGTH_LONG).show();
}











@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}


Solution

  • Inside onActivityResult() you are setting the ImageView to display the new photo, but you are not setting the bmp variable, which is what you are using to read the RGB values. So just add this line to onActivityResult():

    bmp = bp;