Bitmap.getPixels() 中的 IllegalArgumentException
我想使用 getPixels()
将数据从位图复制到 int[]
中,这是我当前的代码:
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。
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]);
}
But it's throwing an exception:
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)
Any ideas? I just specified the y param as 0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这段代码中有一个拼写错误。试试这个:
I think there's a typo in this code. Try this:
您已经切换了宽度和高度。
myBitmap.getPixels(pixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getHeight(), myBitmap.getWidth());
与
public void getPixels (int[] Pixels) , int 偏移量, int 步幅, int x, int y, int 宽度, int 高度)
http://developer.android.com/reference/android/graphics/Bitmap.html#getPixels(int[], int, int, int, int, int, int)
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)