简体   繁体   English

如何通过 Flutter 中的画廊或相机从所选图像中获取图像(不想在屏幕上显示)?

[英]How to get the Image out of a chosen Image by gallery or camera in Flutter (don't want to show it on screen)?

I have this following Flutter / dart code which let me open the camera / gallery to select a picture.我有以下 Flutter / dart 代码,它让我打开相机/画廊到 select 一张图片。 But i don't want to show the image immediately on the screen (as it is here).但我不想立即在屏幕上显示图像(就像这里一样)。 I want the Image to deal with it, so saving it maybe or getting the image like 'Image.asset("")'.我希望图像来处理它,所以可能会保存它或获取像'Image.asset(“”)'这样的图像。 Main thing i can do something with the image after choosing it.主要的事情是我可以在选择图像后对它做一些事情。

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {


  Future<File> imageFile;
  pickImageFromGallery(ImageSource source) {
    setState(() {

    });
    imageFile = ImagePicker.pickImage(source: source);
  }

  Widget showImage() {
    return FutureBuilder<File>(
      future: imageFile,
      builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
        if (snapshot.connectionState == ConnectionState.done && snapshot.data != null) {
          return Image.file(
            snapshot.data,
            width: 300,
            height: 300,
          );

        }
        else {
          return Text("error");
        }
      },
    );
  }

  bildmachen(ImageSource quelle) {
    pickImageFromGallery(quelle);
    Widget bildAusgabe = showImage();
    Navigator.of(context).push(MaterialPageRoute(
        builder: (BuildContext context) => Bildzeige(bildAusgabe)));

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            showImage,
            RaisedButton(
              child: Text("gehtschon?"),
              onPressed: () {
                bildmachen(ImageSource.gallery);
              },
            ),
          ],
        ),
      ),
      backgroundColor: Colors.yellowAccent,
    );
  }
}

Thanks谢谢

If you want to save the selected image from the ImagePicker in a File object you may do it like this如果要将 ImagePicker 中选择的图像保存在文件 object 中,您可以这样做

File _imageFile;
final picker = ImagePicker();
.
.
.
Future getImage() async {
    final pickedFile = await picker.getImage(source: ImageSource.gallery);

    setState(() {
      if (pickedFile != null) {
        _imageFile = File(pickedFile.path);
      }
    });
  }
// the getImage() method will opens the ImagePicker and the selected image will be stored in _imageFile

I hope this solution helps我希望这个解决方案有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM