Skip to content

Instantly share code, notes, and snippets.

@apkelly
Last active January 17, 2019 04:11
Show Gist options
  • Save apkelly/cd0422b66f8a3746cdbf9a64d7456ad4 to your computer and use it in GitHub Desktop.
Save apkelly/cd0422b66f8a3746cdbf9a64d7456ad4 to your computer and use it in GitHub Desktop.
MLKitActivity.kt
class MLKitActivity : AbstractActivity() {
companion object {
private const val TAG = "MLKitActivity"
}
private var mCameraSource: MLCameraSource? = null
/**
* Creates and starts the camera.
*/
override fun createCameraSource() {
mCameraSource = MLCameraSource(this, mGraphicOverlay)
mCameraSource!!.setMachineLearningFrameProcessor(FaceDetector(object:FaceDetector.DetectorCallback {
override fun onSuccess(frameData: ByteBuffer, results: List<FirebaseVisionFace>, frameMetadata: FrameMetadata) {
mGraphicOverlay.clear()
results.forEach {face ->
val faceGraphic = FaceGraphic(face, mGraphicOverlay)
mGraphicOverlay.add(faceGraphic)
}
mGraphicOverlay.postInvalidate()
}
override fun onFailure(exception: Exception) {
exception.printStackTrace()
}
}))
}
/**
* Starts or restarts the camera source, if it exists.
*/
override fun startCameraSource() {
checkGooglePlayServices()
if (mCameraSource != null) {
try {
mCameraPreview.start(mCameraSource!!, mGraphicOverlay)
} catch (e: IOException) {
Log.e(TAG, "Unable to start camera source.", e)
mCameraSource!!.release()
mCameraSource = null
}
}
}
/**
* Releases the resources associated with the camera source.
*/
override fun releaseCameraSource() {
if (mCameraSource != null) {
mCameraSource!!.release()
mCameraSource = null
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment