Skip to content

Commit

Permalink
[Feat] Create camera ui (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
kminji127 committed Feb 16, 2023
1 parent 7f290be commit 6be7441
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 16 deletions.
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ android {
applicationId "com.example.i_can_read"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
minSdkVersion flutter.minSdkVersion
minSdkVersion 21
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
11 changes: 10 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:i_can_read/screens/home_screen.dart';
import 'package:i_can_read/screens/camera/camera_screen.dart';
import 'package:i_can_read/screens/recommend/recommend_screen.dart';

void main() {
List<CameraDescription> cameras = [];

Future<void> main() async {
try {
WidgetsFlutterBinding.ensureInitialized();
cameras = await availableCameras();
} on CameraException catch (e) {
print('Error in fetching the cameras: $e');
}
runApp(const MyApp());
}

Expand Down
77 changes: 63 additions & 14 deletions lib/screens/camera/camera_screen.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,72 @@
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:i_can_read/configs/color.dart';
import 'package:i_can_read/main.dart';
import 'package:i_can_read/screens/camera/display_picture_screen.dart';

class CameraScreen extends StatefulWidget {
const CameraScreen({super.key});

@override
State<CameraScreen> createState() => _CameraScreenState();
}

class _CameraScreenState extends State<CameraScreen> {
// 카메라의 현재 출력물을 보여주는 컨트롤러
late CameraController _controller;
late Future<void> _initializeControllerFuture;

@override
void initState() {
super.initState();
// 특정 카메라 선택, 해상도 지정
_controller = CameraController(cameras[0], ResolutionPreset.max);
// 컨트롤러 초기화
_initializeControllerFuture = _controller.initialize();
}

// 생명주기 종료 시 컨트롤러 해제
@override
void dispose() {
_controller.dispose();
super.dispose();
}

class CameraScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('메뉴판 촬영하기'),
TextButton(
child: const Text('홈 화면'),
onPressed: () {
Navigator.of(context).pushNamed('/');
},
),
],
),
body: FutureBuilder<void>(
future: _initializeControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return CameraPreview(_controller);
} else {
return const Center(child: CircularProgressIndicator());
}
},
),
floatingActionButton: FloatingActionButton.large(
onPressed: () async {
try {
await _initializeControllerFuture;
final image = await _controller.takePicture();

if(!mounted) return;

await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => DisplayPictureScreen(imagePath: image.path),
)
);
} catch (e) {
print(e);
}
},
backgroundColor: ColorPalette.primaryGold,
foregroundColor: ColorPalette.black20,
child: const Icon(Icons.camera_alt),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
);
}
}

0 comments on commit 6be7441

Please sign in to comment.