[Android] NDK OpenCV + jetpack compose CameraX 설정하기 - 2

Kim.Taekwon·2022년 3월 6일
0
post-thumbnail

[Android] NDK OpenCV + jetpack compose CameraX 설정하기 - 1
Android Studio 에 NDK OpenCV설정까지 완료했기에 Jetpack compose를 이용하기위한 설정을 진행한다.

1. Android Jetpack compose 설정


Adding Jetpack Compose to your app
위 링크를 참고하여 Jetpack compose를 추가하는 과정을 설명한다.

bulid.gradle파일 에서 android의 설정값들을 확인한다.

android {
    defaultConfig {
        ...
        minSdkVersion 21
    }

    buildFeatures {
        // Enables Jetpack Compose for this module
        compose true
    }
    ...

    // Set both the Java and Kotlin compilers to target Java 8.
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }

    composeOptions {
        kotlinCompilerExtensionVersion '1.0.5'
    }
}

본인은 viewBindingcompose로 수정하고 composeOptions를 추가해 주었다.

  buildFeatures {
          viewBinding true → compose true
      }
  composeOptions {
          kotlinCompilerExtensionVersion '1.0.5'
      }

bulid.gradle파일 에서 dependencies의 설정값들을 추가해 준다.

dependencies {
    // Integration with activities
    implementation 'androidx.activity:activity-compose:1.3.1'
    // Compose Material Design
    implementation 'androidx.compose.material:material:1.0.5'
    // Animations
    implementation 'androidx.compose.animation:animation:1.0.5'
    // Tooling support (Previews, etc.)
    implementation 'androidx.compose.ui:ui-tooling:1.0.5'
    // Integration with ViewModels
    implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha07'
    // UI Tests
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4:1.0.5'
}

new-package를 클릭하여 ui.theme패키지를 만든다.

ui.theme 패키지에 Color.kt, Shape.kt, Theme.kt, Type.kt Kotlin File을 만든다.

아래를 참고하여 Color.kt, Shape.kt, Theme.kt, Type.kt 파일 내용을 작성한다.
Color.kt

import androidx.compose.ui.graphics.Color

val Purple200 = Color(0xFFBB86FC)
val Purple500 = Color(0xFF6200EE)
val Purple700 = Color(0xFF3700B3)
val Teal200 = Color(0xFF03DAC5)

Shape.kt

import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Shapes
import androidx.compose.ui.unit.dp

val Shapes = Shapes(
    small = RoundedCornerShape(4.dp),
    medium = RoundedCornerShape(4.dp),
    large = RoundedCornerShape(0.dp)
)

Theme.kt @Composable 아래행에 프로젝트명Theme로 변경하는것에 유의한다.

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.MaterialTheme
import androidx.compose.material.darkColors
import androidx.compose.material.lightColors
import androidx.compose.runtime.Composable

private val DarkColorPalette = darkColors(
    primary = Purple200,
    primaryVariant = Purple700,
    secondary = Teal200
)

private val LightColorPalette = lightColors(
    primary = Purple500,
    primaryVariant = Purple700,
    secondary = Teal200

    /* Other default colors to override
    background = Color.White,
    surface = Color.White,
    onPrimary = Color.White,
    onSecondary = Color.Black,
    onBackground = Color.Black,
    onSurface = Color.Black,
    */
)

@Composable
fun 프로젝트명Theme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit
) {
    val colors = if (darkTheme) {
        DarkColorPalette
    } else {
        LightColorPalette
    }

    MaterialTheme(
        colors = colors,
        typography = Typography,
        shapes = Shapes,
        content = content
    )
}

Type.kt

import androidx.compose.material.Typography
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp

// Set of Material typography styles to start with
val Typography = Typography(
    body1 = TextStyle(
        fontFamily = FontFamily.Default,
        fontWeight = FontWeight.Normal,
        fontSize = 16.sp
    )
    /* Other default text styles to override
    button = TextStyle(
        fontFamily = FontFamily.Default,
        fontWeight = FontWeight.W500,
        fontSize = 14.sp
    ),
    caption = TextStyle(
        fontFamily = FontFamily.Default,
        fontWeight = FontWeight.Normal,
        fontSize = 12.sp
    )
    */
)

2. Android CameraX 설정


app-manifests설정에서 camera사용, 퍼미션 관련 내용을 추가해준다.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="tw.camera.opencvjetpackcompose">

    <uses-feature android:name="android.hardware.camera" />
    <uses-permission android:name="android.permission.CAMERA" />
    

bulid.gradlie.app에서 CameraX 관련 dependencies를 추가한다.

    dependencies {
      def camerax_version = "1.1.0-beta01"
      implementation "androidx.camera:camera-camera2:${camerax_version}"
      implementation "androidx.camera:camera-lifecycle:${camerax_version}"
      implementation "androidx.camera:camera-view:${camerax_version}"

이것으로 NDK OpenCV + jetpack compose CameraX의 환경설정은 완료!
다음내용은 카메라 퍼미션과 프리뷰 표시하는 것을 기재하는 것을 목표로 한다.

0개의 댓글