Flutter文本识别插件huawei_ml_text的使用

发布于 1周前 作者 yibo5220 来自 Flutter

Huawei ML 文本 Flutter 插件

安装

请参阅 pub.devAppGallery Connect 配置

文档

问题或反馈

如果您在使用 HMS 示例时遇到任何问题,可以尝试以下选项:

如果在我们的示例中发现错误,请提交一个 issue 到 GitHub 仓库

许可证

Huawei ML 文本 Flutter 插件 使用 Apache 2.0 许可证


示例代码

以下是使用 huawei_ml_text 插件的基本示例代码:

/*
    Copyright 2021-2024. Huawei Technologies Co., Ltd. All rights reserved.

    Licensed under the Apache License, Version 2.0 (the "License")
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        https://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:huawei_ml_text/huawei_ml_text.dart';
import 'package:huawei_ml_text_example/screens/home.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations(
    <DeviceOrientation>[
      DeviceOrientation.portraitUp,
    ],
  );
  // TODO: 将您 agconnect-services.json 文件中的 api_key 值复制粘贴到这里。
  await MLTextApplication().setApiKey('<api_key>');
  await requestPermissions();
  runApp(const MyApp());
}

// TODO: 请实现您自己的 '权限处理程序'。
Future<void> requestPermissions() async {
  // 此插件需要一些权限才能正常工作。
  // 您需要处理这些权限以使用此示例。

  // 您可以从我们的官方文档中了解更多所需的权限。
  // https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Guides/assigning-permissions-0000001052789343?ha_source=hms1
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        appBarTheme: const AppBarTheme(
          elevation: 0,
          centerTitle: false,
          foregroundColor: Colors.black,
          backgroundColor: Colors.white,
        ),
      ),
      home: const Home(),
    );
  }
}

更多关于Flutter文本识别插件huawei_ml_text的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter文本识别插件huawei_ml_text的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


要在Flutter应用中使用华为的文本识别插件 huawei_ml_text,首先需要确保你已经按照华为的文档进行了华为HMS(Huawei Mobile Services)的集成和配置。以下是使用 huawei_ml_text 插件进行文本识别的步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 huawei_ml_text 依赖:

dependencies:
  flutter:
    sdk: flutter
  huawei_ml_text: ^2.0.3+300

然后运行 flutter pub get 来获取依赖。

2. 初始化插件

在使用插件之前,需要在 main.dart 文件中初始化插件:

import 'package:huawei_ml_text/huawei_ml_text.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await MLText().initialize(); // 初始化文本识别插件
  runApp(MyApp());
}

3. 配置权限

AndroidManifest.xml 中添加必要的权限:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />

4. 使用文本识别功能

你可以使用 MLText 类提供的 API 来进行文本识别。以下是一个简单的示例:

import 'package:flutter/material.dart';
import 'package:huawei_ml_text/huawei_ml_text.dart';
import 'package:image_picker/image_picker.dart';

class TextRecognitionPage extends StatefulWidget {
  [@override](/user/override)
  _TextRecognitionPageState createState() => _TextRecognitionPageState();
}

class _TextRecognitionPageState extends State<TextRecognitionPage> {
  String _recognizedText = '';

  Future<void> _recognizeTextFromImage() async {
    final pickedFile = await ImagePicker().getImage(source: ImageSource.camera);

    if (pickedFile != null) {
      final result = await MLText().recognizeText(pickedFile.path);

      setState(() {
        _recognizedText = result.text;
      });
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Text Recognition'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(_recognizedText),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: _recognizeTextFromImage,
              child: Text('Capture and Recognize Text'),
            ),
          ],
        ),
      ),
    );
  }
}
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!