[Flutter] 카메라 제어하기

류재성's avatar
Apr 25, 2024
[Flutter]  카메라 제어하기
 

1. 기본 설정

 
andoid/app/src/main/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 쓰기권한부여 --> .... </manifest>
 
AndroidManifest.xml에 쓰기 권한을 추가한다. 권한은 manifest 바로 밑에 추가한다.
 
notion image
 
pubspec.yml에 image_picker 와 gallery_saver 을 추가한다.
 
android/app/src/build.gradle
notion image
 
build.gradle 에서 해당 부분을 수정한다.
 

2. 사진 촬영, 저장 어플 만들기

 
import 'package:flutter/material.dart'; import 'package:gallery_saver/gallery_saver.dart'; import 'package:image_picker/image_picker.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatefulWidget { const MyApp({super.key}); @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Column( children: [ Expanded( child: Center( child: Text( "사진 저장하기", style: TextStyle(fontSize: 50.0), ), ), ), Row( children: [ IconButton( onPressed: () { _takePhoto(); }, icon: Icon(Icons.camera_alt_outlined), iconSize: 50.0, ), ], ), ], ), ), ); } void _takePhoto() async { ImagePicker().pickImage(source: ImageSource.camera).then((value) { if (value != null && value.path != null) { print("저장경로 : ${value.path}"); GallerySaver.saveImage(value.path).then((value) { print("사진이 저장되었습니다."); },); } },); } }
 
notion image
 
notion image
 
하단의 카메라 버튼을 누르면 가상의 카메라가 뜨면서 사진을 찍을 수 있다.
 
notion image
 
포토를 들어가면 찍은 사진을 확인할 수 있다.
 
 

3. 사진 촬영 저장 및 불러오기 어플 만들기

 
andoid/app/src/main/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 쓰기권한부여 --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!-- 읽기권한부여 --> .... </manifest>
 
쓰기 권한과 함께 읽기 권한도 작성한다.
main.dart
import 'dart:io'; import 'package:flutter/material.dart'; import 'package:gallery_saver/gallery_saver.dart'; import 'package:image_picker/image_picker.dart'; void main() { runApp(MyApp()); } class MyApp extends StatefulWidget { @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { File? _selectedImage; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Column( children: [ Expanded( child: Center( child: Text( "사진 저장하기", style: TextStyle(fontSize: 50.0), ), ), ), Container( child: _selectedImage != null ? Image.file(_selectedImage!) : Text("사진 없음"), ), Row( mainAxisAlignment: MainAxisAlignment.end, children: [ IconButton( onPressed: _takePhoto, icon: Icon(Icons.camera_alt_outlined), iconSize: 50.0, ), IconButton( onPressed: _pickImageFromGallery, icon: Icon(Icons.image_outlined), iconSize: 50.0, ) ], ), ], ), ), ); } void _takePhoto() async { final pickedImage = await ImagePicker().pickImage(source: ImageSource.camera); if (pickedImage != null) { setState(() { _selectedImage = File(pickedImage.path); }); GallerySaver.saveImage(pickedImage.path).then( (bool? success) { print(success == true ? "사진이 저장되었습니다." : "사진 저장 오류"); }, ); } } void _pickImageFromGallery() async { final pickedImage = await ImagePicker().pickImage(source: ImageSource.gallery); if (pickedImage != null) { setState(() { _selectedImage = File(pickedImage.path); }); } } }
notion image
 
notion image
 
카메라 버튼을 누르면 사진도 찍을 수 있고, 오른쪽으 사진 아이콘을 누르면 찍은 사진이 나온다.
Share article
RSSPowered by inblog