Skip to content

Commit

Permalink
Add toggle for camera on AR view
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Oct 31, 2023
1 parent 6b0bcb8 commit 93f6895
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.widget.FrameLayout
import android.widget.ImageButton
import android.widget.SeekBar
import androidx.camera.view.PreviewView
import androidx.core.view.drawToBitmap
import androidx.core.view.isVisible
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
Expand All @@ -34,10 +35,14 @@ class CameraView(context: Context, attrs: AttributeSet?) : FrameLayout(context,

var camera: ICamera? = null

private var lastFov: Pair<Float, Float>? = null

val fov: Pair<Float, Float>
get() {
val defaultFOV = 45f
return camera?.getZoomedFOV() ?: (defaultFOV to defaultFOV)
val fieldOfView = camera?.getZoomedFOV() ?: lastFov ?: (defaultFOV to defaultFOV * 4f / 3f)
lastFov = fieldOfView
return fieldOfView
}

private val preview: PreviewView
Expand Down Expand Up @@ -85,6 +90,8 @@ class CameraView(context: Context, attrs: AttributeSet?) : FrameLayout(context,
isStarted = true
}

alpha = 1f

camera?.stop(this::onCameraUpdate)
imageListener = onImage
camera = Camera(
Expand All @@ -103,6 +110,7 @@ class CameraView(context: Context, attrs: AttributeSet?) : FrameLayout(context,
fun stop() {
camera?.stop(this::onCameraUpdate)
camera = null
alpha = 0f
synchronized(startLock) {
isStarted = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class ARBeaconLayer(
it to distance
}.sortedByDescending { it.second }

// TODO: Avoid recreating markers every time
// TODO: Avoid recreating markers every time - it will help if this didn't filter nearby beacons
// if (!areBeaconsUpToDate) {
layer.setMarkers(visible.flatMap {
val beacon = it.first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class AugmentedRealityFragment : BoundFragment<FragmentAugmentedRealityBinding>(
private val horizonLayer = ARHorizonLayer()
private val northLayer = ARNorthLayer()

private var isCameraEnabled = true

private val compassSyncTimer = CoroutineTimer {
binding.linearCompass.azimuth = Bearing(binding.arView.azimuth)
}
Expand All @@ -78,28 +80,53 @@ class AugmentedRealityFragment : BoundFragment<FragmentAugmentedRealityBinding>(

binding.arView.setLayers(listOf(northLayer, horizonLayer, sunLayer, moonLayer, beaconLayer))

binding.cameraToggle.setOnClickListener {
if (isCameraEnabled) {
stopCamera()
} else {
startCamera()
}
}

scheduleUpdates(INTERVAL_1_FPS)
}

override fun onResume() {
super.onResume()

binding.arView.start()
if (isCameraEnabled) {
startCamera()
}
updateAstronomyLayers()

// TODO: Move this to the AR view
compassSyncTimer.interval(INTERVAL_60_FPS)
}

// TODO: Move this to the AR view
private fun startCamera() {
isCameraEnabled = true
binding.cameraToggle.setImageResource(R.drawable.ic_camera)
binding.arView.backgroundFillColor = Color.TRANSPARENT
requestCamera {
if (it) {
binding.camera.start(
readFrames = false, shouldStabilizePreview = false
)
} else {
isCameraEnabled = false
binding.cameraToggle.setImageResource(R.drawable.ic_camera_off)
binding.arView.backgroundFillColor = Color.BLACK
alertNoCameraPermission()
}
}
}

updateAstronomyLayers()

compassSyncTimer.interval(INTERVAL_60_FPS)
private fun stopCamera(){
binding.cameraToggle.setImageResource(R.drawable.ic_camera_off)
isCameraEnabled = false
binding.arView.backgroundFillColor = Color.BLACK
binding.camera.stop()
}

override fun onPause() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import kotlin.math.asin
import kotlin.math.atan2
import kotlin.math.cos
import kotlin.math.sin
import kotlin.math.sqrt

// TODO: Notify location change
// TODO: This needs a parent view that has the camera, this, and any buttons (like the freeform button)
Expand All @@ -51,6 +50,8 @@ class AugmentedRealityView : CanvasView {

var focusText: String? = null

var backgroundFillColor: Int = Color.TRANSPARENT

private var orientation = Quaternion.zero
private var inverseOrientation = Quaternion.zero

Expand Down Expand Up @@ -223,7 +224,7 @@ class AugmentedRealityView : CanvasView {

override fun draw() {
updateOrientation()
clear()
background(backgroundFillColor)

layers.forEach {
it.draw(this, this)
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_camera_off.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!-- drawable/camera_off.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#ffffff"
android:pathData="M1.2,4.47L2.5,3.2L20,20.72L18.73,22L16.73,20H4A2,2 0 0,1 2,18V6C2,5.78 2.04,5.57 2.1,5.37L1.2,4.47M7,4L9,2H15L17,4H20A2,2 0 0,1 22,6V18C22,18.6 21.74,19.13 21.32,19.5L16.33,14.5C16.76,13.77 17,12.91 17,12A5,5 0 0,0 12,7C11.09,7 10.23,7.24 9.5,7.67L5.82,4H7M7,12A5,5 0 0,0 12,17C12.5,17 13.03,16.92 13.5,16.77L11.72,15C10.29,14.85 9.15,13.71 9,12.28L7.23,10.5C7.08,10.97 7,11.5 7,12M12,9A3,3 0 0,1 15,12C15,12.35 14.94,12.69 14.83,13L11,9.17C11.31,9.06 11.65,9 12,9Z" />
</vector>
14 changes: 13 additions & 1 deletion app/src/main/res/layout/fragment_augmented_reality.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
Expand All @@ -21,12 +22,23 @@
android:layout_height="match_parent"
android:layout_gravity="center" />

<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/camera_toggle"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="end"
android:layout_margin="8dp"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_camera"
app:tint="@color/white" />


</FrameLayout>

<com.kylecorry.trail_sense.navigation.ui.LinearCompassView
android:id="@+id/linear_compass"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_marginBottom="@dimen/default_bottom_margin"/>
android:layout_marginBottom="@dimen/default_bottom_margin" />

</LinearLayout>

0 comments on commit 93f6895

Please sign in to comment.