在 UniApp 中使用拍照进行 AR 识别

1,422 阅读4分钟

简介

本文档详细介绍了如何在 UniApp 中使用相机拍照进行 AR(增强现实)识别的实现方法。该解决方案利用 UniApp 内置的相机功能来捕获图像,对其进行处理,并通过后端 API 进行 AR 识别。本指南侧重于创建一个无缝的用户体验,包括实时图像捕获和 AR 处理。

功能概述

  1. 相机集成:利用 UniApp 的原生相机组件进行图像捕获。
  2. AR 识别:处理捕获的图像,并将其发送至 API 进行 AR 识别。
  3. 实时反馈:根据识别结果提供即时的视觉反馈和更新。
  4. 交互式界面:提供友好的用户界面,包括重新拍照和切换摄像头的选项。

实现细节

1. 相机集成

使用 UniApp 的 camera 组件进行相机集成。重要的是要优雅地处理相机错误,并为用户提供翻转相机视图(前/后置摄像头)和拍照的选项。

  • device-position:控制前置或后置摄像头。

  • binderror 和 bindstop:处理相机错误和停止事件。

2. 拍照功能

捕获的图像需要进行处理,以确保其符合 AR 识别的要求。然后将图像转换为 base64 字符串,以便在 API 请求中更容易处理。

takePhoto() { uni.chooseImage({ count: 1, sizeType: ['original', 'compressed'], sourceType: ['camera'], success: (res) => { this.snapshotsrc = res.tempFilePaths[0]; this.snapshotTaken = true; this.isAnimation = true; this.closeCamera(); this.convertImageToBase64(res.tempFilePaths[0]).then(base64 => { this.detectImageSubjectChatgpt(base64); }); }, fail: (err) => console.error('Failed to choose image:', err) }); },

  • uni.chooseImage:通过相机捕获图像。

  • convertImageToBase64:将图像文件转换为 base64 字符串。

  • 3. AR 识别

AR 识别通过将 base64 编码的图像发送到后端 API 来实现。API 处理图像并返回识别结果,然后将结果显示给用户。 async detectImageSubjectChatgpt(base64Image) { if (!this.isComponentAlive) return; try { const requestData = { imageStr: base64Image, isGpt: true }; const response = await chatFreeImageOCRAPI(requestData); this.handleChatgptResponse(response); } catch (error) { this.handleChatgptError(); } },

  • detectImageSubjectChatgpt:将图像数据发送到后端 API 并处理响应。

  • chatFreeImageOCRAPI:用于 AR 识别的 API。

4. 实时反馈

为了增强用户体验,应用程序在 AR 识别过程中显示动画或消息。这有助于在图像处理期间保持用户的参与度。

{{$t('home.recognizing_in_progress')}} **`isAnimation`**:一个布尔值,用于在识别过程中触发动画显示。
5. 用户界面与交互

界面设计直观,提供清晰的指示和反馈。这包括显示拍摄的图像,允许用户重新拍照,并显示识别结果。

<view class="takePhoto" v-if="!snapshotTaken" @tap="takePhoto"> {{$t('index.ai_health_takePhoto')}}

  • snapshot-container:显示拍摄的图像。

  • takePhoto:一个触发拍照过程的按钮。

技术文档:在 UniApp 中使用拍照进行 AR 识别


简介

本文档详细介绍了如何在 UniApp 中使用相机拍照进行 AR(增强现实)识别的实现方法。该解决方案利用 UniApp 内置的相机功能来捕获图像,对其进行处理,并通过后端 API 进行 AR 识别。本指南侧重于创建一个无缝的用户体验,包括实时图像捕获和 AR 处理。


功能概述

  1. 相机集成:利用 UniApp 的原生相机组件进行图像捕获。
  2. AR 识别:处理捕获的图像,并将其发送至 API 进行 AR 识别。
  3. 实时反馈:根据识别结果提供即时的视觉反馈和更新。
  4. 交互式界面:提供友好的用户界面,包括重新拍照和切换摄像头的选项。

实现细节

1. 相机集成

使用 UniApp 的 camera 组件进行相机集成。重要的是要优雅地处理相机错误,并为用户提供翻转相机视图(前/后置摄像头)和拍照的选项。

vue
复制代码
<camera class="livePusher" :hidden="!showCamera" id="myCamera" :device-position="devicePosition" flash="off"
    binderror="cameraError" bindstop="cameraStop"></camera>
  • device-position:控制前置或后置摄像头。
  • binderror 和 bindstop:处理相机错误和停止事件。
2. 拍照功能

捕获的图像需要进行处理,以确保其符合 AR 识别的要求。然后将图像转换为 base64 字符串,以便在 API 请求中更容易处理。

javascript
复制代码
takePhoto() {
    uni.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['camera'],
        success: (res) => {
            this.snapshotsrc = res.tempFilePaths[0];
            this.snapshotTaken = true;
            this.isAnimation = true;
            this.closeCamera();
            this.convertImageToBase64(res.tempFilePaths[0]).then(base64 => {
                this.detectImageSubjectChatgpt(base64);
            });
        },
        fail: (err) => console.error('Failed to choose image:', err)
    });
},
  • uni.chooseImage:通过相机捕获图像。
  • convertImageToBase64:将图像文件转换为 base64 字符串。
3. AR 识别

AR 识别通过将 base64 编码的图像发送到后端 API 来实现。API 处理图像并返回识别结果,然后将结果显示给用户。

javascript
复制代码
async detectImageSubjectChatgpt(base64Image) {
    if (!this.isComponentAlive) return;
    try {
        const requestData = {
            imageStr: base64Image,
            isGpt: true
        };
        const response = await chatFreeImageOCRAPI(requestData);
        this.handleChatgptResponse(response);
    } catch (error) {
        this.handleChatgptError();
    }
},
  • detectImageSubjectChatgpt:将图像数据发送到后端 API 并处理响应。
  • chatFreeImageOCRAPI:用于 AR 识别的 API。
4. 实时反馈

为了增强用户体验,应用程序在 AR 识别过程中显示动画或消息。这有助于在图像处理期间保持用户的参与度。

vue
复制代码
<view v-if="isAnimation" class="animationView">
    <image class="animation-image" :src="getImageSrc('animation_1.gif')" lazy-load="true"></image>
    <text class="title">{{$t('home.recognizing_in_progress')}}</text>
</view>
  • isAnimation:一个布尔值,用于在识别过程中触发动画显示。
5. 用户界面与交互

界面设计直观,提供清晰的指示和反馈。这包括显示拍摄的图像,允许用户重新拍照,并显示识别结果。

vue
复制代码
<view class="snapshot-container" v-if="snapshotTaken">
    <image class="snapshot-image" :src="snapshotsrc" mode="aspectFit"></image>
    <view class="snapshot-bg"></view>
</view>

<view class="takePhoto" v-if="!snapshotTaken" @tap="takePhoto">
    <text class="takePhoto-text">{{$t('index.ai_health_takePhoto')}}</text>
</view>
  • snapshot-container:显示拍摄的图像。
  • takePhoto:一个触发拍照过程的按钮。

结论

本指南介绍了在 UniApp 中使用相机拍照进行 AR 识别的实现方法。该解决方案集成了多个 UniApp 组件和 API 调用,为用户提供了强大的交互体验。通过遵循本文提供的步骤和代码示例,开发人员可以在他们的应用程序中实现类似的功能,从而增强应用程序的交互性和功能性。