0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[超簡単] Flutterでカメラ機能を実装

Last updated at Posted at 2022-10-09

初めに

Flutterでカメラ機能を実装する必要があったので、実装方法を記事にまとめました。
カメラ機能と聞くと難易度が高いと思われるかもしれませんが、flutterのカメラ機能の実装は超簡単なのでご安心ください。

初期セットアップ

パッケージの追加

 $ flutter pub add image_picker

もしくは、pubspec.yamldependenciesに以下の内容を記述

pubspec.yaml
dependencies:
  image_picker: ^0.8.6  //追加内容

パッケージを取得するために以下のコマンドを実行

$ flutter pub get

iOSの設定

以下の設定をios/Runner/Info.plistに追加します。

Info.plist
<key>NSPhotoLibraryUsageDescription</key>
<string>This app requires to access your photo library</string>
<key>NSCameraUsageDescription</key>
<string>This app requires to add file to your camera</string>
<key>NSMicrophoneUsageDescription</key>
<string>This app requires to add file to your photo library your microphone</string>

Androidの設定

android/app/src/main/AndroidManifest.xmlの タグに以下を追加します。

AndroidManifest.xml
<application
    android:requestLegacyExternalStorage="true" 
>

カメラ機能実装

インポートする

main.dart
import 'package:image_picker/image_picker.dart';

コード記述

main.dart
final picker = ImagePicker();

Future getImage() async {
    final ImagePicker _picker = ImagePicker();
    final XFile? photo = await _picker.pickImage(source: ImageSource.camera);
    print(photo);
}

getImage関数をボタンのonPressedに記述し、ボタンクリックでカメラに遷移します。
上記コードのみでカメラ機能の実装は完了です。

最後に

ImagePickerのバージョン0.8.2以降より記述方法が変更になっています。
main.dart
//古いAPI
PickedFile image = await _picker.getImage(...)
//新しいAPI(0.8.2以降)
XFile image = await _picker.pickImage(...)

参考にしたサイトはバージョンのバージョンが古く、エラーに苦戦したので記載しておきます。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?