본문 바로가기

Flutter

[j Flutter] 갤러리/앨범에서 사진가져오기 & 카메라 실행

728x90
반응형

갤러리에서 사진 가져오기 / 카메라 실행을 해보자.

 

둘 다 실행시킬 수 있는 공통 패키지를 설치해준다.

 

1. image_picker 패키지 pubspec.yaml 에 설치

dependencies:
  image_picker: ^0.6.7+17

 

pub.dev/packages/image_picker

 

image_picker | Flutter Package

Flutter plugin for selecting images from the Android and iOS image library, and taking new pictures with the camera.

pub.dev

2. ImagePicker 사용

File Photo_file;

void Photo(ImageSource source) async {
    File file = await ImagePicker.pickImage(source: source);
    setState(() => Photo_file = file);
  }

 

3. 사진 가져오기 / 사진찍기 적용

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';



Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Container(
                          height: 40,
                          child: ElevatedButton(
                            child: Text(
                              '사진 가져오기',
                              style: TextStyle(fontSize: 20),
                            ),
                            onPressed: () => Photo(ImageSource.gallery),
                          ),
                        ),
                        Container(
                          height: 40,
                          child: ElevatedButton(
                            child: Text(
                              '사진찍기',
                              style: TextStyle(fontSize: 20),
                            ),
                            onPressed: () =>
                                Photo(ImageSource.camera), // 사진 찍기
                          ),
                        ),
                      ],
                    ),

 

 

 

*************** 실행화면**************

앨범에서 사진가져오기 / 사진찍기

 

 

** 동영상 참고: https://www.youtube.com/watch?v=JjaDQUm6B3M 

 

728x90
반응형