Created
February 14, 2021 11:09
-
-
Save doyle-flutter/dbc3d01aafa603c24825aca85cf8875b to your computer and use it in GitHub Desktop.
Android & Flutter 05 Camera++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:typed_data'; | |
import 'package:flutter/gestures.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
void main(){ | |
WidgetsFlutterBinding.ensureInitialized(); | |
GestureBinding.instance.resamplingEnabled = true; | |
return runApp(MaterialApp(home: MyApp(),)); | |
} | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
final MethodChannel channel = new MethodChannel("app.james.cam/cam"); | |
MethodChannel emitChannel; | |
Uint8List data; | |
String btnTxt = "촬영"; | |
@override | |
void initState() { | |
emitChannel = new MethodChannel("app.james.cam/cam2")..setMethodCallHandler((call){ | |
if(call.method == "sendImg"){ | |
final Uint8List _data = call.arguments; | |
setState(() { | |
this.data = _data; | |
btnTxt = "지우기"; | |
}); | |
} | |
return; | |
}); | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(), | |
body: Container( | |
child: Stack( | |
children: [ | |
Positioned( | |
width: 200.0, | |
height: 200.0, | |
top: 0, | |
left: 0, | |
child: Container( | |
decoration: this.data == null | |
? BoxDecoration(color: Colors.grey,) | |
: BoxDecoration( | |
image: DecorationImage( | |
image: MemoryImage(this.data,), | |
fit: BoxFit.cover | |
) | |
) | |
), | |
), | |
Positioned( | |
top: 0, | |
left: 5, | |
child: RaisedButton( | |
child: Text(btnTxt), | |
onPressed: _click, | |
), | |
), | |
], | |
), | |
) | |
); | |
} | |
Future<void> _click() async{ | |
if(this.data != null){ | |
setState(() { | |
this.data = null; | |
btnTxt = "촬영"; | |
}); | |
return; | |
} | |
await channel.invokeMethod('cam'); | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment