2

My current code allows you to take an image and display it inside an image view. How would I implement the ability to touch anywhere on the image and display the color at that pixel, the color would then fill a solid object such as a rectangle to display what it is. I'm hoping the solution contains RGB values as i need them for my application later on.

import android.app.Activity;
    import android.content.Intent;
    import android.graphics.drawable.Drawable;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;


    import java.io.File;

    public class MainActivity extends Activity {

        private static final int CONTENT_REQUEST = 1337;
        private File output = null;
        Button button;
        ImageView imageView;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button = (Button) findViewById(R.id.CameraButton);
            imageView = (ImageView) findViewById(R.id.Image_view);
            button.setOnClickListener(new View.OnClickListener() {


                public void onClick(View v) {

                    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

                    output = new File(dir, "COMP4Image.jpeg");
                    i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));

                    startActivityForResult(i, CONTENT_REQUEST);

                }

            });
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/COMP4Image.jpeg";
            imageView.setImageDrawable(Drawable.createFromPath(path));
        }


    }

1 Answers1

0

I won't provide code but the solution is fairly easy to implement.

Add a View.OnTouchListener to your ImageView.

This listener will respond to every single touch event happening on the View. (touch down, move, touch up etc...)

You can retrieve the coordinate of the event with MotionEvent.getX(int) (and y equivalent). (read MotionEvent documentation to learn more about pointers).

When you have X and Y coordinates of the touch event, you can get the pixel color of the pointed pixel in the bitmap with Bitmap.getPixel(int, int) (see here to get the Bitmap attached to your ImageView).

Voila, you have the selected color. Just print it into a rectangular view below your image view and use Color utility class to extract ARVB components of the selected color.

Hope this helps.

Community
  • 1
  • 1
pdegand59
  • 12,776
  • 8
  • 51
  • 58
  • I have implemented this however the coordinates it finds when i touch on the screen displays a colour that is slightly off from what it should be. I think this is because it is relative to the entire screen rather than the image view. Do you have any idea how i make it relative to the image view? http://pastebin.com/UbT48eJ1 @pdegand59 – Aaron Miller Feb 10 '16 at 14:03
  • Or it is relative to the original image resolution instead of the scaled bitmap. – Aaron Miller Feb 10 '16 at 14:11