最近为了使用扫码和识别,接入了google_ml_kit,记录下踩过的。
功能
Vision
Feature | Android | iOS |
---|---|---|
Text Recognition | ✅ | ✅ |
Face Detection | ✅ | ✅ |
Pose Detection | ✅ | ✅ |
Selfie Segmentation | yet | yet |
Barcode Scanning | ✅ | ✅ |
Image Labelling | ✅ | ✅ |
Object Detection and Tracking | ✅ | yet |
Digital Ink Recognition | ✅ | ✅ |
Text Detector V2 | ✅ | yet |
Text Recognition V2 | ✅ | yet |
Natural Language
Feature | Android | iOS |
---|---|---|
Language Identification | ✅ | ✅ |
On-Device Translation | ✅ | yet |
Smart Reply | ✅ | yet |
Entity Extraction | ✅ | yet |
环境
iOS
- Minimum iOS Deployment Target: 10.0
- Xcode 12 or newer
- Swift 5
- ML Kit only supports 64-bit architectures (x86_64 and arm64). Check this list to see if your device has the required device capabilities.
接入
- 排除 armv7,不是必须步骤:
Xcode > Runner > Building Settings > Excluded Architectures > Any SDK > armv7
- 更改 IPHONEOS_DEPLOYMENT_TARGET,不是必须步骤:
-
Firebase 创建项目:
官方教程不太准确,可以参考到下载 GoogleService-Info.plist
在Firebase创建项目,添加 ios 应用。
在软件包 ID 字段中输入应用的软件包 ID。
点击注册应用。
点击下载 GoogleService-Info.plist,获取 Firebase Apple 平台配置文件 (
GoogleService-Info.plist
)。拷贝GoogleService-Info.plist到 ios/Runner/目录下。右键 Runner,点击 Add Files to "Runner",选择GoogleService-Info.plist。
-
导包,初始化:
截屏2021-12-22 下午1.44.50
import UIKit
import Flutter
//添加这行
import FirebaseCore
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
//添加这行
FirebaseApp.configure()
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
如果找不到FirebaseCore
,在Podfile添加,这种情况是不直接在项目里使用google_ml_kit,而是封装为插件引入项目里:
target 'Runner' do
use_frameworks!
use_modular_headers!
# 添加这句话
pod 'Firebase'
-
运行
pod install
:
如果 CocoaPods 版本过低,请升级 CocoaPods。pod install
如果报错执行以下操作
退出Xcode.
删除 ~/Library/Developer/Xcode/DerivedData
删除 ProjectName.xcworkspace
删除 Podfile.lock和 Pods 文件夹
pod install
大功告成
Android
环境
minSdkVersion: 21
targetSdkVersion: 29
接入
在 Firebase添加 Android 应用
输入包名注册应用
下载google-services.json
拷贝google-services.json到项目 android/app/目录下
-
在
AndroidManifest.xml
里添加meta-data
:-
ica -
Image Labeling
-
ocr -
Barcode Scanning
-
face -
Face Detection
<!-- 添加这个,value 填入你用到的功能--> <meta-data android:name="com.google.mlkit.vision.DEPENDENCIES" android:value="ica,ocr,face" /> <meta-data android:name="flutterEmbedding" android:value="2" /> </application>
-
确保项目级 build.gradle(
android/build.gradle
)里包google
仓库:buildscript { repositories { // 有这个 google() // Google's Maven repository*content_copy* } } allprojects { ... repositories { // 有这个: google() // Google's Maven repository*content_copy* ... } }
-
ica -
完事