uniapp 开发小程序垃圾分类识别
1.开通京东万象垃圾分类识别API每天有2000次免费调用接口。https://wx.jdcloud.com/market/datas/30/139472.调用接口说明3.完整代码<template><view><view class="content"><!-- 选择图片区域 --><view class="images_box">&l
·
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.效果图
更多推荐
已为社区贡献2条内容
所有评论(0)