camera.vue 4.11 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
<template>
<view class="cameraBg">
<camera device-position="back" flash="auto" style="width: 100%; height: 100vh">
<cover-image src="@/static/index/cameraBg.png" class="scan-img"> </cover-image>
<cover-view class="scanBtn" v-if="scanShow">
<cover-view class="beat" @click="scan">
<cover-image class="beatImg" src="@/static/index/album.png"></cover-image>
<cover-view> 相册 </cover-view>
</cover-view>
<cover-view class="album" @click="takePhoto">
<cover-image class="albumImg" src="@/static/index/beat.png"></cover-image>
<cover-view> 拍照 </cover-view>
</cover-view>
</cover-view>
</camera>
</view>
</template>

<script>
export default {
data() {
return {
scanShow: true
}
},
methods: {
// 相册
scan() {
// 选择图片
uni.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album'],
success: (res) => {
this.compress(res.tempFilePaths[0])
}
})
},
// 启动图片压缩
compress(tempFilePaths) {
const vm = this
vm.scanShow = false
uni.showLoading({
title: '智能识别中...'
})
uni.compressImage({
src: tempFilePaths,
quality: 80,
success: (imageRes) => {
// 获取类型
uni.getImageInfo({
src: imageRes.tempFilePath,
success(imageInfo) {
// 转base64
uni.getFileSystemManager().readFile({
filePath: imageRes.tempFilePath,
encoding: 'base64',
success: (base) => {
// 返回base64格式
const base64Str = 'data:image/' + imageInfo.type + ';base64,' + base.data
vm.camera64(base64Str)
},
fail: (err) => {
console.log(err)
}
})
}
})
}
})
},
// 拿到图片开始进行识别
camera64(base64Str) {
// 拿到base64,不需要base64 就把上层的转换去掉
this.scanShow = true
uni.hideLoading()
uni.showToast({
title: '已识别到图片,看console',
duration: 2000
});
console.log(base64Str,'base64Str图片')
// 此处为后端接口 传base64图片 进行ocr识别
// http
// .ocr({
// imageCode: base64Str
// })
// .then((data) => {
// this.scanShow = true
// if (data.status.success) {
// uni.hideLoading()
// if (data.body.vin) {
// this.goPage('/pages/product/index?vin=' + data.body.vin, true, 'redirectTo')
// } else {
// this.msg('无法识别,请重新拍照')
// }
// }
// })
},
// 拍照
takePhoto() {
const ctx = uni.createCameraContext()
ctx.takePhoto({
quality: 'high',
success: (res) => {
this.compress(res)
}
})
},
error(e) {
console.log(e.detail)
}
}
}
</script>

<style lang="scss" scoped>
.cameraBg {
width: 100%;
height: 100vh;
position: fixed;
.scan-img {
width: 100%;
height: 1624rpx;
z-index: 1;
}
.scanBtn {
width: 100%;
z-index: 99999;
position: fixed;
bottom: 100rpx;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
.beat {
position: absolute;
bottom: 0rpx;
left: 100rpx;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-size: 24rpx;
font-weight: 400;
color: #ffffff;
.beatImg {
width: 88rpx;
height: 88rpx;
margin-bottom: 30rpx;
}
}
.album {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-size: 24rpx;
font-weight: 400;
color: #ffffff;
.albumImg {
width: 120rpx;
height: 120rpx;
margin-bottom: 30rpx;
}
}
}
}
</style>