Skip to main content

Pick Color from Image

Picking the Color from the Image in Android in HEX and RGB

In android we use HEX for color code and in ios we use RGB so here we are gonna work on both. You can download the full source code from github PickColorFromImageAndroid.

ImageView picked_imageView;
picked_imageView = (ImageView) findViewById(R.id.imageView2);
picked_imageView.setOnTouchListener(this);
picked_imageView.setImageBitmap(bitmapImage);

OnTouch of picked_imageView we can get the color code in onTouch method. When we touch the imageView we will get the color code from the image and here you can format it in HEX and RGB.

@Override
public boolean onTouch(View v, MotionEvent event) {

    Matrix inverse = new Matrix();    
    picked_imageView.getImageMatrix().invert(inverse);    
    float[] touchPoint = new float[] {event.getX(), event.getY()};    
    inverse.mapPoints(touchPoint);    
    int x = Integer.valueOf((int)touchPoint[0]);    
    int y = Integer.valueOf((int) touchPoint[1]);
    int bitmapHeight=((BitmapDrawable) picked_imageView.getDrawable()).getBitmap()
                                                                       .getHeight();
    if(y>0 && y<bitmapHeight) {

        int pixel = ((BitmapDrawable) picked_imageView.getDrawable()).getBitmap()
                                                                    .getPixel(x, y);
        //then do what you want with the pixel data, e.g        
        int redValue = Color.red(pixel);
        int greenValue = Color.green(pixel);
        int blueValue = Color.blue(pixel);

        String hexColor = String.format("#%06X", (0xFFFFFF & pixel));

    }

    System.gc();
    return false;
}


 Happy Coding!!!


Comments

Popular posts from this blog

Vertical AutoScrolling TextView in Android

In android by default we can scroll the text in horizontal using marquee in layout, but if we want to scroll the text in vertical its not possible by default. So here we will learn to create a custom TextView which will auto-scroll in vertical direction. Source Code:  VerticalScrollingTextView-Android Create a AutoScrollingTextView.class which extends TextView: @SuppressLint ( "AppCompatCustomView" ) public class AutoScrollingTextView extends TextView { private static final float DEFAULT_SPEED = 65.0f ; public Scroller scroller ; public float speed = DEFAULT_SPEED ; public boolean continuousScrolling = true; public AutoScrollingTextView (Context context) { super (context) ; init( null, 0 ) ; scrollerInstance(context) ; } public AutoScrollingTextView (Context context , AttributeSet attrs) { super (context , attrs) ; init(attrs , 0 ) ; scr...

Flexbox inside the RecyclerView as a LayoutManager (FlexboxLayoutManager).

Currently google has release the Flexbox which can be used for building flexible layouts using FlexboxLayout, it can be interpreted as an advanced LinearLayout because both layouts align their child views sequentially. For more detail on this flexbox-layout But here we are gonna work on Flexbox with RecyclerView. Flexbox with a large number of items in a scrollable container! Let's first see what are the Supported attributes / features comparison Due to some characteristics of the RecyclerView, some Flexbox attributes are not available/not implemented to the FlexboxLayoutManager. Here is a quick overview of the attributes/features comparison between the two containers. Attribute / Feature FlexboxLayout                FlexboxLayoutManager (RecyclerView) flexDirection flexWrap (except wrap_reverse ) justifyContent alignItems alignContent - layout_order - layout_fle...

Android RecyclerView and StaggeredGridLayoutManager with Picasso/Glide

This project is there in GitHub https://github.com/yuvaraj119/Picasso-RecyclerView-StaggeredGridLayoutManager You can download and start customizing it for your project also. How to use with Picasso Picasso + RecyclerView + StaggeredGridLayoutManager Its the enhanced version of this project https://github.com/pohh/slotmachinepicasso were there was a problem with Picasso + RecyclerView + StaggeredGridLayoutManager shuffles resizable recycler views infinitely issue posted on github https://github.com/square/picasso/issues/918 I have made some changes now it works with Picasso and Glide without any shuffles and position change Currently this project is done with Picasso If you want to use it with Glide How to use with Glide Glide + RecyclerView + StaggeredGridLayoutManager Add dependencies for Glide https://github.com/bumptech/glide Remove Picasso library from dependency and remove all the codes of Picasso from MyGridAdapter.java and also from other p...