最近爲了使用掃碼和識別,接入了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。
-
導包,初始化:
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 -
完事