Skip to content

Commit

Permalink
Update CameraXBasic sample to alpha06
Browse files Browse the repository at this point in the history
- Update calls to provide Executors
- Update setTargetAspectRatio() to use AspectRatio enums

Change-Id: I82361fc94f5ccacc34090d4c506bd680cce7bb2b
  • Loading branch information
Max LY Lau committed Nov 25, 2019
1 parent 79ef790 commit 69df928
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion CameraXBasic/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ dependencies {
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

// CameraX
def camerax_version = "1.0.0-alpha05"
def camerax_version = "1.0.0-alpha06"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ import android.os.Build
import android.os.Bundle
import android.util.DisplayMetrics
import android.util.Log
import android.util.Rational
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.TextureView
import android.view.View
import android.view.ViewGroup
import android.webkit.MimeTypeMap
import android.widget.ImageButton
import androidx.camera.core.AspectRatio
import androidx.camera.core.CameraX
import androidx.camera.core.ImageAnalysis
import androidx.camera.core.ImageAnalysisConfig
Expand All @@ -51,6 +51,7 @@ import androidx.camera.core.Preview
import androidx.camera.core.PreviewConfig
import androidx.navigation.Navigation
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.core.view.setPadding
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
Expand All @@ -73,7 +74,9 @@ import java.nio.ByteBuffer
import java.text.SimpleDateFormat
import java.util.ArrayDeque
import java.util.Locale
import java.util.concurrent.Executor
import java.util.concurrent.TimeUnit
import kotlin.math.abs

/** Helper type alias used for analysis use case callbacks */
typealias LumaListener = (luma: Double) -> Unit
Expand All @@ -90,6 +93,7 @@ class CameraFragment : Fragment() {
private lateinit var viewFinder: TextureView
private lateinit var outputDirectory: File
private lateinit var broadcastManager: LocalBroadcastManager
private lateinit var mainExecutor: Executor

private var displayId = -1
private var lensFacing = CameraX.LensFacing.BACK
Expand Down Expand Up @@ -136,6 +140,7 @@ class CameraFragment : Fragment() {
super.onCreate(savedInstanceState)
// Mark this as a retain fragment, so the lifecycle does not get restarted on config change
retainInstance = true
mainExecutor = ContextCompat.getMainExecutor(requireContext())
}

override fun onResume() {
Expand Down Expand Up @@ -259,14 +264,20 @@ class CameraFragment : Fragment() {

// Get screen metrics used to setup camera for full screen resolution
val metrics = DisplayMetrics().also { viewFinder.display.getRealMetrics(it) }
val screenAspectRatio = Rational(metrics.widthPixels, metrics.heightPixels)
val screenAspectRatio = metrics.heightPixels * 1.0f / metrics.widthPixels
val aspectRatio =
if (abs(screenAspectRatio - 16.0/9) - abs(screenAspectRatio - 4.0/3) > 0) {
AspectRatio.RATIO_4_3
} else {
AspectRatio.RATIO_16_9
}
Log.d(TAG, "Screen metrics: ${metrics.widthPixels} x ${metrics.heightPixels}")

// Set up the view finder use case to display camera preview
val viewFinderConfig = PreviewConfig.Builder().apply {
setLensFacing(lensFacing)
// We request aspect ratio but no resolution to let CameraX optimize our use cases
setTargetAspectRatio(screenAspectRatio)
setTargetAspectRatio(aspectRatio)
// Set initial target rotation, we will have to call this again if rotation changes
// during the lifecycle of this use case
setTargetRotation(viewFinder.display.rotation)
Expand All @@ -281,7 +292,7 @@ class CameraFragment : Fragment() {
setCaptureMode(CaptureMode.MIN_LATENCY)
// We request aspect ratio but no resolution to match preview config but letting
// CameraX optimize for whatever specific resolution best fits requested capture mode
setTargetAspectRatio(screenAspectRatio)
setTargetAspectRatio(aspectRatio)
// Set initial target rotation, we will have to call this again if rotation changes
// during the lifecycle of this use case
setTargetRotation(viewFinder.display.rotation)
Expand All @@ -300,13 +311,15 @@ class CameraFragment : Fragment() {
}.build()

imageAnalyzer = ImageAnalysis(analyzerConfig).apply {
analyzer = LuminosityAnalyzer { luma ->
// Values returned from our analyzer are passed to the attached listener
// We log image analysis results here -- you should do something useful instead!
val fps = (analyzer as LuminosityAnalyzer).framesPerSecond
Log.d(TAG, "Average luminosity: $luma. " +
"Frames per second: ${"%.01f".format(fps)}")
}
setAnalyzer(mainExecutor,
LuminosityAnalyzer { luma ->
// Values returned from our analyzer are passed to the attached listener
// We log image analysis results here --
// you should do something useful instead!
val fps = (analyzer as LuminosityAnalyzer).framesPerSecond
Log.d(TAG, "Average luminosity: $luma. " +
"Frames per second: ${"%.01f".format(fps)}")
})
}

// Apply declared configs to CameraX using the same lifecycle owner
Expand Down Expand Up @@ -341,7 +354,7 @@ class CameraFragment : Fragment() {
}

// Setup image capture listener which is triggered after photo has been taken
imageCapture.takePicture(photoFile, imageSavedListener, metadata)
imageCapture.takePicture(photoFile, metadata, mainExecutor, imageSavedListener)

// We can only change the foreground Drawable using API level 23+ API
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class AutoFitPreviewBuilder private constructor(
useCase = Preview(config)

// Every time the view finder is updated, recompute layout
useCase.onPreviewOutputUpdateListener = Preview.OnPreviewOutputUpdateListener {
useCase.setOnPreviewOutputUpdateListener(Preview.OnPreviewOutputUpdateListener {
val viewFinder =
viewFinderRef.get() ?: return@OnPreviewOutputUpdateListener
Log.d(TAG, "Preview output changed. " +
Expand All @@ -109,7 +109,7 @@ class AutoFitPreviewBuilder private constructor(
bufferRotation = it.rotationDegrees
val rotation = getDisplaySurfaceRotation(viewFinder.display)
updateTransform(viewFinder, rotation, it.textureSize, viewFinderDimens)
}
})

// Every time the provided texture view changes, recompute layout
viewFinder.addOnLayoutChangeListener { view, left, top, right, bottom, _, _, _, _ ->
Expand Down

0 comments on commit 69df928

Please sign in to comment.