AndroidFlutter

Flutter项目google_ml_kit接入

2021-12-22  本文已影响0人  A_si

最近为了使用扫码和识别,接入了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

接入

  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

截屏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

环境

接入

  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*
        ...
       }
      }
      

完事

插件源码

上一篇 下一篇

猜你喜欢

热点阅读