Skip to content

Commit

Permalink
[#9] cameraX를 이용한 기본 카메라 띄우기
Browse files Browse the repository at this point in the history
  • Loading branch information
zozero94 committed Dec 9, 2020
1 parent 1592d57 commit 2697098
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,85 @@
package com.example.myapplication.ui.mediapipe

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.SurfaceTexture
import android.os.Bundle
import android.util.Log
import android.view.SurfaceView
import android.view.View
import androidx.camera.core.CameraSelector
import androidx.camera.core.Preview
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.example.myapplication.R
import com.example.myapplication.databinding.ActivityMediapipeBinding
import com.example.myapplication.ui.base.BaseActivity

class MediaPipeActivity: BaseActivity<ActivityMediapipeBinding>() {

class MediaPipeActivity : BaseActivity<ActivityMediapipeBinding>() {
override val layoutId: Int = R.layout.activity_mediapipe


private var previewFrameTexture: SurfaceTexture? = null
private var previewDisplayView: SurfaceView? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (checkPermission()) {
startCamera()
} else {
ActivityCompat.requestPermissions(this, PERMISSIONS, REQUEST_CODE_PERMISSIONS)
}
previewDisplayView = SurfaceView(this)
setupPreviewDisplayView();
}

private fun checkPermission() = PERMISSIONS.all {
ContextCompat.checkSelfPermission(baseContext, it) == PackageManager.PERMISSION_GRANTED
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
Log.d("zero", "requestCode : $requestCode , permission : $permissions")
}

private fun startCamera() {
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)

cameraProviderFuture.addListener({
val cameraProvider = cameraProviderFuture.get()

val preview = Preview.Builder()
.build()
.also {
it.setSurfaceProvider(binding.viewFinder.surfaceProvider)
}

val cameraSelector = CameraSelector.DEFAULT_FRONT_CAMERA

try {
cameraProvider.unbindAll()
cameraProvider.bindToLifecycle(this, cameraSelector, preview)
} catch (e: Exception) {
Log.e("startCamera", "use case binding failed")
}

}, ContextCompat.getMainExecutor(this))
}

private fun setupPreviewDisplayView() {
previewDisplayView!!.visibility = View.GONE
}

companion object {
fun newIntent(context: Context) = Intent(context, MediaPipeActivity::class.java)
private val PERMISSIONS = arrayOf(Manifest.permission.CAMERA)
private const val REQUEST_CODE_PERMISSIONS = 100
}
}
12 changes: 11 additions & 1 deletion MyApplication/app/src/main/res/layout/activity_mediapipe.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

Expand All @@ -8,9 +9,18 @@
type="com.example.myapplication.ui.mediapipe.MediaPipeViewModel" />
</data>


<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.camera.view.PreviewView
android:id="@+id/viewFinder"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
18 changes: 16 additions & 2 deletions MyApplication/app/src/main/res/layout/preview_display_layout.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/preview_display_layout"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/no_camera_access_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="적당한 텍스트"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

</TextView>
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 2697098

Please sign in to comment.