How to find pixels color in particular coordinate in images?

筅森魡賤 提交于 2019-12-13 01:28:19

问题


I have one activity, my activity contains one ImageView.I have implemented OnTouchListener for ImageView.Here I want find pixels color where i touch in image.Please let me know how to find pixels color.I have one ideas,

-->In ImageView where i touch just find X and Y coordinate and find pixels in same X and Y coordinate and find pixels color.Please let me is it possible.

Please give any suggestion or document for to do this please.

with ur reference i wrote code for bitmap,

int mId=R.drawable.with_colors;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), mId);
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);  

In onTouch

int x=(int) event.getX();
int y=(int) event.getY();
int color=bitmap.getPixel(x, y);
Log.d("colors","---"+color);

But here Log printing "Colors---0".


回答1:


You can use the getDrawable() method of the imageview to get what is drawn, and then get the bitmap from that by casting to BitmapDrawable and calling getBitmap():

Drawable d = imageView.getDrawable();
Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
int color = bitmap.getPixel(x, y);

To get the coordinates, assign an OnTouchListener:

imageview.setOnTouchListener(new OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event){
        switch(event.getAction()){
        case MotionEvent.ACTION_UP:
            int x = (int) event.getX();
            int y = (int) event.getY();

            //TODO: get pixel at x, y

            break;
        default:
            return false;
        }
    return true;
    }
});

In the code above you can basically just insert the first code where it says "get pixel at x, y" to get the pixel color.



来源:https://stackoverflow.com/questions/9632114/how-to-find-pixels-color-in-particular-coordinate-in-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!