Last active
November 24, 2019 02:54
-
-
Save Lavanyagaur22/c28d15db5db2434376b991be656c3cf9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
/*In the MainActivity class, initialize Fritz SDK. | |
*/ | |
Fritz.configure(this, API_KEY) | |
/*Instead of calling `startCamera()` on the main thread, we use `viewFinder.post { ... }` | |
to make sure that `viewFinder` has already been inflated into the view when `startCamera()` is called. | |
*/ | |
view_finder.post { | |
startCamera() | |
} | |
} | |
//Function that creates and displays the camera preview | |
private fun startCamera() { | |
//Specify the configuration for the preview | |
val previewConfig = PreviewConfig.Builder() | |
.apply { | |
//Set the resolution of the captured image | |
setTargetResolution(Size(1920, 1080)) | |
} | |
.build() | |
//Generate a preview | |
val preview = Preview(previewConfig) | |
//Add a listener to update preview automatically | |
preview.setOnPreviewOutputUpdateListener { | |
val parent = view_finder.parent as ViewGroup | |
//Remove thr old preview | |
parent.removeView(view_finder) | |
//Add the new preview | |
parent.addView(view_finder, 0) | |
view_finder.surfaceTexture = it.surfaceTexture | |
} | |
val analyzerConfig = ImageAnalysisConfig.Builder().apply { | |
// In our analysis, we care more about the latest image than | |
// analyzing *every* image | |
setImageReaderMode( | |
ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE | |
) | |
}.build() | |
val imageAnalysis = ImageAnalysis(analyzerConfig).apply { | |
setAnalyzer(executor, ImageProcessor()) | |
} | |
/* Bind use cases to lifecycle. If Android Studio complains about "this" | |
being not a LifecycleOwner, try rebuilding the project or updating the appcompat dependency to | |
version 1.1.0 or higher. | |
*/ | |
CameraX.bindToLifecycle(this, preview, imageAnalysis) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment