Skip to content

Instantly share code, notes, and snippets.

View husaynhakeem's full-sized avatar

Husayn Hakeem husaynhakeem

View GitHub Profile
childFragmentManager.setFragmentResultListener(...)
parentFragmentManager.setFragmentResultListener(...)
parentFragmentManager.setFragmentResult(
requestKey, // Same request key FragmentA used to register its listener
bundleOf(key to value) // The data to be passed to FragmentA
)
FragmentManager.setFragmentResultListener(
requestKey,
lifecycleOwner,
FragmentResultListener { requestKey: String, result: Bundle ->
// Handle result
})
imageAnalysis.setAnalyzer(executor) { image ->
val rotationDegrees = image.imageInfo.rotationDegrees
// Analyze image. Make sure to close it before returning from the method, otherwise the pipeline will be blocked.
image.close()
}
// Set desired name and type of captured image to be used by the contentResolver when writing captured image to MediaStore
val contentValues = ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, "anImage")
put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg")
}
// Create the output file option to store the captured image in MediaStore
val outputFileOptions = ImageCapture.OutputFileOptions
.Builder(contentResolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
.build()
// Create file to store the image in
val imageFile = createTempFile("anImage", ".jpg")
// Create the output file option from the above file
val outputFileOptions = ImageCapture.OutputFileOptions.Builder(imageFile).build()
// Initiate image capture
imageCapture.takePicture(outputFileOptions, executor, object: ImageCapture.OnImageSavedCallback {
override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {
// Image was successfully saved to `imageFile`
imageCapture.takePicture(executor, object: ImageCapture.OnImageCapturedCallback() {
override fun onCaptureSuccess(image: ImageProxy) {
// Use the image, then make sure to close it.
image.close()
}
override fun onError(exception: ImageCaptureException) {
val errorType = exception.getImageCaptureError()
displayError(errorType)
}
// Create a SurfaceProvider using PreviewView. The CameraInfo you pass in to this method can be:
// - `null`, in which case you won't benifit from certain optimizations PreviewView can provide to display the preview.
// - Obtained after binding the use cases. Calling `bindToLifecycle()` returns a Camera instance, use it to call `Camera.getCameraInfo()`.
val surfaceProvider = previewView.createSurfaceProvider(cameraInfo)
// Attach the SurfaceProvider to the preview to start displaying the camera preview.
preview.setSurfaceProvider(surfaceProvider)
view.setOnTouchListener() { view, event ->
// Handle the touch event
// Return true when the event has been handled, false otherwise
}