Bitmap.getPixels()中的IllegalArgumentException

时间:2011-05-04 19:27:17

标签: android bitmap pixels

我想使用int[]将位图中的数据复制到getPixels(),这是我当前的代码:

int[] pixels = new int[myBitmap.getHeight() * myBitmap.getWidth()];
myBitmap.getPixels(pixels, 0, myBitmap.getWidth(), 0, 0,
        myBitmap.getHeight(), myBitmap.getWidth());

for(int i = 0; i < myBitmap.getHeight() * myBitmap.getWidth(); i++) {
    Log.e(TAG, "pixel"+i+"" +pixels[i]);
}

但这是一个例外:

05-04 20:24:08.281: ERROR/AndroidRuntime(5700): Uncaught handler: thread main exiting due to uncaught exception
05-04 20:24:08.296: ERROR/AndroidRuntime(5700): java.lang.IllegalArgumentException: y + height must be <= bitmap.height()
05-04 20:24:08.296: ERROR/AndroidRuntime(5700):     at android.graphics.Bitmap.checkPixelsAccess(Bitmap.java:818)
05-04 20:24:08.296: ERROR/AndroidRuntime(5700):     at android.graphics.Bitmap.getPixels(Bitmap.java:771)
05-04 20:24:08.296: ERROR/AndroidRuntime(5700):     at com.tecmark.Jjilapp$TouchView.onDraw(Jjilapp.java:206)

有什么想法吗?我刚刚将y参数指定为0。

2 个答案:

答案 0 :(得分:12)

我认为此代码中存在拼写错误。试试这个:

myBitmap.getPixels(pixels, 0, myBitmap.getWidth(), 0, 0,
   myBitmap.getWidth(), myBitmap.getHeight());

答案 1 :(得分:10)

您的宽度和高度已切换。

myBitmap.getPixels(pixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getHeight(), myBitmap.getWidth());

VS

public void getPixels (int[] pixels, int offset, int stride, int x, int y, int width, int height) http://developer.android.com/reference/android/graphics/Bitmap.html#getPixels(int[], int, int, int, int, int, int)