index.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <template>
  2. <view>
  3. <view style="position: fixed;z-index: 100;">
  4. <uni-nav-bar @clickLeft="clickLeft" left-icon="back" title="病虫害识别"></uni-nav-bar>
  5. </view>
  6. <view class="image_box">
  7. <image :src="path" mode="widthFix" class="image"></image>
  8. </view>
  9. <view class="recognition" v-if="datasTF">
  10. <p class="recognition_title">{{name}}</p>
  11. <view :class="tishi?'recognition_img':'recognition_img_two'">
  12. <image :src="path2" mode="" @error="error"></image>
  13. </view>
  14. <button class="recognition_details" v-if="tishi" @click="examine">查看详情</button>
  15. <button class="recognition_details" v-if="!tishi" @click="show=!show">再拍一张</button>
  16. </view>
  17. <view class="datas" v-else>
  18. <p class="datas_title">{{name}}</p>
  19. <view v-for="(item,index) in preventionArr" :key="index" class="prevention">
  20. <view class="prevention_title">
  21. <u-icon name="play-right-fill" size="24" color="#55A92D"></u-icon>
  22. <p>{{regexptitle[index]}}:</p>
  23. </view>
  24. <p class="prevention_con">{{item}}</p>
  25. </view>
  26. <button class="recognition_details" v-if="tishi" @click="examine">查看图片</button>
  27. </view>
  28. <u-modal v-model="show" :mask-close-able="true" title="" :show-cancel-button="true" confirm-text="拍病害" cancel-text="拍虫害"
  29. content="拍照识别病虫害" @confirm="confirm" @cancel="cancel"></u-modal>
  30. <kps-image-cutter @ok="onok" @cancel="oncancle" :url="url" :fixed="false" :blob="false" :maxWidth="500" :maxHeight="500"></kps-image-cutter>
  31. </view>
  32. </template>
  33. <script>
  34. import kpsImageCutter from "@/components/ksp-image-cutter/ksp-image-cutter.vue";
  35. export default {
  36. components: {
  37. kpsImageCutter
  38. },
  39. data() {
  40. return {
  41. path: "",
  42. path2: "",
  43. tishi: true,
  44. name: "",
  45. prevention: "",
  46. datasTF: true,
  47. show: false,
  48. url: "",
  49. preventionArr: [],
  50. regexptitle: []
  51. }
  52. },
  53. methods: {
  54. // pest.pests.insect_discern 虫害
  55. async worm(data) {
  56. const res = await this.$myRequest({
  57. url: '/api/api_gateway?method=pest.pests.insect_discern',
  58. data: {
  59. img_file: data
  60. }
  61. })
  62. // this.res = res.data
  63. console.log(res)
  64. if (res.data.img_urls == undefined) {
  65. this.name = "识别失败,请换张图片"
  66. this.tishi = false
  67. this.path2 = "../../static/image/10ca93e17420371a82826073c8425c0.png"
  68. } else {
  69. this.path2 = res.data.img_urls
  70. this.name = res.data.name
  71. this.tishi = true
  72. this.prevention = res.data.prevention
  73. }
  74. },
  75. //pest.pests.insect_discern病害识别
  76. async disease(data) {
  77. const res = await this.$myRequest({
  78. url: '/api/api_gateway?method=pest.pests.plant_discern',
  79. data: {
  80. img_file: data
  81. }
  82. })
  83. console.log(res)
  84. // this.res = res.data
  85. if (res.data.img_urls == undefined) {
  86. this.name = "识别失败,请换张图片"
  87. this.tishi = false
  88. this.path2 = "../../static/image/10ca93e17420371a82826073c8425c0.png"
  89. } else {
  90. this.path2 = res.data.img_urls
  91. this.name = res.data.name
  92. this.tishi = true
  93. this.prevention = res.data.prevention
  94. }
  95. },
  96. clickLeft() {
  97. uni.switchTab({
  98. url: "../index/index"
  99. })
  100. },
  101. examine() {
  102. this.datasTF = !this.datasTF
  103. },
  104. confirm() {
  105. this.flag = 1
  106. uni.chooseImage({
  107. count: 1, //默认9
  108. // sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  109. sourceType: ['camera', 'album'], //从相册选择
  110. success: (res) => {
  111. this.url = res.tempFilePaths[0]
  112. }
  113. });
  114. },
  115. cancel() {
  116. this.flag = 2
  117. uni.chooseImage({
  118. count: 1, //默认9
  119. // sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  120. sourceType: ['camera', 'album'], //从相册选择
  121. success: (res) => {
  122. console.log(1)
  123. this.url = res.tempFilePaths[0]
  124. }
  125. });
  126. },
  127. onok(ev) {
  128. if (this.flag == 2) {
  129. // pest.pests.insect_discern 虫害
  130. uni.uploadFile({
  131. // url: 'http://182.92.193.64:8002/api/api_gateway?method=base.bases.base_photo', //仅为示例,非真实的接口地址
  132. url: 'http://182.92.193.64:8002/api/api_gateway?method=pest.pests.insect_discern', //仅为示例,非真实的接口地址
  133. filePath: ev.path,
  134. name: 'img_file',
  135. formData: {
  136. 'user': 'test'
  137. },
  138. success: (uploadFileRes) => {
  139. console.log(JSON.parse(uploadFileRes.data))
  140. this.optionverify(JSON.parse(uploadFileRes.data).data)
  141. }
  142. });
  143. } else if (this.flag == 1) {
  144. //pest.pests.insect_discern病害识别
  145. uni.uploadFile({
  146. // url: 'http://182.92.193.64:8002/api/api_gateway?method=base.bases.base_photo', //仅为示例,非真实的接口地址
  147. url: 'http://182.92.193.64:8002/api/api_gateway?method=pest.pests.plant_discern', //仅为示例,非真实的接口地址
  148. filePath: ev.path,
  149. name: 'img_file',
  150. formData: {
  151. 'user': 'test'
  152. },
  153. success: (uploadFileRes) => {
  154. console.log(JSON.parse(uploadFileRes.data))
  155. this.optionverify(JSON.parse(uploadFileRes.data).data)
  156. }
  157. });
  158. }
  159. this.url = "";
  160. },
  161. oncancle() {
  162. // url设置为空,隐藏控件
  163. this.url = ''
  164. },
  165. error() {
  166. this.path2 = "../../static/image/e1cd85dc59139760f43ddbac15136f2.png"
  167. },
  168. optionverify(data){
  169. if (data == null) {
  170. this.name = "识别失败,请换张图片"
  171. this.tishi = false
  172. this.path2 = "../../static/image/10ca93e17420371a82826073c8425c0.png"
  173. } else {
  174. this.path2 = data.img_urls
  175. this.name = data.name
  176. this.tishi = true
  177. this.prevention = data.prevention
  178. var regex2 = /\[(.+?)\]/g; // [] 中括号
  179. var str = this.prevention
  180. var arr = str.match(regex2)
  181. var arrindex = []
  182. for (var i = 0; i < arr.length; i++) {
  183. arrindex.push(str.indexOf(arr[i]))
  184. }
  185. for (var i = 0; i < arr.length; i++) {
  186. this.preventionArr.push(str.slice(arrindex[i] + arr[i].length, arrindex[i + 1]))
  187. }
  188. for (var i = 0; i < arr.length; i++) {
  189. arr[i] = arr[i].slice(1, arr[i].length - 1)
  190. }
  191. this.regexptitle = arr
  192. }
  193. }
  194. },
  195. onLoad(option) {
  196. console.log(option)
  197. this.path = option.path
  198. this.optionverify(JSON.parse(option.datas).data)
  199. }
  200. }
  201. </script>
  202. <style lang="scss">
  203. .image_box{
  204. position: absolute;
  205. top: 44px;
  206. width: 100%;
  207. height: 400rpx;
  208. display: flex;
  209. justify-content: center;
  210. .image {
  211. margin: 0 auto;
  212. }
  213. }
  214. .recognition {
  215. position: absolute;
  216. top: 254px;
  217. width: 100%;
  218. height: 800rpx;
  219. .recognition_title {
  220. width: 100%;
  221. text-align: center;
  222. font-size: 36rpx;
  223. font-weight: 700;
  224. margin: 60rpx 0;
  225. }
  226. .recognition_img {
  227. border: 2rpx dashed #06B535;
  228. width: 450rpx;
  229. height: 450rpx;
  230. border-radius: 50%;
  231. margin: 0 auto;
  232. text-align: center;
  233. line-height: 450rpx;
  234. image {
  235. margin-top: 50rpx;
  236. width: 340rpx;
  237. height: 340rpx;
  238. }
  239. }
  240. .recognition_img_two {
  241. width: 450rpx;
  242. height: 450rpx;
  243. border-radius: 50%;
  244. margin: 0 auto;
  245. text-align: center;
  246. line-height: 450rpx;
  247. image {
  248. margin-top: 10rpx;
  249. width: 340rpx;
  250. height: 340rpx;
  251. }
  252. }
  253. .recognition_details {
  254. width: 400rpx;
  255. height: 80rpx;
  256. border-radius: 40rpx;
  257. margin: 30rpx auto;
  258. font-size: 30rpx;
  259. background-color: #67B25F;
  260. color: #FFFFFF;
  261. }
  262. }
  263. .datas {
  264. position: absolute;
  265. top: 264px;
  266. width: 95%;
  267. left: 2.5%;
  268. .datas_title{
  269. font-size: 32rpx;
  270. font-weight: 700;
  271. margin-left: 30rpx;
  272. }
  273. .recognition_details {
  274. width: 400rpx;
  275. height: 80rpx;
  276. border-radius: 40rpx;
  277. margin: 30rpx auto;
  278. font-size: 30rpx;
  279. background-color: #67B25F;
  280. color: #FFFFFF;
  281. }
  282. }
  283. .prevention{
  284. font-size: 28rpx;
  285. color: #919191;
  286. width: 95%;
  287. margin: 20rpx auto;
  288. .prevention_title{
  289. display: flex;
  290. p{
  291. margin-left: 20rpx;
  292. color: #000000;
  293. font-size: 28rpx;
  294. font-weight: 700;
  295. }
  296. }
  297. .prevention_con{
  298. padding-left: 6%;
  299. }
  300. }
  301. </style>