【uniapp】实现同声传译(长按语音转文字)、【node】识别图片中的文字并提取

uniapp

效果图:

 插件:

采用小程序插件:微信同声传译。插件文档定位

具体步骤:

  • 先登录小程序后台(项目别错了):官网传送
  • 然后 设置 => 第三方设置 => 添加插件 

  • 在插件文档里面拿到Appid和版本号

  • 在manifest.json切换成源码视图 然后在appid同级目录添加插件

  •  然后就是引用插件,开始使用了

完整代码:

<template>
	<view>
		<view class="voicepad">
			{{voiceState}}
		</view>
		<button class="cu-btn  bg-green voicebtn " @touchstart="touchStart" @touchend="touchEnd">
			<image src="../../static/logo.png" mode="widthFix" style="width: 50rpx;"></image>
		</button>
		<view class="center" style="background-color: #555555; color: #FFF;" v-show="isShow">
			正在录音...
		</view>
	</view>
</template>

<script>
	var plugin = requirePlugin("WechatSI")
	let manager = plugin.getRecordRecognitionManager();

	export default {
		data() {
			return {
				voiceState: "你可以这样说...",
				isShow: false
			}
		},
		onShow() {

		},
		onLoad() {
			this.initRecord();
		},
		methods: {
			touchStart() {
				this.isShow = true
				manager.start({
                    //指定录音的时长,单位ms,最大为60000
					duration: 60000,
                    //识别的语言,目前支持zh_CN en_US zh_HK
					lang: "zh_CN"
				});
			},
			touchEnd() {
				uni.showToast({
					title: '录音完成',
					icon: "none"
				})
				this.isShow = false
				manager.stop();
			},
			/**  
			 * 初始化语音识别回调  
			 * 绑定语音播放开始事件  
			 */
			initRecord() {
				manager.onStart = (res) => {
					console.log('start', res.msg);
					this.voiceState = res.msg;
				};
				//有新的识别内容返回,则会调用此事件  
				manager.onRecognize = (res) => {
					this.voiceState = res.result;
					console.log('onRecognize');
				}

				// 识别结束事件  
				manager.onStop = (res) => {
					this.voiceState = res.result;
					console.log('onStop', res.result);
				}

				// 识别错误事件  
				manager.onError = (res) => {
					this.voiceState = res.msg;
					console.log('onError');

				}
			},
		}
	}
</script>

<style>
	.voicebtn {
		height: 130upx;
		display: block;
		width: 130upx;
		line-height: 130upx;
		border-radius: 65upx;
		font-size: 50upx;
		position: absolute;
		top: 1060upx;
		left: 310upx;
	}

	.voicepad {
		height: 250upx;
		width: 680upx;
		background: #fff;
		margin: 30upx auto;
		border-radius: 8upx;

		padding: 20upx;
		font-size: 35upx;
	}

	.center {
		text-align: center;
		align-items: center;
		width: 200rpx;
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		padding: 20rpx;
		border-radius: 20rpx;
		/* 	height: 50rpx; */
		opacity: 0.8;
	}
</style>

注解:

@touchstart="touchStart"   手指触摸动作开始触发

@touchend="touchEnd"     手指触摸动作结束触发

问题:
有的朋友启用真机调试可以会报:error occurs:no such file or directory, access 'wxfile://usr/miniprogramLog/log2'

将2.0转为1.0就行了,发布后可正常 不会出现问题

node

效果图:

一、对接百度智能云

登录百度智能云:百度智能云-云智一体深入产业

新用户可以免费体验,按照下面来就行:

 创建应用之后就会有密钥啥的了

二、在node项目中安装依赖并使用 

所有依赖如下:

    "baidu-aip-sdk": "^4.16.12",
    "express": "^4.18.2",
    "multer": "^1.4.5-lts.1",
    "nodemon": "^2.0.22"

不知道安装命令可到此处查找:npm

index.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>图片上传</title>
</head>

<body>
    <form id="uploadForm">
        <input type="file" id="fileInput">
        <button type="submit">上传图片</button>
    </form>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function () {
            $('#uploadForm').submit(function (event) {
                event.preventDefault(); // 阻止表单的默认提交行为

                const formData = new FormData();
                formData.append('image', $('#fileInput')[0].files[0]);

                $.ajax({
                    url: 'http://127.0.0.1:3000/upload',
                    type: 'POST',
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function (result) {
                        console.log(result);
                    },
                    error: function (error) {
                        console.error(error);
                    }
                });
            });
        });
    </script>
</body>

</html>

app.js,需要在app.js的同级目录下创建static存放图片

var AipOcrClient = require("baidu-aip-sdk").ocr;
const express = require('express')
const multer = require('multer')
const fs = require('fs');
const path = require('path')
const app = express()

// 设置APPID/AK/SK
var APP_ID = "xxxxxxx";
var API_KEY = "xxxxxxxxxxxxxxxxxxxx";
var SECRET_KEY = "xxxxxxxxxxxxxxxxxxxxx";
// 新建一个对象,建议只保存一个对象调用服务接口
var client = new AipOcrClient(APP_ID, API_KEY, SECRET_KEY);

// diskStorage创建上传存储器 
const storage = multer.diskStorage({
    // 设置上传文件存储目录
    destination: function (req, file, cb) {
        cb(null, './static/')
    },
    //保存在 uploads 中的文件名
    filename: function (req, file, cb) {
        const extname = path.extname(file.originalname) // 获取文件后缀名
        const filename = Date.now() + '' + extname     // 时间戳+后缀名 生成唯一文件名
        cb(null, filename)
    }
})

//创建一个名为upload的文件上传示例
const upload = multer({ storage: storage })

// 创建上传路由
// upload.single('image') 处理单个文件上传
app.post('/upload', upload.single('image'), (req, res) => {
    const file = req.file
    if (!file) {
        return res.status(400).send('请选择要上传的图片')
    }
    const filePath = req.file.path;
    // 读取文件
    fs.readFile(filePath, (err, data) => {
        if (err) {
            console.error(err);
            res.status(500).send('Failed to read the file.');
            return;
        }
        // 将文件数据转换为 Base64
        const base64Data = data.toString('base64');
        client.generalBasic(base64Data).then(function(result) {
            res.send(result)
            console.log(result);
        }).catch(function(err) {
            console.log(err);
        });
    });
})


const PORT = process.env.PORT || 3000;
// 启动服务器
app.listen(3000, () => {
    console.log(`Server running on http://localhost:${PORT}`)
})

注解:

百度智能云更详细的可看文档:接口说明 - 文字识别OCR

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
在使用uni-app开发微信小程序时,与直接使用微信网页开发工具开发微信小程序有一些差别。由于uni-app可以开发多平台,因此不同平台的开发需要在指定的位置进行相应的配置才能生效。对于引入微信同声传译小程序,有两种方式可以实现。一种是整个小程序可使用,即小程序的所有分包都可以使用该功能。另一种是指定对应的分包可使用该功能。在使用插件之前,需要在manifest.json文件的mp-weixin内声明使用的插件,并进行相应的配置。具体的配置可以参考所使用插件的开发文档。在代码,可以直接使用文档提供的代码来实现微信同声传译的功能。通过引入插件并调用相应的方法,可以实现文本语音的功能。例如,可以使用plugin.textToSpeech方法来实现将指定的文本换为语音的功能。在方法的参数,可以指定语言、是否启用语音合成等。成功调用该方法后,可以在回调函数获取到生成的语音文件的信息。 #### 引用[.reference_title] - *1* *2* *3* [uni-app微信小程序开发,引入微信同声传译插件](https://blog.csdn.net/qq_32837111/article/details/106804236)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来自湖南的阿晨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值