Image 如何使用camera.dart和camera_screen.dart通过相机捕捉图像,然后将其移动到我的颤振应用程序中的占位符

Image 如何使用camera.dart和camera_screen.dart通过相机捕捉图像,然后将其移动到我的颤振应用程序中的占位符,image,firebase,flutter,storage,Image,Firebase,Flutter,Storage,我使用camera_screen.dart进行相机预览,并使用以下代码捕获图像 实际上,我正在使用图像采集器,我的应用程序在相机拍摄后崩溃,因为画廊图像没有问题 所以我想用相机,省道和相机屏幕省道 请帮助我按照上述步骤放置捕获的图像,并将图像放置在我的占位符或圆形头像中。因此,我想进一步将图像存储到firebase存储中 void updateUI(dynamic myPath) { setState(() { if (myPath == null) {

我使用camera_screen.dart进行相机预览,并使用以下代码捕获图像

实际上,我正在使用图像采集器,我的应用程序在相机拍摄后崩溃,因为画廊图像没有问题

所以我想用相机,省道和相机屏幕省道

请帮助我按照上述步骤放置捕获的图像,并将图像放置在我的占位符或圆形头像中。因此,我想进一步将图像存储到firebase存储中

     void updateUI(dynamic myPath) {
    setState(() {
      if (myPath == null) {
        myPath = 'images/cam.jpg';
        visibilityFlag2 = false;
        visibilityFlag1 = true;
        print('mypathh/.................$myPath');
        return;
      }
      //  myPath = 'images/exit.png';
      myppath = myPath;
      visibilityFlag2 = true;
      visibilityFlag1 = false;
      print('mypathhfgdfgdf/.................$myppath');
    });
  }




  void openImagePickerModal(BuildContext context) {
    final flatButtonColor = Theme
        .of(context)
        .primaryColor;
    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return Container(
            height: 180.0,
            padding: EdgeInsets.all(20.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Visibility(
                  visible: visibilityFlag1,
                  child: Center(

                    child: FlatButton(

                     child: Text("Chose Image from Camera"),

                      onPressed: () async {
                        var ppath = await Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (BuildContext context) {
                              return CameraScreen();
                            },
                          ),
                        );


                        updateUI(ppath);
                        print('sdfbsdfbs.......$ppath}');
                      },


                      color: Colors.green,
                      textColor: Colors.white,

                  ),
                ),
                ),
                Visibility(
                  visible: visibilityFlag2,
                  child: Center(
                    child: GestureDetector(
                      child: Container(
                        margin: EdgeInsets.all(10.0),
                        height: 100.0,
                        width: 200.0,
                        child: Image.file(
                          File(myppath),
                        ),
                      ),
                      onTap: () async {
                        var ppath = await Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (BuildContext context) {
                              return CameraScreen();
                            },
                          ),
                        );
                        updateUI(ppath);
                        print('sdfbsdfbs.......$ppath}');
                        FirebaseStorage fs = FirebaseStorage.instance;

                        StorageReference rootReference = fs.ref();
                        Random random = new Random();
                        int randomNumber = random.nextInt(1000);
                        final StorageReference pictureFolderRef = rootReference
                            .child("images").child("image${randomNumber}");

                        pictureFolderRef.putFile(_image).onComplete.then((storageTask) async
                        {
                          String link = await storageTask.ref.getDownloadURL();
                          print("Image uploaded");
                          setState(() {
                            imageLink = link;

                          });
                        });

                      },
                    ),
                  ),
                ),



                FlatButton(
                  child: Text("Chose Image From Gallery"),
                  onPressed: () async {
                     getImageFile(ImageSource.gallery);
                   // _image = await ImagePicker.pickImage(source: ImageSource.gallery);

                    FirebaseStorage fs = FirebaseStorage.instance;

                    StorageReference rootReference = fs.ref();
                    Random random = new Random();
                    int randomNumber = random.nextInt(1000);
                    final StorageReference pictureFolderRef = rootReference
                        .child("images").child("image${randomNumber}");

                    pictureFolderRef.putFile(_image).onComplete.then((storageTask) async
                    {
                      String link = await storageTask.ref.getDownloadURL();
                      print("Image uploaded");
                      setState(() {
                        imageLink = link;

                      });
                    });
                  },
                  color: Colors.green,
                  textColor: Colors.white,
                ),


              ],
            ),
          );
        });
    //Navigator.of(context).pop();
  }


}

然后我必须将摄像头和画廊中的图像存储到firebase存储中。请指导我。

我唯一的问题是如何在我的代码中点击并以圆形头像显示捕获的图片。请指导我,我唯一的问题是如何返回并在我的代码中以圆形化身显示捕获的图片。请使用下面的代码camera_screen.dart和preview_screen.dart引导meNow Iam,如示例所示。
 class CameraScreen extends StatefulWidget {
  @override
  _CameraScreenState createState() {
    return _CameraScreenState();
  }
}

class _CameraScreenState extends State {
  CameraController controller;
  List cameras;
  int selectedCameraIdx;
  String imagePath;

  @override
  void initState() {
    super.initState();
    availableCameras().then((availableCameras) {
      cameras = availableCameras;

      if (cameras.length > 0) {
        setState(() {
          selectedCameraIdx = 0;
        });

        _initCameraController(cameras[selectedCameraIdx]).then((void v) {});
      } else {
        print("No camera available");
      }
    }).catchError((err) {
      print('Error: $err.code\nError Message: $err.message');
    });
  }

  Future _initCameraController(CameraDescription cameraDescription) async {
    if (controller != null) {
      await controller.dispose();
    }

    controller = CameraController(cameraDescription, ResolutionPreset.high);

    // If the controller is updated then update the UI.
    controller.addListener(() {
      if (mounted) {
        setState(() {});
      }

      if (controller.value.hasError) {
        print('Camera error ${controller.value.errorDescription}');
      }
    });

    try {
      await controller.initialize();
    } on CameraException catch (e) {
      _showCameraException(e);
    }

    if (mounted) {
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Capture image'),
        backgroundColor: kActiveCardColour,
      ),
      body: Container(
        child: SafeArea(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              Expanded(
                flex: 1,
                child: _cameraPreviewWidget(),
              ),
              SizedBox(height: 10.0),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  _cameraTogglesRowWidget(),
                  _captureControlRowWidget(context),
                  Spacer()
                ],
              ),
              SizedBox(height: 20.0)
            ],
          ),
        ),
      ),
    );
  }

  /// Display Camera preview.
  Widget _cameraPreviewWidget() {
    if (controller == null || !controller.value.isInitialized) {
      return const Text(
        'Loading',
        style: TextStyle(
          color: Colors.white,
          fontSize: 20.0,
          fontWeight: FontWeight.w900,
        ),
      );
    }

    return AspectRatio(
      aspectRatio: controller.value.aspectRatio,
      child: CameraPreview(controller),
    );
  }

  /// Display the control bar with buttons to take pictures
  Widget _captureControlRowWidget(context) {
    return Expanded(
      child: Align(
        alignment: Alignment.center,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          mainAxisSize: MainAxisSize.max,
          children: [
            FloatingActionButton(
                child: Icon(Icons.camera_alt),
                backgroundColor: kActiveCardColour,
                onPressed: () {
                  _onCapturePressed(context);
                })
          ],
        ),
      ),
    );
  }

  /// Display a row of toggle to select the camera (or a message if no camera is available).
  Widget _cameraTogglesRowWidget() {
    if (cameras == null || cameras.isEmpty) {
      return Spacer();
    }

    CameraDescription selectedCamera = cameras[selectedCameraIdx];
    CameraLensDirection lensDirection = selectedCamera.lensDirection;

    return Expanded(
      child: Align(
        alignment: Alignment.centerLeft,
        child: FlatButton.icon(
            onPressed: _onSwitchCamera,
            icon: Icon(_getCameraLensIcon(lensDirection)),
            label: Text(
                "${lensDirection.toString().substring(lensDirection.toString().indexOf('.') + 1)}")),
      ),
    );
  }

  IconData _getCameraLensIcon(CameraLensDirection direction) {
    switch (direction) {
      case CameraLensDirection.back:
        return Icons.camera_rear;
      case CameraLensDirection.front:
        return Icons.camera_front;
      case CameraLensDirection.external:
        return Icons.camera;
      default:
        return Icons.device_unknown;
    }
  }

  void _onSwitchCamera() {
    selectedCameraIdx =
    selectedCameraIdx < cameras.length - 1 ? selectedCameraIdx + 1 : 0;
    CameraDescription selectedCamera = cameras[selectedCameraIdx];
    _initCameraController(selectedCamera);
  }

  void _onCapturePressed(context) async {
    // Take the Picture in a try / catch block. If anything goes wrong,
    // catch the error.
    try {
      // Attempt to take a picture and log where it's been saved
      final path = join(
        // In this example, store the picture in the temp directory. Find
        // the temp directory using the `path_provider` plugin.
        (await getTemporaryDirectory()).path,
        '${DateTime.now()}.jpg',
      );
      print(path);
      await controller.takePicture(path);

      // If the picture was taken, display it on a new screen
/*
      Navigator.pop(
          context,
          MaterialPageRoute(
            builder: (context) => HomeNurseRegistration(
              myPath: path,
            ),
          ),);
*/
      Navigator.pop(context, path);
    } catch (e) {
      // If an error occurs, log the error to the console.
      print(e);
    }
  }

  void _showCameraException(CameraException e) {
    String errorText = 'Error: ${e.code}\nError Message: ${e.description}';
    print(errorText);

    print('Error: ${e.code}\n${e.description}');
  }
}
  Align(
              alignment: Alignment.center,

              child: CircleAvatar(
                radius: 100,
                backgroundColor: Color(0xff476cfb),
                child: ClipOval(
                  child: new SizedBox(
                    width: 180.0,
                    height: 180.0,
                    child: (_image!=null)?Image.file(_image, fit: BoxFit.fill,):Image.network(imageLink, fit: BoxFit.fill,),
                  ),
                ),
              ),

            ),
            Padding(
              padding: EdgeInsets.only(top: 60.0),
              child: IconButton(
                icon: Icon(
                  FontAwesomeIcons.camera,
                  size: 30.0,
                ),
                onPressed: () {
                  openImagePickerModal(context);
                  //uploadPic(context);
                },
              ),
            ),
            SizedBox(
              height: 20,
            ),