Flutter:使用相机插件拍照后,Image.file无法读取图像文件

时间:2019-04-20 04:22:09

标签: flutter flutter-layout flutter-dependencies flutter-test flutter-plugin

导入'dart:async';

导入'dart:io';

导入“ package:flutter / material.dart”;

导入'package:camera / camera.dart';

导入'package:path / path.dart';

导入'package:path_provider / path_provider.dart';

CustomCamera类扩展了StatefulWidget {

@override

_CameraAppState createState()=> _CustomCameraState();

}

_CustomCameraState类扩展了状态{

列出摄像机;

CameraController控制器;

bool _isReady = false;

布尔切换=假;

@override

void initState(){

super.initState();

_setupCameras(switched);

}

未来_setupCameras(布尔切换)异步{

try {

  // initialize cameras.

  cameras = await availableCameras();

  // initialize camera controllers.

  controller = new CameraController(cameras[0], ResolutionPreset.medium);

  await controller.initialize();

} on CameraException catch (_) {

  // do something on error.

}

if (!mounted) return;

setState(() {

  _isReady = true;

});

}

字符串newImage;

@override

小部件构建(BuildContext上下文){

if (!_isReady) return Container();

return MaterialApp(

  debugShowCheckedModeBanner: false,

  home: Scaffold(

    body: Stack(

  children: <Widget>[

    Container(

      height: MediaQuery.of(context).size.height,

      width: MediaQuery.of(context).size.width,

      child: AspectRatio(

              aspectRatio: controller.value.aspectRatio,

              child: CameraPreview(controller)

        ),

    ),

    Align(

      alignment: Alignment.bottomCenter,

      child: Padding(

      padding: const EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 26.0),

        child: Row(

          mainAxisAlignment: MainAxisAlignment.spaceEvenly,

          children: <Widget>[

            InkWell(

                child: Container(

                height: 80.0,

                width: 60.0,

                color: Colors.black,

                child: newImage == null ? 

Image.asset('assets / icons / city.png',适合:BoxFit.cover,):

Image.file(File(“ $ newImage”))

              ),

              onTap: (){


              },

            ),

            Opacity(

              opacity: 0.9,

              child: Image.asset('assets/buttons/videoButton.png',scale: 

12.5)

              ),

            InkWell(

              child: Image.asset('assets/buttons/TakePhotoButton.png',scale: 4.5),

              onTap: () async{


try {

  await controller.initialize();

  // Construct the path where the image should be saved using the path

  // package.

  final path = join(

    // In this example, store the picture in the temp directory. Find


    // the temp directory using the `path_provider` plugin.

    (await getApplicationDocumentsDirectory()).path,

    'hello.png',
  );
  //Set the variable
  setState(() {
    newImage = path;
  });
  var echo = path.toString();
  // Attempt to take a picture and log where it's been saved
  await controller.takePicture(path);
  print("$echo this is the path");
} catch (e) {
  // If an error occurs, log the error to the console.
  print(e);
}
              },
              ),
            Opacity(
              opacity: 0.9,
              child: InkWell(
                child: Image.asset('assets/buttons/flipCamera.png',scale: 14.5),
                onTap: (){
                  setState((){
                   switched = true; 
                  });
                },
                )
              ),
            Opacity(
              opacity: 1.0,
              child: Image.asset('assets/buttons/Livebutton.png',scale: 14.5)
              ),
          ],
        ),
      ),
    ),
    Align(
      alignment: Alignment.topRight,
      child: Padding(
        padding: const EdgeInsets.fromLTRB(0.0, 50.0, 26.0, 0.0),
        child: InkWell(
                child: Icon(
                Icons.arrow_forward_ios,
                color: Colors.white,
                size: 30.0,
              ),
          onTap: () => Navigator.pop(context)
        ),
      ),
    )
  ],
),
  ),
);

} }

enter image description here

0 个答案:

没有答案