我使用camera.addCallbackBuffer(data);
重用缓冲区以避免内存不足。我在PreviewCallback中的代码,例如
checkFace(data, camera);
camera.addCallbackBuffer(data);
在方法checkFace中,只需将数据转换为位图,然后使用FaceDetector检查面部。而且我已经尝试在转换数据后使用camera.addCallbackBuffer(data);
,但是Android Studio探查器中的本机内存显示为
我的应用运行大约10分钟后,“本机”从10MB增加到250MB。
当我的应用运行大约4个小时时,它将崩溃并显示Logcat:
E / IMemory(17967):无法映射BpMemoryHeap(binder = 0x11515160),大小= 462848,fd = 70(内存不足)
我认为也许是因为“本机”记忆越来越多
代码:
camera1.setPreviewCallbackWithBuffer(previewCallback1);
camera1.addCallbackBuffer(buffer1);
camera1.startPreview();
...
private Camera.PreviewCallback previewCallback1 = (data, camera) -> {
checkFace(data, camera);
camera.addCallbackBuffer(data);
};
//convert data to bitmap then check face from the bitmap
private void checkFace(byte[] data, Camera camera){
...
...run on new Thread...
Bitmap bitmap = BitmapUtil.ByteToBitmap(data, camera.getParameters().getPreviewSize());
...
FaceDetector detector = new FaceDetector(bitmap.getWidth(), bitmap.getHeight(), numberOfFace);
...then get the result of face detection
}
//convert frame data to bitmap
public static Bitmap ByteToBitmap(byte[] data, Camera.Size previewSize) {
ByteArrayOutputStream baos = null;
Bitmap bitmapOut = null;
try {
int w = previewSize.width;
int h = previewSize.height;
YuvImage yuvimage = new YuvImage(data, ImageFormat.NV21, w, h, null);
baos = new ByteArrayOutputStream();
yuvimage.compressToJpeg(new Rect(0, 0, w, h), 60, baos);
byte[] jdata = baos.toByteArray();
bitmapOut = BitmapFactory.decodeByteArray(jdata, 0, jdata.length);
if (null == bitmapOut) {
return bitmapOut;
}
jdata = null;
yuvimage = null;
Matrix matrix = new Matrix();
matrix.postRotate(90);
bitmapOut = Bitmap.createBitmap(bitmapOut, 0, 0, w, h, matrix, false);
} catch (Exception e) {
} finally {
try {
if (baos != null) {
baos.flush();
baos.close();
}
} catch (Exception e) {
}
}
return bitmapOut;
}
那么,我应该怎么解决呢?