1

In my project i want to pick image from gallery then user can touch that image and that pixel color store into my db.

Problem:

It will give me Exception: y must be <bitmap.height()

Logcat:

07-22 16:38:59.406: E/AndroidRuntime(6139): java.lang.IllegalArgumentException: y must be < bitmap.height()
07-22 16:38:59.406: E/AndroidRuntime(6139):     at android.graphics.Bitmap.checkPixelAccess(Bitmap.java:788)
07-22 16:38:59.406: E/AndroidRuntime(6139):     at android.graphics.Bitmap.getPixel(Bitmap.java:740)
07-22 16:38:59.406: E/AndroidRuntime(6139):     at com.example.mycolorreader.PhotoActivity$2.onTouch(PhotoActivity.java:121)
07-22 16:38:59.406: E/AndroidRuntime(6139):     at android.view.View.dispatchTouchEvent(View.java:3762)
07-22 16:38:59.406: E/AndroidRuntime(6139):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
07-22 16:38:59.406: E/AndroidRuntime(6139):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
07-22 16:38:59.406: E/AndroidRuntime(6139):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
07-22 16:38:59.406: E/AndroidRuntime(6139):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
07-22 16:38:59.406: E/AndroidRuntime(6139):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)

Code:

final Bitmap bitmap = ((BitmapDrawable) ivPhoto.getDrawable()).getBitmap();
ivPhoto.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        int pixel = bitmap.getPixel(x, y);

I am try many more thing like set padding,set Image layout dynamic but always get this exception.

I am search lot on Stack overflow and Google but not getting positive response.

Please give me some idea.

4

1 回答 1

4

事件的 x/y 是触摸坐标。要获取位图的像素,我们应该首先将它们转换为位图坐标。这个过程非常好描述here

于 2013-07-22T12:15:03.050 回答