点按图片可获取特定点的rgb值

时间:2014-05-27 06:40:37

标签: android

我试图在用户点按的图像上获取rgb值。我正在使用以下代码来实现这一目标。

imageView.setOnTouchListener(new ImageView.OnTouchListener(){     
            @Override   
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub       
                int x=0;
                int y=0;
                ImageView imageView = ((ImageView)v);
                Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
                int pixel = bitmap.getPixel(x,y);
                int redValue = Color.red(pixel);
                int blueValue = Color.blue(pixel);
                int greenValue = Color.green(pixel);
                if(pixel == Color.RED){

                    }

                Log.v("RGB",pixel+ " :R: "+redValue+ " G: "+blueValue+ " B:"+greenValue);

                return true;    }     
            });

但它为图像上的每个点返回相同的RGB值,即“-10197916:R:100 G:100 B:100”。 我甚至使用过int x=(int)event.getIntX(); int y=(int)event.getIntY(); 但结果总是一样的。我错过了什么?

3 个答案:

答案 0 :(得分:2)

您使用过:

int x=0;
int y=0;  

您需要使用:

int x = (int)event.getX();
int y = (int)event.getY();

答案 1 :(得分:1)

这个有效,问题是要正确x,y为:

 imageView.setOnTouchListener(imgSourceOnTouchListener);

 OnTouchListener imgSourceOnTouchListener
= new OnTouchListener(){

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

        float eventX = event.getX();
        float eventY = event.getY();
        float[] eventXY = new float[] {eventX, eventY};

        Matrix invertMatrix = new Matrix();
        ((ImageView)view).getImageMatrix().invert(invertMatrix);

        invertMatrix.mapPoints(eventXY);
        int x = Integer.valueOf((int)eventXY[0]);
        int y = Integer.valueOf((int)eventXY[1]);

        System.out.println(
                "touched position: "
                + String.valueOf(eventX) + " / " 
                + String.valueOf(eventY));
        System.out.println(
                "touched position: "
                + String.valueOf(x) + " / " 
                + String.valueOf(y));

        Drawable imgDrawable = ((ImageView)view).getDrawable();
        Bitmap bitmap = ((BitmapDrawable)imgDrawable).getBitmap();

        System.out.println(
                "drawable size: "
                + String.valueOf(bitmap.getWidth()) + " / " 
                + String.valueOf(bitmap.getHeight()));

        //Limit x, y range within bitmap
        if(x < 0){
            x = 0;
        }else if(x > bitmap.getWidth()-1){
            x = bitmap.getWidth()-1;
        }

        if(y < 0){
            y = 0;
        }else if(y > bitmap.getHeight()-1){
            y = bitmap.getHeight()-1;
        }

        int touchedRGB = bitmap.getPixel(x, y);

        System.out.println("touched color: " + "#" + Integer.toHexString(touchedRGB));


        return true;
    }};

答案 2 :(得分:0)

iView_image1.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    Bitmap bmp = Bitmap.createBitmap(v.getDrawingCache());
                    int color = 0;
                    try {
                        color = bmp.getPixel((int) event.getX(), (int) event.getY());
                        int r = Color.red(color); 
                        int  g = Color.green(color); 
                        int b = Color.blue(color);
                    } catch (Exception e) {
                   }

                    return false;
                }
            });