node.js - 如何在颤动中将图像转换为字符串
问题描述
所以我有这个颤振应用程序,这个人需要从应用程序的画廊上传一些照片。如何将这些图像存储在应用程序中并将它们转换为字符串以便保存到我的数据库中?(蒙古数据库)
我想要:
- 将图像保存为一个值
- 将此值转换为字符串
- 将字符串图像保存到“受益人”类中
class BeneficiaryServices extends StatefulWidget {
@override
_beneficiaryState createState() => _beneficiaryState();
}
class _beneficiaryState extends State<BeneficiaryServices>
with View<BeneficiaryServices> {
var data;
bool autoValidate = true;
bool readOnly = false;
bool showSegmentedControl = true;
File _image;
final picker = ImagePicker();
beneficiaryservicesController controller =
new beneficiaryservicesController();
final _formKey = GlobalKey<FormState>();
final _fbKey = GlobalKey<FormBuilderState>();
Future save() async {
final pickedFile = await picker.getImage(source: ImageSource.camera);
setState(() {
if (pickedFile != null) {
_image = File(pickedFile.path);
} else {
print('No image selected.');
}
});
var res = await http.post(Uri.parse("http://192.168.56.1:8080/signin"),
headers: <String, String>{
'Context-Type': 'application/json;charSet=UTF-8'
},
body: <String, String>{
});
print(res.body);
}
Beneficiary beneficiary = Beneficiary('', '', '', '');
@override
Widget getBody() {
return Container(
height: MediaQuery.of(context).size.height - 200,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
SizedBox(
height: 20.0,
),
Padding(
padding: const EdgeInsets.all(8.0),
child: _image == null
? Text('Upload Proof of address')
: Image.file(_image),
),
FloatingActionButton(
onPressed: getImage,
tooltip: 'Pick Image',
child: Icon(Icons.add_a_photo),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: _image == null
? Text('Upload Birth Certificate')
: Image.file(_image),
),
FloatingActionButton(
onPressed: getImage,
tooltip: 'Pick Image',
child: Icon(Icons.add_a_photo),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: _image == null
? Text('Upload CEB/CWA/Phone Bill')
: Image.file(_image),
),
FloatingActionButton(
onPressed: getImage,
tooltip: 'Pick Image',
child: Icon(Icons.add_a_photo),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: _image == null
? Text('Upload Proof of Loan (if applicable)')
: Image.file(_image),
),
FloatingActionButton(
onPressed: getImage,
tooltip: 'Pick Image',
child: Icon(Icons.add_a_photo),
),
Padding(
padding: const EdgeInsets.all(24.0),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
primary: const Color(0xffde0486),
),
child: Container(
margin: const EdgeInsets.all(8),
child: const Text(
'Submit Application',
style: TextStyle(
color: Colors.white,
package: 'flutter_credit_card',
),
),
),
onPressed: () {
print('valid!');
},
),
),
],
),
),
);
}
@override
Controller getController() {
return this.controller;
}
@override
InstanceNames getName() {
return InstanceNames.ZakatCalculator;
}
void getImage() {}
}
解决方案
final imageBytes = await pickedFile.readAsBytes();
final base64Image = base64Encode(imageBytes);
Where返回所提供字节数组base64Encode
的 base64 编码表示,类型为 。String
推荐阅读
- spring-data-jpa - 如何减少 Atomikos 中的 getConnection 时间消耗
- node.js - 请解释在使用 .then() 和 Promise 时调用语法的细微差别的巨大影响
- azerothcore - 如何安装 azerothcore 模块,例如缓冲区 npc 模块
- c++ - DLL 项目未在 Visual Studio 2019 中构建
- python - 使用pytest测试同一个类的不同实例
- r - 将矩阵转换为分配特定列的数组
- javascript - 嵌套在函数内的异步函数
- excel - 当列标题名称的顺序不同时,将一个表插入另一个表
- node.js - 如何像在站点中一样发出 POST 请求
- coq - 复杂函数的平等