当前位置:网站首页>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);
}
}
边栏推荐
- 【BERT-多标签文本分类实战】之一——实战项目总览
- pytest+allure生成测试报告
- 使用cpolar发布树莓派网页(cpolar功能的完善)
- Data representation and calculation (base)
- How to choose effective keywords
- WebAssembly 2022 问卷调查结果新鲜出炉
- Summary of some experiences in the process of R & D platform splitting
- Redis series 3: highly available master-slave architecture
- Asp graduation project - based on C # +asp Net+sqlserver laboratory reservation system design and Implementation (graduation thesis + program source code) - Laboratory Reservation System
- 36. JS动画
猜你喜欢
Jetpack Glance? The spring of widgets is coming

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

No swagger, what do I use?

Flowable 高级篇

Discussion on the integration of storage and calculation and the calculation in storage

How to introduce your project experience?

【BERT-多标签文本分类实战】之一——实战项目总览

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

OpenCV图像处理基础操作

Floweable foundation Chapter 1
随机推荐
分布式Session共享的4类技术方案,与优劣势比较
Flowable UI制作流程图
用户身份标识与账号体系实践
The gold content of PMP certificate has been increased again and included in the scope of Beijing work residence permit
[Bert multi label text classification practice] I - overview of practical projects
dataframe. to_ Sql() inserts too many errors at one time
How to query express logistics and filter out no information doc No. to delete or copy
PyQt5快速开发与实战 6.1 好软件的三个维度 && 6.2 PyQt5中的布局管理 && 6.3 PyQt5的绝对位置布局
Excellent package volume optimization tutorial
【Unity入门计划】C#与Unity-了解类和对象
23 postgraduate entrance examination people hold on! The first wave of exam abandonment peak has arrived!
Pyqt5 rapid development and practice 6.1 three dimensions of good software & 6.2 layout management in pyqt5 & 6.3 absolute location layout of pyqt5
乱打日志的男孩运气怎么样我不知道,加班肯定很多
"Defects" of prototype chain inheritance and constructor inheritance
Flowable 高级篇
[unity entry program] C # and unity - understand classes and objects
[Yunzhu co creation] [hcsd live broadcast] teach the interview tips of big companies in person
OpenCV图像处理基础操作
使用cpolar发布树莓派网页(cpolar隧道的完善)
How to contribute to openharmony