index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <view :style="{ minHeight: sys.windowHeight + 'px' }"
  3. :class="[$tm.vx.state().tmVuetify.black ? 'black' : ' ']">
  4. <tm-menubars title="入库" :shadow="0" :showback="true"></tm-menubars>
  5. <tm-message ref="toast"></tm-message>
  6. <tm-button theme="white" @click="shangchuan()">上传</tm-button>
  7. <image block class="zhanshi" mode="widthFix" :src="src"></image>
  8. <tm-button theme="bg-gradient-blue-accent" @click="paizhao()" block>拍照</tm-button>
  9. </view>
  10. </template>
  11. <script>
  12. import{myRequest} from '@/api/request.js'
  13. import { pathToBase64, base64ToPath } from '@/js_sdk/mmmm-image-tools/index.js'
  14. export default {
  15. data() {
  16. return {
  17. src:'',
  18. imgpath : [],
  19. tag : 0,
  20. imgbase64 : []
  21. };
  22. },
  23. onLoad() {
  24. },
  25. created() {
  26. this.sys = uni.getSystemInfoSync();
  27. // uni.request({
  28. // url: "https://jisutqybmf.market.alicloudapi.com/weather/query",
  29. // method: "get",
  30. // header: {
  31. // 'Authorization':'APPCODE e4b9accfb1f246cb8c0766a54efe8730'
  32. // },
  33. // data:{
  34. // city:'南京'
  35. // },
  36. // dataType:'json',
  37. // success : function(data) {
  38. // },
  39. // error : function(e) {
  40. // alert("网络异常,请重试");
  41. // }
  42. // });
  43. },
  44. methods: {
  45. shangchuan(){
  46. var that=this;
  47. uni.chooseImage({ //uni官方api,用于选取本地图片
  48. count: 1, //默认9
  49. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  50. sourceType: ['album','camera'], //从相册选择
  51. success: function (res) {
  52. var imgpath = res.tempFilePaths[0] //先获得本地文件的路径列表,存储到数组
  53. myRequest({
  54. url: "/common/uploads/upload",
  55. method:'post',
  56. data:{
  57. file:imgpath
  58. }
  59. }).then(res => {
  60. if (res.data.code == 200) {
  61. that.$refs.toast.show({model:'success',label:res.data.msg})
  62. }else{
  63. that.$refs.toast.show({model:'error',label:res.data.msg})
  64. }
  65. })
  66. }
  67. })
  68. },
  69. paizhao(){
  70. var that=this;
  71. uni.chooseImage({ //uni官方api,用于选取本地图片
  72. count: 1, //默认9
  73. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  74. sourceType: ['album','camera'], //从相册选择
  75. success: function (res) {
  76. that.imgpath = res.tempFilePaths[0] //先获得本地文件的路径列表,存储到数组
  77. console.log(that.imgpath) //注意该api会将路径转换为blob格式
  78. that.getBase64Image(that.imgpath);
  79. }
  80. })
  81. },
  82. getBase64Image(path){
  83. var that=this;
  84. console.log(" 开始转换")
  85. pathToBase64(path)
  86. .then(base64 => {
  87. that.shibie(base64)
  88. })
  89. .catch(error => {
  90. console.error(error)
  91. })
  92. // for(let i=0;i<that.imgpath.length;i++){
  93. // var url = that.imgpath[i]
  94. // getImgToBase64(url,function(dataURL){
  95. // that.imgbase64.push(dataURL)
  96. // that.shibie(dataURL[0])
  97. // })
  98. // }
  99. // function getImgToBase64(url,callback){
  100. // var canvas = document.createElement('canvas'),
  101. // ctx = canvas.getContext('2d'),
  102. // img = new Image;
  103. // img.crossOrigin = 'Anonymous';
  104. // img.onload = function(){
  105. // canvas.height = img.height;
  106. // canvas.width = img.width;
  107. // ctx.drawImage(img,0,0);
  108. // var dataURL = canvas.toDataURL('image/png');
  109. // callback(dataURL);
  110. // canvas = null;
  111. // };
  112. // img.src = url;
  113. // }
  114. },
  115. shibie(src){
  116. var that=this;
  117. // 抠图
  118. // uni.request({
  119. // url: 'https://objseg.market.alicloudapi.com/commonseg/rgba',
  120. // method : "post",
  121. // header: {
  122. // 'Authorization':'APPCODE e4b9accfb1f246cb8c0766a54efe8730',
  123. // "Content-Type":"application/json;charset=UTF-8"
  124. // },
  125. // data:{
  126. // photo:src
  127. // },
  128. // success : function(data) {
  129. // console.log(data)
  130. // that.src=data.data.data.result
  131. // },
  132. // error : function(e) {
  133. // alert("网络异常,请重试");
  134. // }
  135. // });
  136. // 识别
  137. uni.request({
  138. url: 'https://dect.market.alicloudapi.com/imgdect',
  139. method : "post",
  140. header: {
  141. 'Authorization':'APPCODE e4b9accfb1f246cb8c0766a54efe8730',
  142. "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"
  143. },
  144. data:{
  145. src:src
  146. },
  147. success : function(data) {
  148. console.log(data)
  149. // that.src=data.data.data.result
  150. },
  151. error : function(e) {
  152. alert("网络异常,请重试");
  153. }
  154. });
  155. }
  156. },
  157. }
  158. </script>
  159. <style lang="scss">
  160. /deep/ .tm-menubars .body{
  161. background-color: #1b1b1b !important;
  162. }
  163. </style>