二维码扫描器应用


欢迎加入开源鸿蒙跨平台社区:
https://openharmonycrossplatform.csdn.net

适配的第三方库地址:

  • mobile_scanner: https://pub.dev/packages/mobile_scanner
  • url_launcher: https://pub.dev/packages/url_launcher
  • shared_preferences: https://pub.dev/packages/shared_preferences
  • share_plus: https://pub.dev/packages/share_plus

一、项目概述

运行效果图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.1 应用简介

二维码扫描器是一款功能强大的扫码工具应用,支持快速扫描二维码和条形码,记录扫描历史,生成自定义二维码,批量扫描等实用功能。应用以清新的绿色为主色调,象征高效与便捷。涵盖扫描、历史、生成、设置四大模块,让用户轻松处理各类码制信息。

应用采用智能识别技术,自动识别扫描内容的类型(网址、文本、邮箱、电话、WiFi等),并提供相应的快捷操作。无论是日常扫码支付、添加好友,还是批量录入商品信息,都能高效完成。

1.2 核心功能

功能模块功能描述实现方式
快速扫描实时扫描二维码和条形码相机预览
批量扫描连续扫描多个码,统一保存批量模式
历史记录保存扫描历史,支持收藏和搜索本地存储
生成二维码自定义内容生成二维码图片QR编码
智能识别自动识别内容类型,提供快捷操作内容解析
快捷操作复制、打开链接、分享、保存等系统接口
闪光灯控制开关闪光灯,适应不同光线环境相机控制
相册扫码从相册选择图片进行扫码图片解析

1.3 扫码类型定义

序号类型名称图标支持格式典型应用
1二维码🔲QR Code网址、名片、支付
2条形码📊EAN/UPC商品识别、库存
3DataMatrix🔲DataMatrix工业标识、物流
4PDF417📄PDF417证件、票据

1.4 内容类型定义

序号类型名称图标颜色识别规则
1网址🔗蓝色以http/https开头
2文本📝灰色普通文本内容
3邮箱✉️红色包含@符号
4电话📞绿色纯数字或tel:开头
5WiFi📶紫色WIFI:开头
6联系人👤橙色vCard格式
7短信💬青色SMS:开头
8商品🛍️棕色条形码数字

1.5 技术栈

技术领域技术选型版本要求
开发框架Flutter>= 3.0.0
编程语言Dart>= 2.17.0
设计规范Material Design 3-
扫码库mobile_scanner>= 4.0.0
链接跳转url_launcher>= 6.2.0
数据存储shared_preferences>= 2.2.0
分享功能share_plus>= 7.2.0
目标平台鸿蒙OS / Android / iOSAPI 21+

1.6 项目结构

lib/
└── main_qr_scanner.dart
    ├── QRScannerApp               # 应用入口
    ├── ScanType                   # 扫码类型枚举
    ├── ContentType                # 内容类型枚举
    ├── ScanResult                 # 扫描结果模型
    ├── GeneratedQR                # 生成二维码模型
    ├── QRScannerHomePage          # 主页面(底部导航)
    ├── _buildScannerPage          # 扫描页
    ├── _buildHistoryPage          # 历史页
    ├── _buildGeneratePage         # 生成页
    ├── _buildSettingsPage         # 设置页
    └── QRCodePainter              # 二维码绘制器

二、系统架构

2.1 整体架构图

Data Layer

Business Layer

Presentation Layer

主页面
QRScannerHomePage

扫描页

历史页

生成页

设置页

扫描视图

快捷操作

批量模式

收藏列表

历史列表

搜索筛选

类型选择

内容输入

二维码预览

扫描引擎
ScannerEngine

内容解析器
ContentParser

二维码生成器
QRGenerator

历史管理器
HistoryManager

ScanResult
扫描结果

GeneratedQR
生成记录

Settings
设置数据

2.2 类图设计

uses

uses

manages

manages

has

has

QRScannerApp

+Widget build()

«enumeration»

ScanType

+String label

+IconData icon

+qrCode()

+barcode()

+dataMatrix()

+pdf417()

«enumeration»

ContentType

+String label

+IconData icon

+Color color

+url()

+text()

+email()

+phone()

+wifi()

+contact()

+sms()

+product()

ScanResult

+String id

+String content

+ContentType contentType

+ScanType scanType

+DateTime scanTime

+String title

+bool isFavorite

+copyWith()

GeneratedQR

+String id

+String content

+String name

+DateTime createdAt

+int size

QRScannerHomePage

+int currentIndex

+List<ScanResult> scanHistory

+List<GeneratedQR> generatedQRs

+bool isScanning

+bool flashOn

+bool batchMode

+List<ScanResult> batchResults

+void startScanning()

+void stopScanning()

+void generateQR()

+void deleteScanResult()

+void toggleFavorite()

2.3 页面导航流程

扫描

历史

生成

设置

复制

打开

分享

关闭

应用启动

扫描页

底部导航

历史页

生成页

设置页

开始扫描

扫描成功

显示结果弹窗

用户操作

复制到剪贴板

跳转链接

调用分享

批量模式

连续扫描

完成扫描

保存批量结果

查看历史

点击记录

收藏/删除

选择类型

输入内容

生成二维码

保存/分享

2.4 扫描处理流程

历史管理器内容解析器扫描引擎扫描页用户历史管理器内容解析器扫描引擎扫描页用户点击扫描按钮启动相机预览返回预览画面对准二维码捕获图像帧解码识别返回扫描结果解析内容类型返回类型信息保存扫描记录保存成功显示结果弹窗选择操作执行对应操作

三、核心模块设计

3.1 数据模型设计

3.1.1 扫码类型枚举 (ScanType)
enum ScanType {
  qrCode(label: '二维码', icon: Icons.qr_code_2),
  barcode(label: '条形码', icon: Icons.barcode_reader),
  dataMatrix(label: 'DataMatrix', icon: Icons.grid_on),
  pdf417(label: 'PDF417', icon: Icons.picture_as_pdf);

  final String label;
  final IconData icon;
  const ScanType({required this.label, required this.icon});
}
3.1.2 内容类型枚举 (ContentType)
enum ContentType {
  url(label: '网址', icon: Icons.link, color: Colors.blue),
  text(label: '文本', icon: Icons.text_fields, color: Colors.grey),
  email(label: '邮箱', icon: Icons.email, color: Colors.red),
  phone(label: '电话', icon: Icons.phone, color: Colors.green),
  wifi(label: 'WiFi', icon: Icons.wifi, color: Colors.purple),
  contact(label: '联系人', icon: Icons.person, color: Colors.orange),
  sms(label: '短信', icon: Icons.sms, color: Colors.teal),
  product(label: '商品', icon: Icons.shopping_bag, color: Colors.brown);

  final String label;
  final IconData icon;
  final Color color;
  const ContentType({required this.label, required this.icon, required this.color});
}
3.1.3 扫描结果模型 (ScanResult)
class ScanResult {
  final String id;
  final String content;
  final ContentType contentType;
  final ScanType scanType;
  final DateTime scanTime;
  final String? title;
  final bool isFavorite;

  const ScanResult({
    required this.id,
    required this.content,
    required this.contentType,
    required this.scanType,
    required this.scanTime,
    this.title,
    this.isFavorite = false,
  });

  ScanResult copyWith({
    String? id,
    String? content,
    ContentType? contentType,
    ScanType? scanType,
    DateTime? scanTime,
    String? title,
    bool? isFavorite,
  }) {
    return ScanResult(
      id: id ?? this.id,
      content: content ?? this.content,
      contentType: contentType ?? this.contentType,
      scanType: scanType ?? this.scanType,
      scanTime: scanTime ?? this.scanTime,
      title: title ?? this.title,
      isFavorite: isFavorite ?? this.isFavorite,
    );
  }
}
3.1.4 生成二维码模型 (GeneratedQR)
class GeneratedQR {
  final String id;
  final String content;
  final String name;
  final DateTime createdAt;
  final int size;

  const GeneratedQR({
    required this.id,
    required this.content,
    required this.name,
    required this.createdAt,
    this.size = 200,
  });
}
3.1.5 内容类型分布
35%25%15%10%8%5%2%扫描内容类型分布示例网址商品文本WiFi邮箱电话其他

3.2 页面结构设计

3.2.1 主页面布局

QRScannerHomePage

IndexedStack

扫描页

历史页

生成页

设置页

NavigationBar

扫描 Tab

历史 Tab

生成 Tab

设置 Tab

3.2.2 扫描页结构

扫描页

SliverAppBar

扫描视图

快捷操作

批量指示器

相机预览

扫描框

扫描线动画

状态提示

扫描按钮

相册选择

闪光灯开关

已扫描数量

完成按钮

3.2.3 历史页结构

历史页

SliverAppBar

收藏分组

其他分组

清空按钮

收藏记录列表

记录卡片

类型图标

内容预览

时间显示

操作按钮

其他记录列表

3.2.4 生成页结构

生成页

类型选择网格

已生成列表

网址

文本

WiFi

联系人

邮箱

电话

二维码卡片

二维码预览

名称显示

操作菜单

3.3 内容识别逻辑

渲染错误: Mermaid 渲染失败: Parse error on line 5: ...D[识别为WiFi] B -->|mailto:或含@| E[识别为邮箱 ----------------------^ Expecting 'AMP', 'COLON', 'PIPE', 'TESTSTR', 'DOWN', 'DEFAULT', 'NUM', 'COMMA', 'NODE_STRING', 'BRKT', 'MINUS', 'MULT', 'UNICODE_TEXT', got 'LINK_ID'

3.4 批量扫描逻辑

开启批量模式

开始扫描

扫描成功

添加到批量列表

继续扫描?

点击完成

保存批量结果

显示汇总信息

退出批量模式


四、UI设计规范

4.1 配色方案

应用以清新的绿色为主色调,象征高效与便捷:

颜色类型色值用途
主色#4CAF50 (Green)导航、主题元素、按钮
主色容器#E8F5E9卡片背景、扫描框
辅助色#388E3C强调元素
第三色#81C784次要元素
背景色#FAFAFA页面背景
卡片背景#FFFFFF信息卡片
成功色#4CAF50扫描成功
警告色#FF9800处理中
错误色#F44336扫描失败

4.2 内容类型配色

内容类型色值视觉效果
网址#2196F3蓝色
文本#9E9E9E灰色
邮箱#F44336红色
电话#4CAF50绿色
WiFi#9C27B0紫色
联系人#FF9800橙色
短信#009688青色
商品#795548棕色

4.3 字体规范

元素字号字重颜色
页面标题20pxBold主色
扫描框提示12pxMedium#FFFFFF
内容标题16pxMedium#000000
内容预览12pxRegular#666666
时间标签10pxRegular#999999
按钮文字14pxMedium主色

4.4 组件规范

4.4.1 扫描视图布局
┌─────────────────────────────────────┐
│  [← 返回]    二维码扫描器    [批量] [💡] │
├─────────────────────────────────────┤
│                                     │
│     ┌─────────────────────────┐    │
│     │                         │    │
│     │    ┌───────────────┐   │    │
│     │    │               │   │    │
│     │    │   ─────────   │   │    │
│     │    │               │   │    │
│     │    │     🔲        │   │    │
│     │    │               │   │    │
│     │    └───────────────┘   │    │
│     │                         │    │
│     │   将二维码/条形码放入框内  │    │
│     └─────────────────────────┘    │
│                                     │
│      [📷 扫描]  [🖼️ 相册]  [💡 闪光灯] │
└─────────────────────────────────────┘
4.4.2 扫描结果弹窗
┌─────────────────────────────────────┐
│  🔗 网址                             │
│                                     │
│  扫描结果:                          │
│  ┌─────────────────────────────┐   │
│  │ https://www.example.com     │   │
│  └─────────────────────────────┘   │
│                                     │
│  扫描时间:2024-01-15 14:30         │
│                                     │
│  [📋 复制]  [🌐 打开]  [📤 分享]     │
└─────────────────────────────────────┘
4.4.3 历史记录卡片
┌─────────────────────────────────────┐
│  ┌────┐                            │
│  │ 🔗 │  https://www.example.com   │
│  │    │  2024-01-15 14:30     ⭐ 🗑️ │
│  └────┘                            │
└─────────────────────────────────────┘
4.4.4 生成类型选择
┌─────────────────────────────────────┐
│  快速创建                            │
│                                     │
│  ┌─────────┐ ┌─────────┐ ┌─────────┐
│  │   🔗    │ │   📝    │ │   📶    │
│  │  网址   │ │  文本   │ │  WiFi  │
│  └─────────┘ └─────────┘ └─────────┘
│                                     │
│  ┌─────────┐ ┌─────────┐ ┌─────────┐
│  │   👤    │ │   ✉️    │ │   📞    │
│  │ 联系人  │ │  邮箱   │ │  电话   │
│  └─────────┘ └─────────┘ └─────────┘
└─────────────────────────────────────┘
4.4.5 批量扫描指示器
┌─────────────────────────────────────┐
│  ☑️ 批量扫描模式        已扫描 5 个   │
│                          [完成]     │
└─────────────────────────────────────┘

五、核心功能实现

5.1 扫描功能实现

void _startScanning() {
  setState(() {
    _isScanning = true;
  });
  _scanAnimationController.repeat();

  Future.delayed(const Duration(seconds: 2), () {
    if (_isScanning) {
      _simulateScan();
    }
  });
}

void _stopScanning() {
  setState(() {
    _isScanning = false;
  });
  _scanAnimationController.stop();
}

5.2 内容识别实现

ContentType _identifyContentType(String content) {
  if (content.startsWith('http://') || content.startsWith('https://')) {
    return ContentType.url;
  } else if (content.startsWith('WIFI:')) {
    return ContentType.wifi;
  } else if (content.contains('@')) {
    return ContentType.email;
  } else if (content.startsWith('tel:') || RegExp(r'^\d+$').hasMatch(content)) {
    return ContentType.phone;
  } else if (content.startsWith('SMS:')) {
    return ContentType.sms;
  } else if (content.startsWith('BEGIN:VCARD')) {
    return ContentType.contact;
  } else {
    return ContentType.text;
  }
}

5.3 批量扫描实现

void _toggleBatchMode() {
  setState(() {
    _batchMode = !_batchMode;
    if (!_batchMode && _batchResults.isNotEmpty) {
      _scanHistory.insertAll(0, _batchResults);
      _showBatchResultsSummary();
      _batchResults = [];
    }
  });
}

void _simulateScan() {
  final result = ScanResult(
    id: 'scan_${DateTime.now().millisecondsSinceEpoch}',
    content: scannedContent,
    contentType: _identifyContentType(scannedContent),
    scanType: ScanType.qrCode,
    scanTime: DateTime.now(),
  );

  setState(() {
    if (_batchMode) {
      _batchResults.add(result);
      // 继续扫描
      if (_isScanning) {
        Future.delayed(const Duration(seconds: 1), () {
          if (_isScanning) _simulateScan();
        });
      }
    } else {
      _scanHistory.insert(0, result);
      _isScanning = false;
      _scanAnimationController.stop();
    }
  });
}

5.4 二维码生成实现

void _generateQR() {
  if (_generateController.text.isEmpty) return;

  final qr = GeneratedQR(
    id: 'gen_${DateTime.now().millisecondsSinceEpoch}',
    content: _generateController.text,
    name: _nameController.text.isEmpty ? '未命名' : _nameController.text,
    createdAt: DateTime.now(),
  );

  setState(() {
    _generatedQRs.insert(0, qr);
  });
}

5.5 收藏功能实现

void _toggleFavorite(ScanResult result) {
  setState(() {
    final index = _scanHistory.indexOf(result);
    _scanHistory[index] = result.copyWith(isFavorite: !result.isFavorite);
  });
}

六、交互设计

6.1 扫描流程

结果弹窗扫描引擎扫描页用户结果弹窗扫描引擎扫描页用户点击扫描按钮启动相机返回预览画面显示扫描框对准二维码识别解码返回结果保存历史显示结果展示内容和操作选择操作执行操作反馈结果

6.2 批量扫描流程

开启批量模式

开始扫描

扫描成功

添加到批量列表

显示提示

继续扫描?

点击完成

保存所有结果

显示汇总

退出批量模式

6.3 生成二维码流程

不满意

选择类型

输入内容

预览二维码

确认生成

保存记录

修改内容


七、扩展功能规划

7.1 后续版本规划

2024-01-072024-01-142024-01-212024-01-282024-02-042024-02-112024-02-182024-02-252024-03-032024-03-102024-03-172024-03-242024-03-312024-04-07基础UI框架 扫描功能实现 历史记录功能 生成二维码功能 批量扫描优化 更多码制支持 云端同步功能 OCR文字识别 身份证识别 银行卡识别 V1.0 基础版本V1.1 增强版本V1.2 进阶版本二维码扫描器开发计划

7.2 功能扩展建议

7.2.1 高级识别功能

识别功能:

  • 身份证信息识别
  • 银行卡号识别
  • 车牌号码识别
  • 文档OCR识别
7.2.2 数据管理功能

管理功能:

  • 云端数据同步
  • 多设备数据共享
  • 数据导出备份
  • 分类标签管理
7.2.3 安全隐私功能

安全功能:

  • 敏感内容加密存储
  • 隐私模式保护
  • 访问密码设置
  • 自动清理机制

八、注意事项

8.1 开发注意事项

  1. 相机权限:确保正确申请和处理相机权限

  2. 隐私保护:敏感扫描内容需加密存储

  3. 性能优化:相机预览需注意内存和电量消耗

  4. 兼容性:不同设备相机性能差异需做适配

  5. 用户体验:扫描过程需提供清晰的视觉反馈

8.2 常见问题

问题原因解决方案
无法打开相机权限未授予引导用户开启权限
扫描识别慢光线不足/对焦问题提示调整光线和距离
识别错误码制不支持扩展支持的码制
历史丢失数据未持久化集成本地存储
闪光灯无效设备不支持检测设备能力

8.3 使用技巧

📱 二维码扫描器使用技巧 📱

扫描技巧

  • 保持手机稳定,避免晃动
  • 确保二维码清晰完整
  • 适当调整光线和距离
  • 使用闪光灯辅助暗光环境

批量扫描

  • 开启批量模式连续扫描
  • 扫描完成后统一保存
  • 适合商品录入场景

生成二维码

  • 选择合适的内容类型
  • 内容不宜过长
  • 可自定义名称便于管理

九、运行说明

9.1 环境要求

环境版本要求
Flutter SDK>= 3.0.0
Dart SDK>= 2.17.0
鸿蒙OSAPI 21+
AndroidAPI 21+
iOS12.0+
相机权限必需

9.2 运行命令

# 查看可用设备
flutter devices

# 运行到Web服务器
flutter run -d web-server -t lib/main_qr_scanner.dart --web-port 8147

# 运行到鸿蒙设备
flutter run -d 127.0.0.1:5555 lib/main_qr_scanner.dart

# 运行到Android设备
flutter run -d android lib/main_qr_scanner.dart

# 代码分析
flutter analyze lib/main_qr_scanner.dart

9.3 依赖配置

pubspec.yaml 中添加以下依赖(完整版需要):

dependencies:
  flutter:
    sdk: flutter
  mobile_scanner: ^4.0.1
  url_launcher: ^6.2.2
  shared_preferences: ^2.2.2
  share_plus: ^7.2.1
  permission_handler: ^11.3.1
  image_picker: ^1.0.4

十、总结

二维码扫描器应用通过智能识别技术、便捷的操作方式、完善的历史管理,让用户轻松处理各类码制信息。应用支持4种扫码类型、8种内容类型识别,满足日常扫码的各种需求。

核心功能涵盖快速扫描、批量扫描、历史记录、生成二维码、智能识别、快捷操作六大模块。用户可以快速扫描各类码制,自动识别内容类型,一键执行复制、打开、分享等操作,大幅提升工作效率。

应用采用 Material Design 3 设计规范,以清新的绿色为主色调,象征高效与便捷。通过本应用,希望能够为用户提供便捷高效的扫码体验,让信息获取更加简单。

二维码扫描器——扫一扫,世界尽在掌握


Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐