【问题标题】:How to copy pixels from a column using Bitmap getPixels function如何使用 Bitmap getPixels 函数从列中复制像素
【发布时间】:2017-12-01 22:20:38
【问题描述】:

我使用Bitmap.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);

结果是这样的:

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

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

int[] verticalMiddleArray = new int[height];
source.getPixels(verticalMiddleArray, 0, width, width / 2, 0, 1, height -1 );

使用此代码,我收到 ArrayIndexOutOfBoundsException 异常。

现在位图的大小是 32x32。

【问题讨论】:

    标签: java android android-image android-bitmap


    【解决方案1】:

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

    strideint:在行之间跳过的条目数[](必须>=位图的宽度)。可以是负数。

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

    垂直切片的调用应该是:

    source.getPixels(verticalMiddleArray, 0, 1, width / 2, 0, 1, height);
    

    (我假设你有 height - 1 作为尝试修复。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-09
      • 2012-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-03
      相关资源
      最近更新 更多