本文介绍了Bitmap.getPixels()中的IllegalArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to copy the data from a Bitmap into an int[] using getPixels(), this is my current code:

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.

Any ideas? I just specified the y param as 0.

推荐答案

您已经改变了宽度和高度.

You have your width and hight switched.

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,int)

这篇关于Bitmap.getPixels()中的IllegalArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 00:24