728x90
MainActivity
- 메인 폼
Camera & Gallery 로 이미지 가져오기
이미지 촬영
- 촬영 버튼을 누르면 카메라가 실행
- 카메라에서 찍은 사진을 이미지뷰에 나타냄
이미지 불러오기
- 불러오기 버튼을 누르면 갤러리에서 사진을 불러올 수 있음
- 갤러리에서 불러온 사진을 이미지뷰에 나타냄
bulid.gradle
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
defaultConfig {
applicationId "com.example.cameraandgallery"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
}
}
dependencies {
def dependency_version = "1.3.1"
implementation "androidx.activity:activity-ktx:$dependency_version"
implementation "androidx.fragment:fragment-ktx:$dependency_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cameraandgallery">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="AI 랜드마크 인식"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CameraAndGallery">
//<activity
// android:name=".ImgActivity"
// android:exported="false" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>
xml 폴더에 파일 경로 저장하는 코드
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="my_images"
path="Android/data/com.example.cameraandgallery/files/Pictures" />
</paths>
activity_mai.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/imagePreview"
android:layout_width="match_parent"
android:layout_height="587dp" />
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_okay"
android:layout_width="418dp"
android:layout_height="wrap_content"
android:text="확인"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/buttonCamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="true"
android:visibility="visible"
android:text="촬영" />
<Button
android:id="@+id/buttonGallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="true"
android:text="불러오기" />
</LinearLayout>
</LinearLayout>
MainActivity.kt
package com.example.cameraandgallery
import android.Manifest
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.view.View
import android.widget.ImageView
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.FileProvider
import com.example.cameraandgallery.databinding.ActivityMainBinding
import java.io.ByteArrayOutputStream
import java.io.File
class MainActivity : AppCompatActivity() {
var photoUri:Uri? = null
lateinit var cameraPermission:ActivityResultLauncher<String>
lateinit var storagePermission:ActivityResultLauncher<String>
lateinit var cameraLauncher:ActivityResultLauncher<Uri>
lateinit var galleryLauncher:ActivityResultLauncher<String>
val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
storagePermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
if(isGranted) {
setViews()
} else {
Toast.makeText(baseContext, "외부저장소 권한을 승인해야 앱을 사용할 수 있습니다.", Toast.LENGTH_LONG).show()
finish()
}
}
cameraPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
if(isGranted) {
openCamera()
} else {
Toast.makeText(baseContext, "카메라 권한을 승인해야 카메라를 사용할 수 있습니다.", Toast.LENGTH_LONG).show()
}
}
cameraLauncher = registerForActivityResult(ActivityResultContracts.TakePicture()) { isSuccess ->
if(isSuccess) { binding.imagePreview.setImageURI(photoUri) }
}
galleryLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
binding.imagePreview.setImageURI(uri)
}
storagePermission.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
fun setViews() {
binding.buttonCamera.setOnClickListener {
cameraPermission.launch(Manifest.permission.CAMERA)
if (binding.imagePreview != null) {
binding.btnOkay.visibility = View.VISIBLE
}
}
binding.buttonGallery.setOnClickListener {
openGallery()
if (binding.imagePreview != null) {
binding.btnOkay.visibility = View.VISIBLE
}
}
}
fun openCamera() {
val photoFile = File.createTempFile(
"IMG_",
".jpg",
getExternalFilesDir(Environment.DIRECTORY_PICTURES)
)
photoUri = FileProvider.getUriForFile(
this,
"${packageName}.provider",
photoFile
)
cameraLauncher.launch(photoUri)
}
fun openGallery() {
galleryLauncher.launch("image/*")
}
}
728x90
'App & Web > Android' 카테고리의 다른 글
[코틀린] 파이어베이스 리얼타임 데이터베이스 업로드 (0) | 2022.10.11 |
---|---|
[코틀린] 이미지 이진화 하기 (0) | 2022.10.11 |
코틀린 OpenCV (0) | 2022.10.11 |
[Android] 파이어 베이스 연동 안될 때 해결 방법 - 안드로이드 스튜디오 (0) | 2022.09.28 |
[Android] 이미지 데이터를 다음 화면(서브 인텐트)로 옮기기 - Kotlin 코틀린 (0) | 2022.09.16 |