Last active
January 17, 2019 04:11
-
-
Save apkelly/cd0422b66f8a3746cdbf9a64d7456ad4 to your computer and use it in GitHub Desktop.
MLKitActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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