uniapp 开发小程序垃圾分类识别

1.开通京东万象垃圾分类识别API

每天有2000次免费调用接口。
https://wx.jdcloud.com/market/datas/30/13947
在这里插入图片描述

2.调用接口说明

在这里插入图片描述

3.完整代码

<template>
	<view>
		<view class="content">
			<!-- 选择图片区域 -->
			<view class="images_box">
				<image class='img' :src='imgTempUrl' mode='aspectFill'></image>
			</view>
			<!-- 按钮区域 -->
			<button class="btn" type="primary" @click="imgAdd">上传图片</button>
			<!-- 识别结果 -->
			<view class="txt" >
				<view class="txt1"> {{cate_name}} </view>
				<view > {{ps}} </view>
			</view>
		</view>
	</view>
</template>

<script>
	var imgB64 = ''	

	export default {
		data() {
			return {	
				imgTempUrl:'/static/ljfl.png',
				cate_name:'',
				ps:''
			}
		},

		// 分享给朋友
		onShareAppMessage: function() {},
		//分享到朋友圈
		onShareTimeline: function() {},

		methods: {
			//选择图片
			imgAdd() {
				wx.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						this.imgTempUrl = res.tempFilePaths[0]
						this.getB64ByUrl(this.imgTempUrl)  //调用方法getB64ByUrl,并传入参数url
						uni.showLoading({
							title:'处理中...'
						})
					}
				})
			},
			
			//图片转换成Base64格式
			getB64ByUrl: function(url) {				
				const FileSystemManager = wx.getFileSystemManager();
				FileSystemManager.readFile({
					filePath: url,
					encoding: 'base64',
					success:(res) => {
						//console.log(res.data)
						imgB64 = res.data
						this.getResult()
					},
					fail(err) {
						console.log(err)
					}
				})
			},

			//调用垃圾分类识别接口
			getResult: function() { 
				let that = this
				wx.request({
					url: 'https://way.jd.com/JDAI/garbageImageSearch?appkey=17b5e****************30ce', 
					method: "post",
					data: {
						cityId: "310000",
						imgBase64: imgB64
					},
					success(res) {
						//console.log(res.data)
						let resData = res.data.result.result.garbage_info
						//把数组的对象以字段confidence进行降序排序
						let resDataDesc = resData.sort(function(a, b){return b.confidence - a.confidence})  
						console.log(resDataDesc)
						that.cate_name = resDataDesc[0].cate_name
						that.ps = resDataDesc[0].ps
					},
					fail(err) {
						console.log(err)
					},
					complete() {
						uni.hideLoading()
					}
				})
			}

		}
	}
</script>

<style>

	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.btn {
		margin: 10px;
		width: 80%;
	}


	.images_box {
		border: 1rpx;
		border-style: solid;
		border-color: rgba(0, 0, 0, 0.452);
		width: 98%;
		height: 600rpx;
		margin: 10rpx;
		position: relative;
	}

	.img {
		width: 100%;
		height: 100%;
	}
	
	.txt{
		margin: 10px;
		font-size: 20px;
		color: #007AFF;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.txt1{
		margin: 10px;
		font-size: 30px;
		color: #ff55ff;
		
	}

</style>

4.效果图

在这里插入图片描述

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UniApp是一种基于Vue.js框架的跨平台开发框架,可以用于开发小程序、H5、App等多个平台的应用程序。使用UniApp开发小程序具有以下特点和优势: 1. 跨平台开发:使用UniApp可以一次编写代码,同时在多个平台上运行,包括微信小程序、支付宝小程序、百度小程序、字节跳动小程序等。 2. 统一开发语言:UniApp使用Vue.js作为开发语言,开发者可以使用熟悉的Vue语法进行开发,降低学习成本。 3. 丰富的组件库:UniApp提供了丰富的组件库,包括基础组件和扩展组件,可以快速构建界面。 4. 强大的性能优化:UniApp在底层做了很多性能优化,包括渲染层和逻辑层的分离、虚拟DOM等,提升了应用的性能和用户体验。 5. 支持原生能力:UniApp支持调用原生API,可以实现更多的功能和交互效果。 使用UniApp开发小程序的步骤如下: 1. 安装UniApp开发工具:下载并安装HBuilderX,它是一款专门用于UniApp开发的集成开发环境。 2. 创建项目:在HBuilderX中创建一个UniApp项目,选择小程序模板,并填写相关信息。 3. 开发页面:在项目中创建页面,使用Vue语法编写页面的结构和样式,添加交互逻辑。 4. 调试和预览:在HBuilderX中可以进行实时预览和调试,可以在模拟器或真机上查看效果。 5. 发布小程序:完成开发后,可以将项目打包成小程序发布到各个平台。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值