我编写了一个关于相机的应用程序,使用来自相机的每张图像,我将其传递给班级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()
}
}