質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

Dart

Dartは、Googleによって開発されたJavaScriptの代替となることを目的に作られた、ウェブ向けのプログラミング言語である。

Q&A

0回答

748閲覧

Flutter カメラが内カメラに切り替わらない

sugoroku

総合スコア5

Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

Dart

Dartは、Googleによって開発されたJavaScriptの代替となることを目的に作られた、ウェブ向けのプログラミング言語である。

0グッド

0クリップ

投稿2021/03/28 12:40

編集2021/03/28 12:53

前提・実現したいこと

Flutterでカメラアプリを作っている者です。
ボタンを押したら、カメラが内カメラや外カメラに切り替わるような機能を実装したいです。

発生している問題・エラーメッセージ

エラーメッセージはありませんが、カメラが内カメラに切り替わりません。

該当のソースコード

dart

1 2import 'dart:io'; 3import 'package:camera/camera.dart'; 4import 'package:flutter/cupertino.dart'; 5import 'package:flutter/material.dart'; 6import 'dart:async'; 7import 'package:flutter_file_dialog/flutter_file_dialog.dart'; 8import 'package:flutter/services.dart'; 9 10 String result; 11 String imagePath; 12 String image; 13 bool _isBusy = false; 14 OpenFileDialogType _dialogType = OpenFileDialogType.image; 15 16 SourceType _sourceType = SourceType.photoLibrary; 17 bool _allowEditing = false; 18 File _currentFile; 19 String _savedFilePath; 20 bool _localOnly = false; 21 int selectedCameraIndex; 22 List cameras;///```` 23 ///////`````↓ 24Future initCamera(CameraDescription cameraDescription) async { 25 CameraController cameraController; 26 if (cameraController != null) { 27 await cameraController.dispose(); 28 } 29} 30///////``````↑ 31class TakePictureScreen extends StatefulWidget { 32 33 final CameraDescription camera; 34 35 const TakePictureScreen({ 36 Key key, 37 this.camera, 38 }) : super(key: key); 39 40 41 TakePictureScreenState createState() => TakePictureScreenState(); 42} 43 44class TakePictureScreenState extends State<TakePictureScreen> { 45 CameraController _controller; 46 Future<void> _initializeControllerFuture; 47 48 49 void initState() { 50 super.initState(); 51 52 _controller = CameraController( 53 54 widget.camera, 55 56 ResolutionPreset.medium, 57 ); 58 availableCameras().then((value) {////````↓ 59 cameras = value; 60 if(cameras.length > 0){ 61 setState(() { 62 selectedCameraIndex = 0; 63 }); 64 initCamera(cameras[selectedCameraIndex]).then((value) { 65 66 }); 67 } 68 });////````↑ 69 70 _initializeControllerFuture = _controller.initialize(); 71 } 72 73 74 void dispose() { 75 // Dispose of the controller when the widget is disposed. 76 _controller.dispose(); 77 super.dispose(); 78 } 79 80Widget _buildControlBar() { 81 return Row( 82 mainAxisAlignment: MainAxisAlignment.spaceAround, 83 children: <Widget>[ 84 IconButton( 85 color: Colors.white, 86 icon: Icon(Icons.flash_auto), 87 onPressed: () {}, 88 ), 89 FloatingActionButton ( 90 child: Icon(Icons.camera_alt), 91 // Provide an onPressed callback. 92 onPressed: () async { 93 // Take the Picture in a try / catch block. If anything goes wrong, 94 // catch the error. 95 try { 96 // Ensure that the camera is initialized. 97 await _initializeControllerFuture; 98 99 // Attempt to take a picture and get the file `image` 100 // where it was saved. 101 final image = await _controller.takePicture(); 102 103 // If the picture was taken, display it on a new screen. 104 Navigator.push( 105 context, 106 MaterialPageRoute( 107 builder: (context) => DisplayPictureScreen( 108 109 imagePath: image?.path, 110 111 ), 112 ), 113 ); 114 } catch (e) { 115 // If an error occurs, log the error to the console. 116 print(e); 117 } 118 }, 119 ), 120 IconButton( 121 color: Colors.white, 122 icon: Icon(Icons.switch_camera), 123 onPressed:(){onSwitchCamera();} 124 ), 125 ], 126 ); 127 } 128 129 130 onSwitchCamera() {/////```````↓ 131 selectedCameraIndex = 132 selectedCameraIndex < cameras.length - 1 ? selectedCameraIndex + 1 : 0; 133 CameraDescription selectedCamera = cameras[selectedCameraIndex]; 134 initCamera(selectedCamera); 135 }///////```````↑ 136 137 Widget build(BuildContext context) { 138 return Scaffold( 139 appBar: AppBar(title: Text('Take a picture')), 140 // Wait until the controller is initialized before displaying the 141 // camera preview. Use a FutureBuilder to display a loading spinner 142 // until the controller has finished initializing. 143body: Stack( 144 children: <Widget>[ 145 FutureBuilder<void>( 146 future: _initializeControllerFuture, 147 builder: (context, snapshot) { 148 if (snapshot.connectionState == ConnectionState.done) { 149 return CameraPreview(_controller); 150 } else { 151 return Center(child: CircularProgressIndicator()); 152 } 153 }, 154 ), 155 Column( 156 mainAxisAlignment: MainAxisAlignment.end, 157 children: <Widget>[ 158 159 _buildControlBar(), 160 Container( 161 padding: EdgeInsets.symmetric(vertical: 10.0), 162 child: Text( 163 'Tap for photo', 164 style: Theme.of(context) 165 .textTheme 166 .subtitle1 167 .copyWith(color: Colors.white), 168 ), 169 ) 170 ], 171 ), 172 ], 173), 174 175 ); 176 } 177 178 179} 180 181// A widget that displays the picture taken by the user. 182class DisplayPictureScreen extends StatefulWidget { 183 final String imagePath; 184 DisplayPictureScreen({Key key, this.imagePath}) ; 185 186 187 _DisplayPictureScreenState createState() => _DisplayPictureScreenState(imagePath:imagePath); 188 189} 190 191class _DisplayPictureScreenState extends State<DisplayPictureScreen>{ 192 final String imagePath; 193 _DisplayPictureScreenState({this.imagePath}); 194 195 Widget build(BuildContext context) { 196 return Scaffold( 197 appBar: AppBar(title: Text('Display the Picture')), 198 // The image is stored as a file on the device. Use the `Image.file` 199 // constructor with the given path to display the image. 200 body: Image.file(File(imagePath)), 201 202 203 floatingActionButton:FloatingActionButton( 204 onPressed:imagePath==null?null :()=> _saveFile(false), 205 child:Text('save'), 206 207 208 ), 209 210 ); 211 } 212 Future<void> _saveFile(bool useData) async { 213 214 try { 215 setState(() { 216 _isBusy = true; 217 if(imagePath!=null){ 218 _currentFile = File(imagePath); 219 }else{ 220 _currentFile=null; 221 } 222 223 }); 224 final data = useData ? await _currentFile.readAsBytes() : null; 225 final params = SaveFileDialogParams( 226 sourceFilePath: useData ? null : _currentFile.path, 227 data: data, 228 localOnly: _localOnly, 229 fileName: useData ? "untitled" : null); 230 result= await FlutterFileDialog.saveFile(params: params); 231 print(result); 232 } on PlatformException catch (e) { 233 print(e); 234 } finally { 235 setState(() { 236 _savedFilePath = result ?? _savedFilePath; 237 _isBusy = false; 238 }); 239 } 240 } 241} 242 243

試したこと

このコードを参考に内カメラに切り替わる機能を追加しようとしたのですが、うまくいきませんでした。

補足情報(FW/ツールのバージョンなど)

pubspec.yamlのdependenciesは以下の通りです。
camera: ^0.7.0+2
flutter_file_dialog: ^1.0.0
よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問