728x90
반응형
SMALL
Camera
Android 프레임워크에는 기기에서 이용 가능한 다양한 카메라와 카메라 기능에 대한 지원을 포함하여 애플리케이션에서 사진과 동영상을 캡처할 수 있도록 한다.
AndroidManifest
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
xml
<?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"
tools:context=".home.camera">
<ImageView
android:id="@+id/avatars"
android:layout_width="500dp"
android:layout_height="500dp"
android:layout_marginTop="28dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.505"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" />
<Button
android:id="@+id/picture"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="44dp"
android:layout_marginEnd="68dp"
android:text="갤러리"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/avatars" />
<Button
android:id="@+id/camera"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="68dp"
android:layout_marginTop="44dp"
android:text="카메라"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
main
import android.Manifest
import android.app.Activity
import android.content.ContentValues
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.example.foresthealing.R
import java.io.FileOutputStream
import java.text.SimpleDateFormat
class camera : AppCompatActivity() {
// storage 권한 처리에 필요한 변수
val CAMERA = arrayOf(Manifest.permission.CAMERA)
val STORAGE = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
val CAMERA_CODE = 98
val STORAGE_CODE = 99
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 카메라
val camera = findViewById<Button>(R.id.camera)
camera.setOnClickListener {
CallCamera()
}
// 사진 저장
val picture = findViewById<Button>(R.id.picture)
picture.setOnClickListener {
GetAlbum()
}
}
// 카메라 권한, 저장소 권한
// 요청 권한
override fun onRequestPermissionsResult(requestCode: Int,
permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when(requestCode){
CAMERA_CODE -> {
for (grant in grantResults){
if(grant != PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "카메라 권한 승인", Toast.LENGTH_LONG).show()
}
}
}
STORAGE_CODE -> {
for(grant in grantResults){
if(grant != PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "저장소 권한 승인", Toast.LENGTH_LONG).show()
}
}
}
}
}
// 다른 권한 확인
fun checkPermission(permissions: Array<out String>, type:Int):Boolean{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
for (permission in permissions){
if(ContextCompat.checkSelfPermission(this, permission)
!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, permissions, type)
return false
}
}
}
return true
}
// 카메라 촬영 - 권한 처리
fun CallCamera(){
if(checkPermission(CAMERA, CAMERA_CODE) && checkPermission(STORAGE, STORAGE_CODE)){
val itt = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(itt, CAMERA_CODE)
}
}
// 사진 저장
fun saveFile(fileName:String, mimeType:String, bitmap: Bitmap): Uri?{
var CV = ContentValues()
// MediaStore에 파일명, mimeType을 지정
CV.put(MediaStore.Images.Media.DISPLAY_NAME, fileName)
CV.put(MediaStore.Images.Media.MIME_TYPE, mimeType)
// 안정성 검사
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
CV.put(MediaStore.Images.Media.IS_PENDING, 1)
}
// MediaStore에 파일 저장
val uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, CV)
if(uri != null){
var scriptor = contentResolver.openFileDescriptor(uri, "w")
val fos = FileOutputStream(scriptor?.fileDescriptor)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos)
fos.close()
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
CV.clear()
// IS_PENDING 초기화
CV.put(MediaStore.Images.Media.IS_PENDING, 0)
contentResolver.update(uri, CV, null, null)
}
}
return uri
}
// 결과
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
val imageView = findViewById<ImageView>(R.id.avatars)
if(resultCode == Activity.RESULT_OK){
when(requestCode){
CAMERA_CODE -> {
if(data?.extras?.get("data") != null){
val img = data?.extras?.get("data") as Bitmap
val uri = saveFile(RandomFileName(), "image/jpeg", img)
imageView.setImageURI(uri)
}
}
STORAGE_CODE -> {
val uri = data?.data
imageView.setImageURI(uri)
}
}
}
}
// 파일명을 날짜로 저장
fun RandomFileName() : String{
val fileName = SimpleDateFormat("yyyyMMddHHmmss").format(System.currentTimeMillis())
return fileName
}
// 갤러리 취득
fun GetAlbum(){
if(checkPermission(STORAGE, STORAGE_CODE)){
val itt = Intent(Intent.ACTION_PICK)
itt.type = MediaStore.Images.Media.CONTENT_TYPE
startActivityForResult(itt, STORAGE_CODE)
}
}
}
https://developer.android.com/guide/topics/media/camera?hl=ko
728x90
반응형
LIST
'App Programming > Kotlin' 카테고리의 다른 글
[Kotlin] UI 프로그래밍 (2) (0) | 2023.01.12 |
---|---|
[Kotlin] UI 프로그래밍 (1) (0) | 2023.01.12 |
[Kotlin] Jetpack BottomNavigationView (0) | 2023.01.09 |
[Kotlin] 애니메이션 (Animation) (0) | 2023.01.09 |
[Kotlin]코틀린 실행 (0) | 2022.04.28 |