添加了翻转摄像头功能

This commit is contained in:
jackfiled 2023-01-11 16:37:00 +08:00
parent b2078e2938
commit 371fc779f2
9 changed files with 145 additions and 38 deletions

View File

@ -1,17 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<runningDeviceTargetSelectedWithDropDown>
<targetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<type value="QUICK_BOOT_TARGET" />
<deviceKey>
<Key>
<type value="SERIAL_NUMBER" />
<value value="adb-3040223527000PT-yH8BGJ._adb-tls-connect._tcp" />
<type value="VIRTUAL_DEVICE_PATH" />
<value value="$USER_HOME$/.android/avd/Pixel_3a_API_33_x86_64.avd" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2023-01-09T08:15:42.195541Z" />
</targetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2023-01-10T13:25:57.409214Z" />
</component>
</project>

View File

@ -5,11 +5,9 @@ import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.util.Size
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.View
import android.view.ViewGroup
import com.google.mediapipe.components.CameraHelper
import android.view.*
import androidx.appcompat.widget.Toolbar
import com.google.mediapipe.components.CameraHelper.CameraFacing
import com.google.mediapipe.components.CameraXPreviewHelper
import com.google.mediapipe.components.ExternalTextureConverter
import com.google.mediapipe.components.FrameProcessor
@ -34,6 +32,9 @@ class MainActivity : AppCompatActivity() {
lateinit var processor: FrameProcessor
lateinit var converter: ExternalTextureConverter
lateinit var cameraHelper: CameraXPreviewHelper
private var cameraFacing = CameraFacing.BACK
private lateinit var toolbar: Toolbar
init {
// 加载项目中用到的jni库
@ -45,6 +46,21 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
toolbar = findViewById(R.id.main_toolbar)
toolbar.inflateMenu(R.menu.toolbar_menu)
toolbar.setOnMenuItemClickListener {
Log.d(tag, "Button Clicked.")
when (it.itemId) {
R.id.action_camera_flip -> {
Log.i(tag, "Flip Camera Button Pressed.")
flipCamera()
true
} else -> {
super.onMenuItemSelected(it.itemId, it)
}
}
}
// 初始化展示界面
previewDisplayView = SurfaceView(this)
setupPreviewDisplayView()
@ -112,7 +128,64 @@ class MainActivity : AppCompatActivity() {
PermissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
/**
* 启动摄像机
*/
private fun startCamera() {
Log.i(tag, "Start Camera.")
cameraHelper = CameraXPreviewHelper()
cameraHelper.setOnCameraStartedListener {
if (it != null) {
previewFrameTexture = it
}
// 将预览画面设置为可见
// 这将触发添加到holder中的callback
previewDisplayView.visibility = View.VISIBLE
}
cameraHelper.startCamera(
this,
cameraFacing,
null,
// 不提供大小
// 让helper自己决定
null
)
}
/**
* 翻转摄像头方向
*/
private fun flipCamera() {
Log.i(tag, "Flipping Camera Now!")
// 首先关闭目前的正在使用的摄像头
previewDisplayView.visibility = View.GONE
try {
// 根据 https://github.com/google/mediapipe/issues/1842#issuecomment-867954937
// 需要首先关闭converter
converter.close()
previewFrameTexture.release()
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
cameraFacing = if (cameraFacing == CameraFacing.FRONT) {
CameraFacing.BACK
} else {
CameraFacing.FRONT
}
converter = ExternalTextureConverter(
eglManager.context,
2
)
converter.setFlipY(true)
converter.setConsumer(processor)
cameraHelper = CameraXPreviewHelper()
cameraHelper.setOnCameraStartedListener {
if (it != null) {
@ -124,7 +197,6 @@ class MainActivity : AppCompatActivity() {
previewDisplayView.visibility = View.VISIBLE
}
val cameraFacing = CameraHelper.CameraFacing.BACK
cameraHelper.startCamera(
this,
cameraFacing,

View File

@ -0,0 +1,8 @@
<!-- drawable/camera-flip.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#000" android:pathData="M20 5H17L15 3H9L7 5H4C2.9 5 2 5.9 2 7V19C2 20.11 2.9 21 4 21H20C21.11 21 22 20.11 22 19V7C22 5.9 21.11 5 20 5M5 12H7.1C7.65 9.29 10.29 7.55 13 8.1C13.76 8.25 14.43 8.59 15 9L13.56 10.45C13.11 10.17 12.58 10 12 10C10.74 10 9.6 10.8 9.18 12H11L8 15L5 12M16.91 14C16.36 16.71 13.72 18.45 11 17.9C10.25 17.74 9.58 17.41 9 17L10.44 15.55C10.9 15.83 11.43 16 12 16C13.27 16 14.41 15.2 14.83 14H13L16 11L19 14H16.91Z" />
</vector>

View File

@ -1,26 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/preview_display"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
tools:ignore="MissingConstraints"
>
<TextView
android:id="@+id/no_camera_access_hint"
android:orientation="vertical">
<!--标题栏-->
<androidx.appcompat.widget.Toolbar
android:id="@+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints"
android:background="?attr/colorPrimary"
app:title="@string/app_name"
>
</androidx.appcompat.widget.Toolbar>
<FrameLayout
android:id="@+id/preview_display"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/no_camera_access_hint">
tools:ignore="MissingConstraints"
>
</TextView>
</FrameLayout>
<TextView
android:id="@+id/no_camera_access_hint"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/no_camera_access_hint">
</TextView>
</FrameLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_camera_flip"
app:showAsAction="ifRoom"
android:icon="@drawable/camera_flip"
android:title="@string/action_flip_camera_name">
</item>
</menu>

View File

@ -2,12 +2,12 @@
<!-- Base application theme. -->
<style name="Theme.MotionCapture" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_200</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorPrimary">@color/blue_dark</item>
<item name="colorPrimaryVariant">@color/blue_dark</item>
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_200</item>
<item name="colorSecondary">@color/blue_dark</item>
<item name="colorSecondaryVariant">@color/blue_dark</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>

View File

@ -1,10 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="blue">#34A2DD</color>
<color name="blue_dark">#3a5fcd</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
</resources>

View File

@ -1,4 +1,5 @@
<resources>
<string name="app_name">MotionCapture</string>
<string name="no_camera_access_hint">Please Make Sure the Camera Permission is Given!</string>
<string name="action_flip_camera_name">Flip Camera</string>
</resources>

View File

@ -1,13 +1,14 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MotionCapture" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!--继承没有actionbar的主题-->
<style name="Theme.MotionCapture" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorPrimary">@color/blue</item>
<item name="colorPrimaryVariant">@color/blue</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorSecondary">@color/blue_dark</item>
<item name="colorSecondaryVariant">@color/blue_dark</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>