我目前正在使用 Firebase 云函数,我希望可以在 Call 中使用它来检查图像是否包含成人内容,并查看我设法创建此函数的一些教程:
exports.checkImage = functions.https.onCall(async (data, context) => {
const img_base64 = data;
const request = {
image: {
content: img_base64
}
};
try {
const [result] = await client2.safeSearchDetection(request);
console.log(result.safeSearchAnnotation?.adult);
return result.safeSearchAnnotation?.adult;
} catch (error) {
console.log(error);
return;
}
return;
});
现在我尝试使用 iOS 模拟器通过该调用函数以两种方式发送图像:
我尝试的第一个选项:
1.
exports.checkImage = functions.https.onCall(async (data, context) => {
const img_base64 = data;
const request = {
image: {
content: img_base64
}
};
try {
const [result] = await client2.safeSearchDetection(request);
console.log(result.safeSearchAnnotation?.adult);
return result.safeSearchAnnotation?.adult;
} catch (error) {
console.log(error);
return;
}
return;
});
这里的问题是我在调试控制台上收到此错误:
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: MissingPluginException(No implementation found for method FirebaseFunctions#call on channel plugins.flutter.io/firebase_functions)
#0 convertPlatformException (package:cloud_functions_platform_interface/src/method_channel/utils/exception.dart:16:5)
#1 MethodChannelHttpsCallable.call (package:cloud_functions_platform_interface/src/method_channel/method_channel_https_callable.dart:39:13)
<asynchronous suspension>
#2 HttpsCallable.call (package:cloud_functions/src/https_callable.dart:35:37)
<asynchronous suspension>
#3 DatabaseMethods.clearImage
package:property1/providers/database.dart:294
在某个地方我读到 onCall 云函数可以用作常规端点,所以我这样做了:
clearImage(imageX) async {
final url = Uri.parse(
'https://us-central1-XXXXXXX-XXXX.cloudfunctions.net/checkImage',
);
try {
final response = await http.post(
url,
body: json.encode({
"image": {
"content": imageX
}),
);
}
}
在此情况下,我只收到无法处理的无效请求。
我什至尝试从邮递员处得到相同的结果错误请求。
作为附加数据,我从使用以下代码选择图像的位置调用此函数:
void _pickImage() async {
final imagePicker = ImagePicker();
final pickedImageFile = await imagePicker.getImage(
source: ImageSource.gallery,
imageQuality: 50,
);
setState(() {
if (pickedImageFile != null) {
_pickedImage = File(pickedImageFile.path);
}
});
// This section will be used to check the Images with Google Vision.
List<int> imageBytes = _pickedImage.readAsBytesSync();
String base64Image = base64Encode(imageBytes);
DatabaseMethods().clearImage(base64Image);
widget.imagePickFn(File(pickedImageFile.path));
}
基本上,我将图像转换为发送的 Base64 编码图像,以便 Cloud Vision 可以使用它。
我什至尝试通过注释掉Cloudvision的尝试来检查我是否到达云功能,并添加一个控制台日志来获取正在接收的数据,但它永远不会到达,总是一个无效的请求,无法处理是我的得到。
关于为什么我没有达到该功能的任何想法,或者关于如何从我的 iOS 模拟器测试它的任何想法。
亲切的问候,
要从flutter应用程序调用
onCall
云函数,您必须使用FirebaseFunctions.instance.httpsCallable()
:
import 'package:cloud_functions/cloud_functions.dart';
try {
final result =
await FirebaseFunctions.instance.httpsCallable('checkImage').call();
} on FirebaseFunctionsException catch (error) {
print(error.code);
print(error.details);
print(error.message);
}
首先,安装软件包:
flutter pub add cloud_functions
这是文档。
检查您的cloud_functions包版本。更新到最新的 云函数