Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用位图getPixels函数从列中复制像素 我使用下面的代码在位图图像中间获得一行1像素的高度: int width = source.getWidth(); int height = source.getHeight(); int[] horizontalMiddleArray = new int[width]; source.getPixels(horizontalMiddleArray, 0, width, 0, height / 2, width, 1);_Java_Android_Android Image_Android Bitmap - Fatal编程技术网

Java 如何使用位图getPixels函数从列中复制像素 我使用下面的代码在位图图像中间获得一行1像素的高度: int width = source.getWidth(); int height = source.getHeight(); int[] horizontalMiddleArray = new int[width]; source.getPixels(horizontalMiddleArray, 0, width, 0, height / 2, width, 1);

Java 如何使用位图getPixels函数从列中复制像素 我使用下面的代码在位图图像中间获得一行1像素的高度: int width = source.getWidth(); int height = source.getHeight(); int[] horizontalMiddleArray = new int[width]; source.getPixels(horizontalMiddleArray, 0, width, 0, height / 2, width, 1);,java,android,android-image,android-bitmap,Java,Android,Android Image,Android Bitmap,结果是: 现在我想做同样的事情,但在垂直方向: 我尝试了同样的逻辑,但它不起作用,我看不出我做错了什么: int[] verticalMiddleArray = new int[height]; source.getPixels(verticalMiddleArray, 0, width, width / 2, 0, 1, height -1 ); 使用此代码,我收到一个ArrayIndexOutOfBoundsException异常 目前位图的大小为32x32。该方法的文档要么完全错误,

结果是:

现在我想做同样的事情,但在垂直方向:

我尝试了同样的逻辑,但它不起作用,我看不出我做错了什么:

int[] verticalMiddleArray = new int[height];
source.getPixels(verticalMiddleArray, 0, width, width / 2, 0, 1, height -1 );
使用此代码,我收到一个
ArrayIndexOutOfBoundsException
异常


目前位图的大小为32x32。

该方法的文档要么完全错误,要么无意中误导,具体取决于解释。对于
stride
参数,它表示:

stride
int
:行之间要跳过的以像素[]为单位的条目数(必须>=位图的宽度)。可能是负面的

这里,“位图的宽度”不是指源的宽度,而是指目标的宽度。当它进行检查以确保提供的数组足够大以容纳请求的数据时,您将获得
ArrayIndexOutOfBoundsException
。由于源位图比目标位图宽,因此数据所需的大小大于所传递数组的大小

对垂直切片的调用应为:

source.getPixels(verticalMiddleArray, 0, 1, width / 2, 0, 1, height);
(我假设您尝试修复了
高度-1
。)