Skip to content

Instantly share code, notes, and snippets.

@shubham0204
Created February 22, 2022 01:07
Show Gist options
  • Save shubham0204/b074c628bb2c461e034cf464b95ed9d1 to your computer and use it in GitHub Desktop.
Save shubham0204/b074c628bb2c461e034cf464b95ed9d1 to your computer and use it in GitHub Desktop.
private lateinit var activityMainBinding : ActivityMainBinding
private lateinit var progressDialog : ProgressDialog
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activityMainBinding = ActivityMainBinding.inflate( layoutInflater )
setContentView( activityMainBinding.root )
progressDialog = ProgressDialog( this )
activityMainBinding.selectImageButton.setOnClickListener {
dispatchSelectPictureIntent()
}
}
// Dispatch Intent to let the user select an image from the gallery
private fun dispatchSelectPictureIntent() {
val selectPictureIntent = Intent( Intent.ACTION_OPEN_DOCUMENT ).apply {
type = "image/*"
addCategory( Intent.CATEGORY_OPENABLE )
}
activityResultLauncher.launch( selectPictureIntent )
}
// Intercept the result of the select image intent here.
private val activityResultLauncher = registerForActivityResult( ActivityResultContracts.StartActivityForResult() ) {
if ( it.data == null ) {
// If the user didn't select any image from the gallery
return@registerForActivityResult
}
progressDialog.setMessage( "🤳 Detecting faces ..." )
progressDialog.show()
val inputStream = contentResolver.openInputStream( it.data?.data!! )
val bitmap = BitmapFactory.decodeStream( inputStream )
inputStream?.close()
// Pass the image to the face detector
detectFaces( bitmap )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment