Flutter項目google_ml_kit接入

最近爲了使用掃碼和識別,接入了google_ml_kit,記錄下踩過的。

version:0.7.3

功能

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.

接入

  1. 排除 armv7,不是必須步驟:

Xcode > Runner > Building Settings > Excluded Architectures > Any SDK > armv7

  1. 更改 IPHONEOS_DEPLOYMENT_TARGET,不是必須步驟
  1. 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

  1. 導包,初始化:


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'
  1. 運行pod install:
    如果 CocoaPods 版本過低,請升級 CocoaPods。

    pod install
    

如果報錯執行以下操作

  1. 退出Xcode.

  2. 刪除 ~/Library/Developer/Xcode/DerivedData

  3. 刪除 ProjectName.xcworkspace

  4. 刪除 Podfile.lock和 Pods 文件夾

  5. pod install

大功告成

Android

環境

  • minSdkVersion: 21

  • targetSdkVersion: 29

接入

  1. Firebase添加 Android 應用

  2. 輸入包名註冊應用

  3. 下載google-services.json

  4. 拷貝google-services.json到項目 android/app/目錄下

  5. 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>
    
    1. 確保項目級 build.gradle(android/build.gradle)裏包 google倉庫:

      buildscript {
       repositories {
        // 有這個
        google()  // Google's Maven repository*content_copy*
       }
      }
      
      allprojects {
       ...
       repositories {
        // 有這個:
        google()  // Google's Maven repository*content_copy*
        ...
       }
      }
      

完事

插件源碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章