camera-scan-code.uvue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. success: (res) => {
  64. //this.data = res.tempFilePaths; // 获取选中的图片路径
  65. this.data = []
  66. for(let i=0; i<res.tempFilePaths.length; i++){
  67. this.data.push({image: res.tempFilePaths[i], name:''})
  68. }
  69. },
  70. fail: (err) => {
  71. console.error('选择图片失败', err);
  72. }
  73. });
  74. },
  75. saveImage() {
  76. uni.chooseImage({
  77. count: 1, // 默认9
  78. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图
  79. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机
  80. success: (chooseImageRes) => {
  81. const tempFilePaths = chooseImageRes.tempFilePaths; // 临时文件路径
  82. const tempFiles = chooseImageRes.tempFiles; // 临时文件信息
  83. //保存图片信息到数据库
  84. }
  85. });
  86. },
  87. previewSingleImage(imageUrl:string) {
  88. uni.previewImage({
  89. urls: [imageUrl], // 需要预览的图片链接列表
  90. current: 0, // 当前显示图片的索引
  91. indicator: 'number', // 图片指示器样式
  92. loop: false // 是否可循环预览
  93. });
  94. },
  95. handleScanCode(ev : UniCameraScanCodeEvent) {
  96. const deatil = ev.detail;
  97. this.result = {
  98. type: deatil.type,
  99. result: deatil.result
  100. } as CameraScanCodeResult
  101. }
  102. }
  103. }
  104. </script>
  105. <style>
  106. .camera-scan-code-back-wrap {
  107. display: flex;
  108. justify-content: center;
  109. align-items: center;
  110. }
  111. .camera-scan-code-table {
  112. background-color: white;
  113. margin-top: 20rpx;
  114. }
  115. .camera-scan-code-table-pair {
  116. height: 60rpx;
  117. flex-direction: row;
  118. justify-content: space-between;
  119. align-items: center;
  120. }
  121. .camera-scan-code-table-pair-label {
  122. flex-grow: 1;
  123. margin-left: 15px;
  124. }
  125. .camera-scan-code-table-pair-value{
  126. flex-grow: 2;
  127. }
  128. .camera-scan-code-table-top-line {
  129. border-top: 1px solid #eee;
  130. }
  131. .swiper {
  132. height: 300rpx;
  133. }
  134. .swiper-box {
  135. height: 150px;
  136. }
  137. .swiper-item {
  138. /* #ifndef APP-NVUE */
  139. display: flex;
  140. /* #endif */
  141. flex-direction: column;
  142. justify-content: center;
  143. align-items: center;
  144. color: #fff;
  145. height: 300rpx;
  146. line-height: 300rpx;
  147. }
  148. .btn {
  149. margin-top: 30px;
  150. height: 45px;
  151. }
  152. </style>