【发布时间】:2021-12-25 18:58:24
【问题描述】:
我想用我的手机拍照,应该只包含几个字母,然后将其发送到服务器,服务器会将图片转换为文本字符串。
我导入的包:
import 'dart:io';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:image_picker/image_picker.dart';
我目前有这个拍照功能:
// Camera implementation
File? _image;
final ImagePicker _picker = ImagePicker();
Future getImage() async {
final image = await _picker.pickImage(source: ImageSource.camera);
setState(() {
_image = File(image!.path);
});
}
我在这个按钮中使用它:
// Camera button
ElevatedButton.icon(
onPressed: getImage,
icon: const Icon(Icons.camera_alt_rounded),
label: const Text('Scan'),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.green[500]),
textStyle: MaterialStateProperty.all(const TextStyle(fontSize: 26)),
)
)
我已经测试了只向 jsonplaceholder 发送一些数据并且它可以工作,但我无法理解如何将它实现到应该发送到我的服务器的图片。
// Send Data to the Server (TEST VERSION)
postDataTest() async{
try{
var response = await http.post(Uri.parse("https://jsonplaceholder.typicode.com/posts"),
body: {
"id": 1.toString(),
"name": "Hax",
}
);
print(response.body);
} catch(e){
print(e);
}
}
TLDR。我想拍照并发送到服务器。
【问题讨论】: