当前位置:网站首页>Shutter -- use camera (continuously updating)
Shutter -- use camera (continuously updating)
2022-07-29 09:29:00 【antu58】
Currently in use
camera: ^0.9.8+1
import 'dart:ffi';
import 'dart:io';
import 'dart:typed_data';
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'package:ui_base/button/custom_button.dart';
import 'package:ui_base/extension/ext.dart';
import 'package:ui_base/widget/base_state.dart';
import 'package:ui_base/widget/ui_text.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:photo_view/photo_view.dart';
late List<CameraDescription> _cameras;
List<XFile> _photos = [];
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
_cameras = await availableCameras();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: CameraApp(),
);
}
}
/// CameraApp is the Main Application.
class CameraApp extends StatefulWidget {
/// Default Constructor
const CameraApp({Key? key}) : super(key: key);
@override
State<CameraApp> createState() => _CameraAppState();
}
class _CameraAppState extends BaseState<CameraApp> with WidgetsBindingObserver {
late CameraController controller;
@override
void initState() {
super.initState();
initCamera(0);
}
initCamera(int index, {double zoom = 1}) {
controller = CameraController(_cameras[index], ResolutionPreset.max);
controller.initialize().then((_) {
if (!mounted) {
return;
}
controller.setZoomLevel(zoom);
refresh();
}).catchError((Object e) {
if (e is CameraException) {
switch (e.code) {
case 'CameraAccessDenied':
print('User denied camera access.');
break;
default:
print('Handle other errors.');
break;
}
}
});
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
XFile? _lastImageFile;
@override
Widget build(BuildContext context) {
if (!controller.value.isInitialized) {
return Container();
}
return Scaffold(
body: SafeArea(child: Column(children: [
CameraPreview(controller).expanded(),
SizedBox(height: midPadding,),
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween,children: [
UIButton(
width: 50,
height: 50,
child: _lastImageFile != null ? Image.file(File(_lastImageFile!.path), fit: BoxFit.fill,) : null,
onTap: () {
if (_lastImageFile == null) return;
//navigatorPush(PhotoListView(_photos.reversed.toList()));
},
),
UIButton(
width: 64,
height: 64,
allRadius: 32,
backgroundColor: Colors.red,
onTap: () async {
try {
XFile file = await controller.takePicture();
Uint8List bytes = await file.readAsBytes();
final result = await ImageGallerySaver.saveImage(
bytes,
quality: 100,
name: "hello");
_lastImageFile = file;
_photos.add(file);
refresh();
} catch(e) {
}
},
),
SizedBox(width: 50, height: 50,)
], ).size(height: 200).padding(padding: EdgeInsets.only(left: 50, right: 50)),
],)),
backgroundColor: Colors.black,
);
void actionCard() {
}
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
final CameraController cameraController = controller;
// App state changed before we got the chance to initialize.
if (!cameraController.value.isInitialized) {
return;
}
if (state == AppLifecycleState.inactive) {
cameraController.dispose();
} else if (state == AppLifecycleState.resumed) {
initCamera(0);
}
}
Widget lensListView() {
return ListView.separated(
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) {
CameraDescription? description = _cameras[index];
return UIButton(
onTap: () {
initCamera(index);
},
padding: EdgeInsets.only(left: smallPadding, right: smallPadding),
child: UIText(text: " The lens ${index + 1}", color: Colors.white,),
);
},
separatorBuilder: (context, index) {
return SizedBox(width: smallPadding,);
},
itemCount: _cameras.length
).size(height: 30);
}
}
边栏推荐
- Use cpolar to publish raspberry pie web pages (improvement of cpolar tunnel)
- Unity Xchart3.0基本用法快速上手
- 如何介绍自己的项目经验?
- Implementation of DFA string recognition based on C language simulation
- Summary of introduction to unityshader (2): Beginner Level
- 先序遍历/后序遍历确定树的大致形态
- 怎么样的框架对于开发者是友好的?
- 23考研人撑住!考研第一波弃考高峰期已经到来!
- Pyqt5 rapid development and practice 6.5 qgridlayout (grid layout)
- Retinal Vessel Segmentation via a Semantics and Multi-Scale Aggregation Network
猜你喜欢
redis可视化工具读取数据乱码问题解决
网络安全(6)
Will the modified data be updated when it is the same as the original data?
当 update 修改数据与原数据相同时会被更新吗?
【机器学习】逻辑回归代码练习
[Bert multi label text classification practice] I - overview of practical projects
How does alternates achieve high-performance publish and subscribe?
36. JS动画
Flowable UI制作流程图
Pyqt5 rapid development and practice 6.4 qboxlayout (box layout)
随机推荐
Safety is no longer the only selling point. Test drive "slash youth" Volvo C40
远程连接windows版本服务器redis的配置文件设置
Explanation of trie tree (dictionary tree)
VS2015采用loadlibrary方式调用dll库
Introduction to translation professional qualification (level) examination
【BERT-多标签文本分类实战】之一——实战项目总览
基于ArkUI eTS开发的坚果新闻(NutNews)
PyQt5快速开发与实战 6.1 好软件的三个维度 && 6.2 PyQt5中的布局管理 && 6.3 PyQt5的绝对位置布局
不用Swagger,那我用啥?
[Apple Developer account]06 after transferring the developer account, the annual fee of the developer is automatically renewed
Unity xchart3.0 basic usage quick start
dataframe.to_sql() 一次性插入过多报错
怎么样的框架对于开发者是友好的?
存算一体与存内计算计算杂谈
pytest+allure生成测试报告
The gold content of PMP certificate has been increased again and included in the scope of Beijing work residence permit
简述堆和栈的区别
文件重命名后,怎样将新旧文件名及所在位置导出到excel
Gao Zhiwei: data management enables the digital transformation of the transportation industry
23考研人撑住!考研第一波弃考高峰期已经到来!