我得到这个函数的错误,我是新的科特林编程,不知道我会如何修复这个问题。我能找到的唯一的answewr是针对Java的,我不知道如何转换它们
我不知道如何使这个静态或尝试把它从一个嵌套的类任何帮助都会得到赞赏
此AsyncTask类应该是静态的,否则可能会发生泄漏(匿名)
android.os.AsyncTask(异步任务)
更少(F1)静电场会泄漏。
private fun detectAndFrame(imageBitmap: Bitmap) {
val outputStream = ByteArrayOutputStream()
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
val inputStream = ByteArrayInputStream(outputStream.toByteArray())
val detectTask = object : AsyncTask<InputStream, String, Array<Face>>() {
var exceptionMessage = ""
override fun doInBackground(vararg params: InputStream): Array<Face>? {
try {
publishProgress("Detecting...")
val result = faceServiceClient.detect(
params[0],
true, // returnFaceId
false, null// returnFaceAttributes:
/* new FaceServiceClient.FaceAttributeType[] {
FaceServiceClient.FaceAttributeType.Age,
FaceServiceClient.FaceAttributeType.Gender }
*/
)// returnFaceLandmarks
if (result == null) {
publishProgress(
"Detection Finished. Nothing detected"
)
return null
}
publishProgress(
String.format(
"Detection Finished. %d face(s) detected",
result.size
)
)
return result
} catch (e: Exception) {
exceptionMessage = String.format(
"Detection failed: %s", e.message
)
return null
}
}
override fun onPreExecute() {
//TODO: show progress dialog
detectionProgressDialog.show()
}
override fun onProgressUpdate(vararg progress: String) {
//TODO: update progress
detectionProgressDialog.setMessage(progress[0])
}
override fun onPostExecute(result: Array<Face>) {
//TODO: update face frames
detectionProgressDialog.dismiss()
if (exceptionMessage != "") {
showError(exceptionMessage)
}
if (result == null) return
imageTook.setImageBitmap(
drawFaceRectanglesOnBitmap(imageBitmap, result)
)
imageBitmap.recycle()
}
}
detectTask.execute(inputStream)
}
private fun drawFaceRectanglesOnBitmap(
originalBitmap: Bitmap, faces: Array<Face>?
): Bitmap {
val bitmap = originalBitmap.copy(Bitmap.Config.ARGB_8888, true)
val canvas = Canvas(bitmap)
val paint = Paint()
paint.isAntiAlias = true
paint.style = Paint.Style.STROKE
paint.color = Color.RED
paint.strokeWidth = 10f
if (faces != null) {
for (face in faces) {
val faceRectangle = face.faceRectangle
canvas.drawRect(
faceRectangle.left.toFloat(),
faceRectangle.top.toFloat(),
(faceRectangle.left + faceRectangle.width).toFloat(),
faceRectangle.top + faceRectangle.height.toFloat(),
paint
)
}
}
return bitmap
}
private fun showError(message: String) {
AlertDialog.Builder(this)
.setTitle("Error")
.setMessage(message)
.setPositiveButton("OK") { dialog, id -> }
.create().show()
}