我写了一个关于相机的应用程序,将相机中的每个图像传递给BarCode
或DetectFace
类,以检测条形码或面部。但是我在使用DetectFace
时遇到内存泄漏的问题。
如果我使用BarCode
,那是安全的:
override fun onImageAvailable(reader: ImageReader) {
imageOnFrame = reader.acquireNextImage()
barcode = BarCode(this@MainActivity, imageOnFrame, getRotation("0"))
barcode!!.run()
barcode = null
imageOnFrame.close()
}
但是,如果我使用DetectFace
,则会导致内存泄漏:
override fun onImageAvailable(reader: ImageReader) {
imageOnFrame = reader.acquireNextImage()
detectFace = DetectFace(this@MainActivity, imageOnFrame, getRotation("0"))
detectFace!!.run()
detectFace = null
imageOnFrame.close()
}
这是课程DetectFace
:
class DetectFace(private val context: Context, private val image: Image, private val rotation: Int) {
private val option = FirebaseVisionFaceDetectorOptions.Builder()
.setModeType(FirebaseVisionFaceDetectorOptions.ACCURATE_MODE)
.setLandmarkType(FirebaseVisionFaceDetectorOptions.ALL_LANDMARKS)
.setClassificationType(FirebaseVisionFaceDetectorOptions.ALL_CLASSIFICATIONS)
.setTrackingEnabled(true)
.setMinFaceSize(0.01f)
.build()
fun run(){
val image = FirebaseVisionImage.fromMediaImage(this.image, rotation)
val detector = FirebaseVision.getInstance()
.getVisionFaceDetector(option)
detector.detectInImage(image)
.addOnSuccessListener {
// code ..
}
.addOnFailureListener{
// code ..
}
this.image.close()
}
}
答案 0 :(得分:0)
主要是由于发送到ML Kit的图像太快而无法及时处理,并且内存使用量增加了。我们将提供一种在ML Kit中正确处理它的方法。同时,您可以限制发送到ML Kit的图像。
例如,ML Kit快速入门示例应用程序在此处进行图像调节: https://github.com/firebase/quickstart-android/blob/master/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/VisionProcessorBase.java#L49