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

build_settings_01
  1. 更改 IPHONEOS_DEPLOYMENT_TARGET,不是必须步骤:
image-20211222134107492
image-20211222134252278
image-20211222134337633
  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。

截屏2021-12-22 下午1.58.43
  1. 导包,初始化:


    截屏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'
  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*
        ...
       }
      }
      

完事

插件源码

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容