4

我正在开发适用于 Android 的增强现实应用程序。我正在实现 Tom Gibara 的精明边缘检测器类,并将 Android 不支持的 BufferedImage 替换为 Bitmap。

“follow”方法(在下面发布)对我造成了 StackOverflow 错误。这是一个递归函数,但让我感到困惑的是,它可以在设备上正常工作大约 10-15 秒,然后崩溃。

从 Google 看来,人们已经在 J​​ava 中成功实现了这个类,但我想知道它是否出于某种原因在 Android 上不起作用。Gibara 的代码指定它仅供单线程使用;这可能是问题的一部分吗?如果不是这样,我的错误对任何人来说都是显而易见的吗?

谢谢!

private void follow(int x1, int y1, int i1, int threshold) {  
    int x0 = x1 == 0 ? x1 : x1 - 1;  
    int x2 = x1 == width - 1 ? x1 : x1 + 1;  
    int y0 = y1 == 0 ? y1 : y1 - 1;  
    int y2 = y1 == height -1 ? y1 : y1 + 1;

    data[i1] = magnitude[i1];  
    for (int x = x0; x <= x2; x++) {  
        for (int y = y0; y <= y2; y++) {  
            int i2 = x + y * width;  
            if ((y != y1 || x != x1) && data[i2] == 0 
                    && magnitude[i2] >= threshold) {  
                follow(x, y, i2, threshold);  
                return;  
            }  
        }  
    }  
}
4

1 回答 1

1

Android's default thread stack is much smaller than what you get on a desktop. In current Android builds (2.3), the stack size is set to 12kB I believe. Your recursion is simply too deep.

于 2011-01-31T19:47:19.253 回答