在抖动中将图像转换为base64

时间:2020-03-13 07:57:07

标签: image flutter base64

我用手机的相机拍照,然后调整大小并将其转换为base64字符串。但是,经过这些操作之后,我得到的base64字符串似乎无效。 我尝试将其转换回此网站https://codebeautify.org/base64-to-image-converter上的图像。单击生成图像后,没有任何反应,但是他们网站上的示例正常工作。在其他网站上尝试过,仍然没有运气。 我的代码:

import 'package:image/image.dart' as img;
import 'package:image_picker/image_picker.dart';

  File _photo;
  String photoBase64;

  Future getImage(ImageSource source) async {
    var photo = await ImagePicker.pickImage(source: source);

    img.Image image = img.decodeImage(photo.readAsBytesSync());
    img.Image imageResized = img.copyResize(image, width: 120);

    setState(() {
      _photo = photo;

    List<int> imageBytes = imageResized.getBytes();
    photoBase64 = base64Encode(imageBytes);
    });
  } 

我也尝试过base64UrlEncode(),但是问题仍然存在。我尝试将其转换回图像的字符串是photoBase64。我的目标是稍后将其发送到POST请求的正文中。我到底在做什么错?

谢谢

2 个答案:

答案 0 :(得分:1)

您可以在下面复制粘贴运行完整代码
您可以使用软件包https://pub.dev/packages/flutter_native_image获取调整大小的图像

imageResized = await FlutterNativeImage.compressImage(photo.path,
    quality: 100, targetWidth: 120, targetHeight: 120);
...
List<int> imageBytes = imageResized.readAsBytesSync();

工作演示

enter image description here enter image description here enter image description here

完整代码

import 'package:flutter/material.dart';
import 'package:image/image.dart' as img;
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'dart:convert';
import 'package:flutter_native_image/flutter_native_image.dart';

void main() => runApp(MyApp());

File _photo;
String photoBase64;

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  File imageResized;

  Future getImage(ImageSource source) async {
    var photo = await ImagePicker.pickImage(source: source);

    imageResized = await FlutterNativeImage.compressImage(photo.path,
        quality: 100, targetWidth: 120, targetHeight: 120);

    setState(() {
      _photo = photo;

      List<int> imageBytes = imageResized.readAsBytesSync();
      photoBase64 = base64Encode(imageBytes);
      print(photoBase64);
    });
  }

  void _incrementCounter() {
    getImage(ImageSource.camera);
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            imageResized == null ? Container() : Image.file(imageResized),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

答案 1 :(得分:0)

我的解决方案

  Future getImage() async {
    pickedFile =
        await _picker.getImage(source: ImageSource.gallery, imageQuality: 50);
    setState(() {
      if (pickedFile != null) {
        file = File(pickedFile!.path); 
        final bytes =
            file!.readAsBytesSync(); 
        img64 = base64Encode(bytes);
      } else {
        print('No image selected.');
      }
    });
  }