camera-scan-code.uvue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <view style="flex:1">
  3. <!--
  4. <camera style="width: 100%; height: 300px;" :resolution="'high'" :mode="'scanCode'" @scancode="handleScanCode">
  5. </camera> -->
  6. <swiper class="swiper-box my-swiper" :current="swiperDotIndex" :indicator-dots="true" >
  7. <swiper-item v-for="item in data" >
  8. <view class="swiper-item" @tap="previewSingleImage(item.image)">
  9. <image class="my-image" :src="item.image" mode="aspectFill" />
  10. </view>
  11. </swiper-item>
  12. </swiper>
  13. <view class="camera-scan-code-back-wrap">
  14. <button class="btn block bg-blue lg round" @click="chooseImage">
  15. 浏览相册
  16. </button>
  17. </view>
  18. <view class="camera-scan-code-back-wrap">
  19. <button class="btn block bg-blue lg round" @click="saveImage">
  20. 保存图片
  21. </button>
  22. </view>
  23. <view class="camera-scan-code-back-wrap">
  24. <button type="default" class="btn bg-blue round" @click="navigateBack">返回拍照</button>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. type CameraScanCodeResult = {
  30. type : string | null;
  31. result : string | null;
  32. }
  33. type ImageItem={
  34. image:string,
  35. name:string
  36. }
  37. export default {
  38. data() {
  39. return {
  40. result: null as CameraScanCodeResult | null,
  41. swiperDotIndex: 0 as number,
  42. data: [{
  43. image: '/static/images/banner/banner01.png',
  44. name: 'banner01.png'
  45. },
  46. ] as ImageItem[]
  47. }
  48. },
  49. onLoad(options) {
  50. let path = options?.['path'] ?? ''
  51. console.log(path)
  52. if(path!=''){
  53. this.data = []
  54. this.data.push({image:path, name:''})
  55. }
  56. },
  57. methods: {
  58. navigateBack() {
  59. uni.navigateBack()
  60. },
  61. chooseImage() {
  62. uni.chooseImage({
  63. sourceType: ['album'],
  64. success: (res) => {
  65. //this.data = res.tempFilePaths; // 获取选中的图片路径
  66. this.data = []
  67. for(let i=0; i<res.tempFilePaths.length; i++){
  68. this.data.push({image: res.tempFilePaths[i], name:''})
  69. }
  70. },
  71. fail: (err) => {
  72. console.error('选择图片失败', err);
  73. }
  74. });
  75. },
  76. saveImage() {
  77. uni.chooseImage({
  78. count: 1, // 默认9
  79. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图
  80. sourceType: ['album'], // 可以指定来源是相册还是相机
  81. success: (chooseImageRes) => {
  82. const tempFilePaths = chooseImageRes.tempFilePaths; // 临时文件路径
  83. const tempFiles = chooseImageRes.tempFiles; // 临时文件信息
  84. //保存图片信息到数据库
  85. }
  86. });
  87. },
  88. previewSingleImage(imageUrl:string) {
  89. uni.previewImage({
  90. urls: [imageUrl], // 需要预览的图片链接列表
  91. current: 0, // 当前显示图片的索引
  92. indicator: 'number', // 图片指示器样式
  93. loop: false // 是否可循环预览
  94. });
  95. },
  96. handleScanCode(ev : UniCameraScanCodeEvent) {
  97. const deatil = ev.detail;
  98. this.result = {
  99. type: deatil.type,
  100. result: deatil.result
  101. } as CameraScanCodeResult
  102. }
  103. }
  104. }
  105. </script>
  106. <style>
  107. .camera-scan-code-back-wrap {
  108. display: flex;
  109. justify-content: center;
  110. align-items: center;
  111. }
  112. .camera-scan-code-table {
  113. background-color: white;
  114. margin-top: 20rpx;
  115. }
  116. .camera-scan-code-table-pair {
  117. height: 60rpx;
  118. flex-direction: row;
  119. justify-content: space-between;
  120. align-items: center;
  121. }
  122. .camera-scan-code-table-pair-label {
  123. flex-grow: 1;
  124. margin-left: 15px;
  125. }
  126. .camera-scan-code-table-pair-value{
  127. flex-grow: 2;
  128. }
  129. .camera-scan-code-table-top-line {
  130. border-top: 1px solid #eee;
  131. }
  132. .swiper {
  133. height: 300rpx;
  134. }
  135. .swiper-box {
  136. height: 150px;
  137. }
  138. .swiper-item {
  139. /* #ifndef APP-NVUE */
  140. display: flex;
  141. /* #endif */
  142. flex-direction: column;
  143. justify-content: center;
  144. align-items: center;
  145. color: #fff;
  146. height: 300rpx;
  147. line-height: 300rpx;
  148. }
  149. .btn {
  150. margin-top: 30px;
  151. height: 45px;
  152. }
  153. </style>