我创建了一个应用程序,它获取 Android 设备捕获的图像,并在 ImageView 中显示该图像。然后,用户可以按下按钮对图像进行模糊或去模糊。当我在 Android 设备上运行该应用程序时,我可以使用相机拍摄图像并毫无问题地显示该图像。当我按下模糊按钮时出现问题,该按钮运行一些代码来模糊图像。应用程序被卡住,并且我的一行代码创建了一个新数组并将其存储在嵌套 for 循环中的另一个数组中,因此出现 OutOfMemoryException。
这是嵌套 for 循环的代码:
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int xTranslated = (x + width / 2) % width;
int yTranslated = (y + height / 2) % height;
double real = temp[2 * (xTranslated + yTranslated * width)];
double imaginary = temp[2 * (xTranslated + yTranslated * width) + 1];
degradation[2 * (x + y * width)] = real;
degradation[2 * (x + y * width) + 1] = imaginary;
Complex c = new Complex(real, imaginary);
complex[y * width + x] = c;
}
}
此嵌套 for 循环处理从输入图像中提取的数据,该数据存储为位图。
这是应用运动模糊的完整方法:
public Complex[] motionBlur(double[] degradation, int width, int height, double alpha, double gamma, double sigma) {
Complex[] complex = new Complex[width * height];
double[] temp = new double[2 * width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
double teta = Math.PI * ( (((x - width/2) % width) * gamma) + ((((y - height/2) % height) * sigma) ));
Sinc sinc = new Sinc();
double real = (Math.cos(teta) * sinc.value(teta)) * alpha;
double imaginary = (Math.sin(teta) * sinc.value(teta)) * alpha;
Complex cConj = new Complex(real, imaginary).conjugate();
temp[2 * (x + y * width)] = cConj.getReal();
temp[2 * (x + y * width) + 1] = cConj.getImaginary();
}
}
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int xTranslated = (x + width / 2) % width;
int yTranslated = (y + height / 2) % height;
double real = temp[2 * (xTranslated + yTranslated * width)];
double imaginary = temp[2 * (xTranslated + yTranslated * width) + 1];
degradation[2 * (x + y * width)] = real;
degradation[2 * (x + y * width) + 1] = imaginary;
Complex c = new Complex(real, imaginary);
complex[y * width + x] = c;
}
}
return complex;
}
以下是我尝试运行应用程序时 logcat 输出内容的链接:http://pastebin.com/ysbN9A3s
MainActivity.java:373对应的行,
Complex c = new Complex(real, imaginary);
最佳答案
这里是nice talk关于 Android 崩溃。
Pierre-Yves 在 11:39 谈论 OOM(OutOfMemory 错误)。所以OOM的问题不在于OOM发生的地方。您应该分析您的应用程序以找到消耗最多内存的地方。我应该承认 OOM 是最难解决的错误之一。
祝你好运!
关于android - 如何修复 Android 中有关数组数组的内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29847924/