0

I've the below code working fine, that upon button click, the Camera intent is opened, allowing me to take a photo, then confirm the photo is ok, i.e. 2 interfaces from the user after clicking the button, till the pic is loaded in the image view. Can I automate it, i.e. once the user click the button on the activity, camera intent is opened directly, take a photo of whatever there, and return what had been captured to the user.

mm, i.e. something like the apps working with take a selfie voice command.

My typical code for opening the camera intent is:

btnCamera.setOnClickListener {   // I need this click to be the only thing done by the user 
    if(isPermissionGranted(permission.CAMERA)) startCamera()
     else requestCameraPermission(this)
}

private fun startCamera() {
    val fileName = System.currentTimeMillis().toString() + ".jpeg"
    output = File(
       this.getExternalFilesDir(Environment.DIRECTORY_PICTURES),
       fileName
    )

    val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    outPutFileUri = this.let { it ->
        FileProvider.getUriForFile(
           it,
           BuildConfig.APPLICATION_ID,
           output!!
        )
     }
     intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutFileUri)
     startActivityForResult(intent, REQUEST_IMAGE_CAPTURE)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) = runBlocking {
    super.onActivityResult(requestCode, resultCode, data)
    val activity = this
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
        val bitmap = outPutFileUri?.let { getCapturedImage(it) }
        imageView.setImageBitmap(bitmap)
    }
}

private fun getCapturedImage(selectedPhotoUri: Uri): Bitmap =
    when {
         Build.VERSION.SDK_INT < 28 -> MediaStore.Images.Media.getBitmap(
           contentResolver, selectedPhotoUri)
    else -> {
          val source = ImageDecoder.createSource(contentResolver, selectedPhotoUri)
          ImageDecoder.decodeBitmap(source)
    }
}

The Camera will be opened, picture will be taken, user will see what is going on the screen,, mm if there is a way to take a pic without opening the camera app it will be welcomed as well. thanks

Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
  • Do you mean without opening camera you want to capture photo ? – M D Sep 24 '19 at 11:12
  • Check this https://stackoverflow.com/questions/16297606/android-take-camera-picture-without-save-delete-confirmation && https://stackoverflow.com/questions/38581244/how-to-skip-or-avoid-retake-and-review-option-after-captureing-photo-from-came?noredirect=1&lq=1 && https://stackoverflow.com/questions/35956635/android-take-camera-picture-intent-remove-confirmation-dialog?noredirect=1&lq=1 – AskNilesh Sep 24 '19 at 11:13
  • @MD The Camera will be opened, picture will be taken, user will see what is going on the screen,, mm if there is a way to take a pic without opening the camera app it will be welcomed as well. thanks – Hasan A Yousef Sep 24 '19 at 11:14
  • Check CameraX for this. I am not quite sure it will server your purpose – M D Sep 24 '19 at 11:15

1 Answers1

0

Can I automate it, i.e. once the user click the button on the activity, camera intent is opened directly, take a photo of whatever there, and return what had been captured to the user.

No.

You would need to implement your own camera app functionality for automated image capture, whether using the camera APIs directly or via a wrapper library (CameraX, Fotoapparat, CameraKit-Android, etc.).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491