基于coze+微信小程序实现图片上传并利用大模型解析

项目截图:

实现代码(直接搬去可用)

前提:需要填写你的oss配置+coze的api授权配置!!!

<template>
	<view class="container">
		<!-- 高斯模糊背景 -->
		<view class="animated-bg">
			<view class="gradient-blob"></view>
			<view class="gradient-blob two"></view>
			<view class="gradient-blob three"></view>
			<view class="gradient-blob four"></view>
			<view class="overlay"></view>
		</view>
		
		<!-- 返回按钮 -->
		        <view class="back-button" @tap="goBack">
		            <text class="back-icon">←</text>
		            <text class="back-text">返回</text>
		        </view>
		
		<!-- 头部区域 -->
		<view class="header">
			<text class="logo-text">单词识别助手</text>
			<text class="subtitle">上传单词图片,快速获取详细释义</text>
		</view>

		<!-- 上传区域 -->
		<view class="upload-section">
			<view class="upload-box" @tap="chooseImage" v-if="!tempFilePath">
				<text class="upload-icon">📷</text>
				<text class="upload-text">点击上传单词图片</text>
				<text class="upload-desc">支持拍照或从相册选择</text>
			</view>
			
			<view class="preview-box" v-else>
				<image :src="tempFilePath" mode="aspectFit" class="preview-image"/>
				<view class="action-buttons">
					<button class="action-btn cancel" @tap="cancelUpload">取消</button>
					<button class="action-btn confirm" @tap="analyzeImage">识别</button>
				</view>
			</view>
		</view>

		<!-- 加载状态 -->
		<view class="loading-state" v-if="loading">
			<view class="pulse-loader">
				<view class="pulse"></view>
				<view class="pulse"></view>
				<view class="pulse"></view>
			</view>
			<text class="loading-text">正在识别中...</text>
		</view>

		<!-- 只有当有单词数据时才显示结果区域 -->
		<div class="word-info" v-if="wordInfo.word">
      <div class="word-header">
        <text class="word">{{ wordInfo.word }}</text>
        <text class="phonetic">{{ wordInfo.phonetic }}</text>
      </div>
      
      <div class="word-content">
        <div class="item">
          <text class="label">词性:</text>
          <text>{{ wordInfo.type }}</text>
        </div>
        
        <div class="item">
          <text class="label">中文含义:</text>
          <text>{{ wordInfo.meaning }}</text>
        </div>
        
        <div class="item">
          <text class="label">例句:</text>
          <text>{{ wordInfo.example }}</text>
        </div>
        
        <div class="item">
          <text class="label">相关词汇:</text>
          <text>{{ wordInfo.similar }}</text>
        </div>
      </div>
    </div>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tempFilePath: '', // 临时图片路径
				loading: false,
				wordResult: null, // 模拟的识别结果
				// 模拟数据
				mockResult: {
					word: "Happiness",
					phonetic: "ˈhæpinəs",
					meanings: [
						{
							partOfSpeech: "名词",
							definition: "幸福;快乐;愉快;幸福感",
							example: "Money doesn't buy happiness."
						},
						{
							partOfSpeech: "名词",
							definition: "适当;恰当;正确",
							example: "She questioned the happiness of his choice of words."
						}
					]
				},
				ossConfig: {
					accessKeyId: '----------------填写你的-----------------',
					accessKeySecret: '----------------填写你的-----------------',
					bucket: '----------------填写你的-----------------',
					region: '----------------填写你的-----------------', // 根据你的实际地区修改
					endpoint: '----------------填写你的-----------------',
				},
				cozeConfig: {
            		apiUrl: 'https://api.coze.cn/open_api/v2/chat',
            		token: '----------------填写你的-----------------',
            		botId: '----------------填写你的-----------------',
            		userId: '----------------填写你的-----------------'
        		},
				wordInfo: {
					word: '',
					phonetic: '',
					meaning: '',
					example: '',
					similar: '',
					pronunciation: ''
				}
			}
		},
		methods: {
			// 返回上一页
			        goBack() {
			            uni.navigateBack({
			                delta: 1 // 返回的页面层数,1 表示返回上一页
			            });
			        },
			// 选择图片
			chooseImage() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						this.tempFilePath = res.tempFilePaths[0];
					}
				});
			},
			// 取消上传
			cancelUpload() {
				this.tempFilePath = '';
				this.wordResult = null;
			},
			// Base64编码函数
			base64Encode(str) {
				const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
				let output = '';
				let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
				let i = 0;
				
				while (i < str.length) {
					chr1 = str.charCodeAt(i++);
					chr2 = str.charCodeAt(i++);
					chr3 = str.charCodeAt(i++);
					
					enc1 = chr1 >> 2;
					enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
					enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
					enc4 = chr3 & 63;
					
					if (isNaN(chr2)) {
						enc3 = enc4 = 64;
					} else if (isNaN(chr3)) {
						enc4 = 64;
					}
					
					output = output +
						chars.charAt(enc1) + chars.charAt(enc2) +
						chars.charAt(enc3) + chars.charAt(enc4);
				}
				return output;
			},
			// 上传到OSS
			async uploadToOSS(filePath) {
				return new Promise((resolve, reject) => {
					const date = new Date();
					date.setHours(date.getHours() + 1); // 设置policy过期时间为1小时后
					const policyText = {
						expiration: date.toISOString(), // 设置过期时间
						conditions: [
							["content-length-range", 0, 5048576000] // 设置上传文件的大小限制
						]
					};
					
					// 生成policy,使用自定义的base64编码函数
					const policy = this.base64Encode(JSON.stringify(policyText));
					
					// 生成signature
					const signature = this.computeSignature(policy, this.ossConfig.accessKeySecret);
					
					// 生成文件名
					const fileName = `word_images/${Date.now()}_${Math.random().toString(36).slice(2)}.jpg`;
					
					uni.uploadFile({
						url: this.ossConfig.endpoint,
						filePath: filePath,
						name: 'file',
						formData: {
							key: fileName,
							policy: policy,
							OSSAccessKeyId: this.ossConfig.accessKeyId,
							signature: signature,
							success_action_status: '200'
						},
						success: (res) => {
							if (res.statusCode === 200) {
								const url = `https://${this.ossConfig.bucket}.${this.ossConfig.region}/${fileName}`;
								resolve(url);
							} else {
								uni.showToast({
									title: '上传失败',
									icon: 'none'
								});
								reject(new Error('上传失败'));
							}
						},
						fail: (err) => {
							uni.showToast({
								title: '上传失败',
								icon: 'none'
							});
							reject(err);
						}
					});
				});
			},
			// 计算签名
			computeSignature(policy, accessKeySecret) {
				// 由于uni-app环境中可能没有crypto-js,这里使用一个简单的方法来计算
				const hmac = function(key, str) {
					const crypto = require('crypto');
					const hmac = crypto.createHmac('sha1', key);
					hmac.update(str);
					return hmac.digest('base64');
				};
				
				return hmac(accessKeySecret, policy);
			},
			// 调用Coze API
			async callCozeAPI(imageUrl) {
				try {
					const response = await uni.request({
						url: this.cozeConfig.apiUrl,
						method: 'POST',
						header: {
							'Authorization': `Bearer ${this.cozeConfig.token}`,
							'Content-Type': 'application/json',
							'Accept': '*/*',
							'Host': 'api.coze.cn',
							'Connection': 'keep-alive'
						},
						data: {
							conversation_id: Date.now().toString(),
							bot_id: this.cozeConfig.botId,
							user: this.cozeConfig.userId,
							query: `请识别这张图片中的单词,图片地址是:${imageUrl}`,
							stream: false
						}
					});
					
					const responseData =  response;
					console.log(responseData)
					const result = JSON.parse(responseData[1].data.messages[1].content);	
					console.log(result)

					// 根据实际返回数据更新显示
					
							
					return result;
					
				} catch (error) {
					console.error('Coze API 调用失败:', error);
					uni.showToast({
						title: error.message || 'API调用失败',
						icon: 'none',
						duration: 3000
					});
					return null;
				}
			},
			// 修改原有的analyzeImage方法
			async analyzeImage() {
				this.loading = true;
				try {
					// 1. 上传图片到OSS
					const imageUrl = await this.uploadToOSS(this.tempFilePath);
					console.log(imageUrl)
					
					// 2. 调用Coze API
					const result = await this.callCozeAPI(imageUrl);
					console.log(result)
					
					// 3. 处理返回结果
					this.wordInfo = {
              			word: result.out2, // pear
              			phonetic: result.yinbiao, // 英/peə(r)/;美/per/
              			type: result.output, // 名词
              			meaning: result.out, // 梨
              			example: result.liju, // I like to eat pears.(我喜欢吃梨。)
              			similar: result.xiangsi // apple(苹果)、banana(香蕉)等,都属于水果类词汇
            		};
				} catch (error) {
					console.error('处理失败:', error);
				} finally {
					this.loading = false;
				}
			}
		}
	}
</script>

<style>
	.container {
		min-height: 100vh;
		position: relative;
		overflow: hidden;
		background: #f8f9fa;
	}

	.animated-bg {
		position: fixed;
		width: 100%;
		height: 100%;
		top: 0;
		left: 0;
		overflow: hidden;
		z-index: 0;
		background: linear-gradient(125deg, #00fff3, #ff00e6);
		animation: gradientBG 15s ease infinite;
	}

	.gradient-blob {
		position: absolute;
		width: 1000rpx;
		height: 1000rpx;
		background: linear-gradient(
			45deg,
			rgba(255, 49, 97, 0.5),
			rgba(255, 97, 162, 0.5)
		);
		border-radius: 50%;
		filter: blur(80px);
		animation: blob-movement 20s infinite linear,
				   color-shift 10s infinite alternate;
		opacity: 0.7;
		top: -300rpx;
		left: -300rpx;
	}

	.gradient-blob.two {
		background: linear-gradient(
			45deg,
			rgba(67, 206, 162, 0.5),
			rgba(24, 90, 157, 0.5)
		);
		animation: blob-movement-2 25s infinite linear,
				   color-shift-2 12s infinite alternate;
		animation-delay: -3s;
		top: 60%;
		left: 60%;
	}

	.gradient-blob.three {
		background: linear-gradient(
			45deg,
			rgba(255, 197, 84, 0.5),
			rgba(255, 159, 0, 0.5)
		);
		animation: blob-movement-3 30s infinite linear,
				   color-shift-3 8s infinite alternate;
		animation-delay: -5s;
		top: 40%;
		left: 30%;
	}

	.gradient-blob.four {
		background: linear-gradient(
			45deg,
			rgba(147, 88, 255, 0.5),
			rgba(19, 123, 255, 0.5)
		);
		animation: blob-movement-4 28s infinite linear,
				   color-shift-4 15s infinite alternate;
		animation-delay: -7s;
		top: 20%;
		left: 70%;
	}

	.overlay {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: rgba(255, 255, 255, 0.2);
		backdrop-filter: blur(20px);
	}

	@keyframes gradientBG {
		0% {
			background-position: 0% 50%;
		}
		50% {
			background-position: 100% 50%;
		}
		100% {
			background-position: 0% 50%;
		}
	}

	@keyframes blob-movement {
		0% {
			transform: translate(0, 0) scale(1) rotate(0deg);
		}
		33% {
			transform: translate(300rpx, 200rpx) scale(1.2) rotate(120deg);
		}
		66% {
			transform: translate(-200rpx, 400rpx) scale(0.8) rotate(240deg);
		}
		100% {
			transform: translate(0, 0) scale(1) rotate(360deg);
		}
	}

	@keyframes blob-movement-2 {
		0% {
			transform: translate(0, 0) scale(1.1) rotate(0deg);
		}
		33% {
			transform: translate(-250rpx, -300rpx) scale(0.9) rotate(120deg);
		}
		66% {
			transform: translate(300rpx, -200rpx) scale(1.2) rotate(240deg);
		}
		100% {
			transform: translate(0, 0) scale(1.1) rotate(360deg);
		}
	}

	@keyframes blob-movement-3 {
		0% {
			transform: translate(0, 0) scale(0.9) rotate(0deg);
		}
		33% {
			transform: translate(400rpx, -100rpx) scale(1.1) rotate(120deg);
		}
		66% {
			transform: translate(-300rpx, 200rpx) scale(0.8) rotate(240deg);
		}
		100% {
			transform: translate(0, 0) scale(0.9) rotate(360deg);
		}
	}

	@keyframes blob-movement-4 {
		0% {
			transform: translate(0, 0) scale(1) rotate(0deg);
		}
		33% {
			transform: translate(-200rpx, 300rpx) scale(1.1) rotate(120deg);
		}
		66% {
			transform: translate(250rpx, -250rpx) scale(0.9) rotate(240deg);
		}
		100% {
			transform: translate(0, 0) scale(1) rotate(360deg);
		}
	}

	@keyframes color-shift {
		0% {
			background: linear-gradient(45deg, rgba(255, 49, 97, 0.5), rgba(255, 97, 162, 0.5));
		}
		100% {
			background: linear-gradient(45deg, rgba(255, 182, 193, 0.5), rgba(255, 20, 147, 0.5));
		}
	}

	@keyframes color-shift-2 {
		0% {
			background: linear-gradient(45deg, rgba(67, 206, 162, 0.5), rgba(24, 90, 157, 0.5));
		}
		100% {
			background: linear-gradient(45deg, rgba(0, 255, 255, 0.5), rgba(0, 128, 128, 0.5));
		}
	}

	@keyframes color-shift-3 {
		0% {
			background: linear-gradient(45deg, rgba(255, 197, 84, 0.5), rgba(255, 159, 0, 0.5));
		}
		100% {
			background: linear-gradient(45deg, rgba(255, 215, 0, 0.5), rgba(255, 140, 0, 0.5));
		}
	}

	@keyframes color-shift-4 {
		0% {
			background: linear-gradient(45deg, rgba(147, 88, 255, 0.5), rgba(19, 123, 255, 0.5));
		}
		100% {
			background: linear-gradient(45deg, rgba(138, 43, 226, 0.5), rgba(65, 105, 225, 0.5));
		}
	}

	/* 头部样式 */
	.header {
		padding: 60rpx 40rpx;
		text-align: center;
		z-index: 1;
		position: relative;
	}

	.logo-text {
		font-size: 48rpx;
		color: #1a1a1a;
		font-weight: 600;
		letter-spacing: 2rpx;
		text-shadow: 0 2px 4px rgba(255, 255, 255, 0.2);
	}

	.subtitle {
		font-size: 28rpx;
		color: rgba(0, 0, 0, 0.7);
		margin-top: 16rpx;
		display: block;
	}

	/* 上传区域样式 */
	.upload-section {
		padding: 40rpx;
		z-index: 2;
		position: relative;
	}

	.upload-box {
		background: rgba(255, 255, 255, 0.2);
		backdrop-filter: blur(20px);
		border-radius: 20rpx;
		padding: 60rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		border: 2rpx dashed rgba(255, 255, 255, 0.5);
	}

	.upload-icon {
		font-size: 80rpx;
		margin-bottom: 20rpx;
	}

	.upload-text {
		font-size: 32rpx;
		color: #333;
		margin-bottom: 10rpx;
	}

	.upload-desc {
		font-size: 24rpx;
		color: rgba(0, 0, 0, 0.5);
	}

	.preview-box {
		width: 100%;
		border-radius: 20rpx;
		overflow: hidden;
	}

	.preview-image {
		width: 100%;
		height: 400rpx;
		border-radius: 20rpx;
	}

	.action-buttons {
		display: flex;
		justify-content: space-between;
		margin-top: 20rpx;
		gap: 20rpx;
	}

	.action-btn {
		flex: 1;
		padding: 20rpx;
		border-radius: 10rpx;
		text-align: center;
		font-size: 28rpx;
	}

	.action-btn.cancel {
		background: rgba(255, 255, 255, 0.3);
		color: #333;
	}

	.action-btn.confirm {
		background: linear-gradient(135deg, #7C3AED, #3B82F6);
		color: #fff;
	}

	.word-text {
		font-size: 40rpx;
		font-weight: bold;
		color: #333;
	}

	.phonetic {
		font-size: 28rpx;
		color: rgba(0, 0, 0, 0.6);
		margin-left: 20rpx;
	}

	.meanings {
		margin-top: 30rpx;
	}

	.meaning-item {
		margin-bottom: 20rpx;
	}

	.pos {
		font-size: 26rpx;
		color: #666;
		background: rgba(0, 0, 0, 0.1);
		padding: 4rpx 12rpx;
		border-radius: 6rpx;
		margin-right: 10rpx;
	}

	.definition {
		font-size: 30rpx;
		color: #333;
		display: block;
		margin: 10rpx 0;
	}

	.example {
		font-size: 26rpx;
		color: #666;
		display: block;
		font-style: italic;
	}

	/* 加载动画 */
	.loading-state {
		padding: 60rpx;
		text-align: center;
	}

	.pulse-loader {
		display: flex;
		justify-content: center;
		gap: 12rpx;
	}

	.pulse {
		width: 16rpx;
		height: 16rpx;
		background: #fff;
		border-radius: 50%;
		animation: pulse 1.5s infinite ease-in-out;
	}

	.pulse:nth-child(2) { animation-delay: 0.2s; }
	.pulse:nth-child(3) { animation-delay: 0.4s; }

	@keyframes pulse {
		0%, 100% { transform: scale(0.5); opacity: 0.2; }
		50% { transform: scale(1); opacity: 1; }
	}

	.loading-text {
		color: rgba(255,255,255,0.7);
		font-size: 28rpx;
		margin-top: 20rpx;
		display: block;
	}

	/* 结果卡片 */
	.result-wrapper {
		padding: 40rpx;
	}

	.result-card {
		background: rgba(255, 255, 255, 0.2);
		backdrop-filter: blur(15px);
		border-radius: 20rpx;
		padding: 30rpx;
		border: 1px solid rgba(255, 255, 255, 0.3);
		animation: slideUp 0.3s ease;
	}

	.result-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;
	}

	.back-button {
	    position: fixed;
	    top: 40rpx;
	    left: 20rpx;
	    display: flex;
	    align-items: center;
	    padding: 10rpx 20rpx;
	    background: rgba(255, 255, 255, 0.8);
	    border-radius: 50rpx;
	    backdrop-filter: blur(10px);
	    z-index: 999;
	    box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
	}
	
	.back-icon {
	    font-size: 36rpx;
	    margin-right: 10rpx;
	    color: #333;
	}
	
	.back-text {
	    font-size: 28rpx;
	    color: #333;
	}

	.word-info {
		padding: 20px;
	}

	.word-header {
		margin-bottom: 20px;
	}

	.word {
		font-size: 24px;
		font-weight: bold;
		margin-right: 10px;
	}

	.phonetic {
		color: #666;
	}

	.item {
		margin-bottom: 15px;
	}

	.label {
		color: #333;
		font-weight: bold;
		margin-right: 10px;
	}
</style>

### Coze服务或应用上传照片更换头像的操作指南 在Coze服务或应用程序中,用户可以通过简单的操作流程来实现基于个人照片生成卡通化头像的功能。以下是具体方法: #### 图片上传与处理 为了利用Coze的服务生成个性化的卡通头像,需按照以下方式完成图片的上传和设置过程。首先,在支持文件输入的应用界面找到用于上传图像的位置,通常会提供一个按钮或者链接入口以便于导入本地存储的照片资源[^1]。 #### 提示词定制 当成功上传所需的基础素材——即用户的原始自拍照之后,下一步便是编辑调整相应的提示参数以满足个性化需求。这些提示可以包括但不限于希望呈现的艺术风格、颜色偏好以及任何其他特定的要求说明。例如,如果想要获得具有“原画”特性的输出,则应在原有基础上增加关于此方面的描述性文字作为指导条件[^2]。 #### 风格切换注意事项 值得注意的是,在尝试不同艺术表现形式的过程中可能会遇到某些局限性;比如从先前选定的一种样式转换至另一种时,可能无法达到预期中的理想效果。这是因为每种预设模板都有其独特的算法逻辑及适用范围,因此对于部分复杂场景下的适应能力可能存在差异。 此外还应注意这样一个细节问题:一旦下达了创建新版本图形的任务请求后,之前所设定的各项配置选项将会被隐藏起来不再显示,这无疑给后续进一步改进带来了不便之处。所以建议在初次定义好各项属性前仔细斟酌考虑清楚再做决定。 ```python # 示例代码展示如何模拟发送带有指定样式的API调用请求 (假设存在这样的功能接口) import requests url = "https://api.coze.com/generate_avatar" payload = { 'image_url': '<your_uploaded_image_url>', 'style': 'original_art', # 设置为目标风格:"原画" } headers = {'Content-Type': 'application/json'} response = requests.post(url, json=payload, headers=headers) if response.status_code == 200: result_data = response.json() else: error_message = f"Error occurred: {response.text}" ``` 以上就是有关如何借助Coze平台将自己的真实面貌转化为虚拟形象的相关介绍及其技术要点解析
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值