How can I show a CameraPreview widget Immediately,instead of inited with a black block?

661 views
Skip to first unread message

huang

unread,
Jul 9, 2018, 6:38:31 AM7/9/18
to Flutter Dev
import 'package:camera/camera.dart';
import 'dart:async';
import 'package:flutter/material.dart';

class CameraMgr{
 CameraMgr._();
 CameraController _cameraCtrl;

  Future<CameraController> openCamera() async {
   CameraDescription cameraDescription;
   List<CameraDescription> cameras = await availableCameras();
   for (CameraDescription xx in cameras){
     if(xx.lensDirection==CameraLensDirection.front){
       cameraDescription = xx;
       break;
     }
   }
   if(cameraDescription==null){
     return null;
   }
   if (_cameraCtrl != null) {
     await _cameraCtrl.dispose();
   }
   _cameraCtrl = new CameraController(cameraDescription, ResolutionPreset.low)..addListener(() {
     if (_cameraCtrl.value.hasError) {
       print("");
     }
   });

    try {
     await _cameraCtrl.initialize();
   } on CameraException catch (e) {
     print(e);
   }
   return _cameraCtrl;
 }
 close()async {
   if (_cameraCtrl != null) {
     await _cameraCtrl.dispose();
   }
   _cameraCtrl = null;
 }
}
CameraMgr myCameraMgr = new CameraMgr._();

class TestCamera extends StatefulWidget{
 @override
 State<StatefulWidget> createState() {
   return new _TestCameraState();
 }
}
class _TestCameraState extends State<TestCamera>{
 @override
 void initState() {
   super.initState();
   myCameraMgr.openCamera();
 }
 @override
 void dispose(){
   super.dispose();
   myCameraMgr.close();
 }
 @override
 Widget build(BuildContext context) {
   return new MaterialApp(
     home: new Scaffold(
       backgroundColor: Colors.blueAccent,
       body: new Center(
         child: new LayoutBuilder(
           builder: (BuildContext ctx,BoxConstraints bcts){
             return new GestureDetector(
               child: new Container(
                 height: 80.0,
                 color: Colors.red[100],
                 child: new Text("touch to show dialog"),
               ),
               onTap: ()async{
                 CameraController ctrl = await myCameraMgr.openCamera();
                 showDialog(
                   context: ctx,
                   builder: (BuildContext _){
                     return new Container(
                       height: bcts.maxHeight,
                       width: bcts.maxWidth,
                       child: new DialogCamera(ctrl),
                     );
                   }
                 );
               },
             );
           },
         )
       ),
     ),
   );
 }
}

class DialogCamera extends StatelessWidget{
 DialogCamera(this.ctrl);
 final CameraController ctrl;
 @override
 Widget build(BuildContext context) {
   return new Container(
     margin: new EdgeInsets.all(150.0),
     child: new Center(
       child: new CameraPreview(this.ctrl),
     ),
   );
 }
}

void main() {
 runApp(new TestCamera());
}


There is my test case above. In my app,I need show and take photo by CameraPreview frequently,but the Initialization of the widget is too slow.
I have tried to cache the CameraController,but it change nothing.
Is there some ways to rending the widget in advance?or other ways to solve this problem?
Thank you very much!

huang

unread,
Jul 9, 2018, 9:57:58 PM7/9/18
to Flutter Dev
Sorry,it's my fault,I made a mistake.
if(_cameraCtrl!=null){
return _cameraCtrl;
}



在 2018年7月9日星期一 UTC+8下午6:38:31,huang写道:
Reply all
Reply to author
Forward
0 new messages