当前位置:网站首页>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);
}
}
边栏推荐
- Data representation and calculation (base)
- Gao Zhiwei: data management enables the digital transformation of the transportation industry
- Four types of technical solutions shared by distributed sessions, and their advantages and disadvantages
- Webassembly 2022 questionnaire results are fresh
- A structured random inactivation UNET for retinal vascular segmentation
- Safety is no longer the only selling point. Test drive "slash youth" Volvo C40
- Nutnews developed based on arkui ETS
- 40余岁的边缘老技术,是未来乏力还是掘地而起?
- [machine learning] naive Bayesian code practice
- Briefly describe the difference between heap and stack
猜你喜欢

Configuration file settings for remote connection to Windows version server redis

Summary of pit trampling records and solutions of data warehouse project

PyQt5快速开发与实战 6.1 好软件的三个维度 && 6.2 PyQt5中的布局管理 && 6.3 PyQt5的绝对位置布局

Asp graduation project - based on C # +asp Net+sqlserver laboratory reservation system design and Implementation (graduation thesis + program source code) - Laboratory Reservation System

《UnityShader入门精要》总结(2):初级篇

MySQL事务与MVCC如何实现的隔离级别

远程连接windows版本服务器redis的配置文件设置

浅谈契约测试

MySQL converts some table names to uppercase

分布式Session共享的4类技术方案,与优劣势比较
随机推荐
201803-3 CCF URL映射 满分题解
Asp graduation project - based on C # +asp Design and implementation of enterprise investment value analysis system based on. Net + sqlserver (graduation thesis + program source code) -- enterprise in
【云驻共创】【HCSD大咖直播】亲授大厂面试秘诀
Network security (6)
如何为OpenHarmony做贡献
【机器学习】朴素贝叶斯代码练习
36. JS动画
View port occupancy
[Bert multi label text classification practice] I - overview of practical projects
Appendix 2 – some simple exercises
文件重命名后,怎样将新旧文件名及所在位置导出到excel
mysql将部分表名统一转换为大写
C# 使用RestSharp库实现POST请求
What is the key to fast multi tag user profile analysis?
NFA determination and DFA minimization based on C language
smart-webcomponents 14.2.0 Crack
Nutnews developed based on arkui ETS
Floweable foundation Chapter 1
AxureRP原型设计 快速开始
Pyqt5 rapid development and practice 6.5 qgridlayout (grid layout)