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
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<com.otaliastudios.cameraview.CameraView | |
android:id="@+id/camera_view" |
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
override fun onCreate(savedInstanceState: Bundle?) { | |
… | |
checkAndRequestCameraPermission() | |
} | |
private fun checkAndRequestCameraPermission() { | |
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) | |
!= PackageManager.PERMISSION_GRANTED) { | |
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.CAMERA), | |
PERMISION_REQUEST_CODE) |
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
implementation 'com.google.firebase:firebase-core:16.0.1' | |
implementation 'com.google.firebase:firebase-ml-vision:16.0.0' | |
implementation "android.arch.lifecycle:extensions:1.1.1" | |
implementation 'com.otaliastudios:cameraview:1.5.1' |
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
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { | |
if (requestCode == PERMISION_REQUEST_CODE) { | |
if (android.Manifest.permission.CAMERA == permissions[0] && | |
grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
startFaceProcessor() | |
} | |
} | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults) | |
} |
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
lifecycle.addObserver(MainActivityLifecycleObserver(camera_view)) | |
//MainActivityLifecycleObserver.class | |
class MainActivityLifecycleObserver(private val cameraView: CameraView) : LifecycleObserver { | |
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME) | |
fun startCamera() { | |
cameraView.start() | |
} |
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
cameraView.addFrameProcessor { frame -> | |
// Processes frame here | |
} |
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
// Initialize the face detection option, and we need all the face landmarks | |
private val options = FirebaseVisionFaceDetectorOptions.Builder() | |
.setLandmarkType(FirebaseVisionFaceDetectorOptions.ALL_LANDMARKS) | |
.build() |
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
// Obtain the FirebaseVisionFaceDetector instance | |
private val detector = FirebaseVision.getInstance().getVisionFaceDetector(options) |
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
// Build a image meta data object | |
val metadata = FirebaseVisionImageMetadata.Builder() | |
.setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21) | |
.setWidth(frame.size.width) | |
.setHeight(frame.size.height) | |
.setRotation(rotation) | |
.build() | |
// Create vision image object, and it will be consumed by FirebaseVisionFaceDetector | |
// for face detection | |
val firebaseVisionImage = FirebaseVisionImage.fromByteArray(frame.data, metadata) |
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
// Perform face detection | |
detector.detectInImage(firebaseVisionImage).addOnSuccessListener { faceList -> | |
if (faceList.size > 0) { | |
// We just need the first face | |
val face = faceList[0] | |
// Draw the bitmaps on the detected faces | |
} | |
} |
OlderNewer