index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. <template>
  2. <view class="scan-page">
  3. <!-- 顶部导航栏 -->
  4. <view class="header-bg" :style="{ paddingTop: statusBarHeight + 'px' }">
  5. <view class="header-content">
  6. <view class="back-btn" @click="handleBack">
  7. <text class="back-icon">‹</text>
  8. </view>
  9. <text class="header-title">小程序扫描</text>
  10. <view class="placeholder"></view>
  11. </view>
  12. </view>
  13. <!-- 扫描区域 -->
  14. <view class="scan-area">
  15. <camera
  16. device-position="back"
  17. flash="off"
  18. class="camera"
  19. @error="handleCameraError"
  20. >
  21. <!-- 检测边框覆盖层 -->
  22. <canvas
  23. v-if="detectedBorders.length > 0"
  24. canvas-id="borderCanvas"
  25. class="border-canvas"
  26. :style="{ width: canvasWidth + 'px', height: canvasHeight + 'px' }"
  27. ></canvas>
  28. </camera>
  29. <!-- 模式切换按钮 - 在底部操作栏上方 -->
  30. <view class="mode-switch-wrapper">
  31. <view class="mode-switch">
  32. <view
  33. class="mode-btn"
  34. :class="{ active: scanMode === 'single' }"
  35. @click="switchToSingle"
  36. >
  37. <text class="mode-text">单页</text>
  38. </view>
  39. <view
  40. class="mode-btn"
  41. :class="{ active: scanMode === 'multiple' }"
  42. @click="switchToMultiple"
  43. >
  44. <text class="mode-text">多页</text>
  45. </view>
  46. </view>
  47. </view>
  48. <!-- 底部操作按钮 -->
  49. <view class="action-buttons">
  50. <view class="action-btn" @click="handleSelectImage">
  51. <image class="btn-icon" src="/static/pages/scan/image.png" mode="aspectFit" />
  52. <text class="btn-text">导入图片</text>
  53. </view>
  54. <view class="capture-btn" @click="handleCapture">
  55. <view class="capture-inner"></view>
  56. </view>
  57. <view class="action-btn" @click="handleSelectFile">
  58. <image class="btn-icon" src="/static/pages/scan/file.png" mode="aspectFit" />
  59. <text class="btn-text">导入文档</text>
  60. </view>
  61. </view>
  62. </view>
  63. </view>
  64. </template>
  65. <script setup>
  66. import { ref, onMounted, onUnmounted } from 'vue'
  67. import { useI18n } from 'vue-i18n'
  68. const { t } = useI18n()
  69. // 状态栏高度
  70. const statusBarHeight = ref(0)
  71. // 扫描模式:single-单页,multiple-多页
  72. const scanMode = ref('single')
  73. // 检测到的边框
  74. const detectedBorders = ref([])
  75. // Canvas尺寸
  76. const canvasWidth = ref(0)
  77. const canvasHeight = ref(0)
  78. // 相机上下文
  79. let cameraContext = null
  80. // Canvas上下文
  81. let canvasContext = null
  82. // 扫描定时器
  83. let scanTimer = null
  84. onMounted(() => {
  85. // 获取系统信息
  86. const windowInfo = uni.getWindowInfo()
  87. statusBarHeight.value = windowInfo.statusBarHeight || 0
  88. // 获取屏幕尺寸
  89. const systemInfo = uni.getSystemInfoSync()
  90. canvasWidth.value = systemInfo.windowWidth
  91. canvasHeight.value = systemInfo.windowHeight - statusBarHeight.value - 88 - 200 // 减去头部和底部按钮高度
  92. // 初始化相机上下文
  93. cameraContext = uni.createCameraContext()
  94. // 初始化Canvas上下文
  95. canvasContext = uni.createCanvasContext('borderCanvas')
  96. // 开始实物检测
  97. startDetection()
  98. })
  99. onUnmounted(() => {
  100. // 停止检测
  101. stopDetection()
  102. })
  103. // 返回上一页
  104. const handleBack = () => {
  105. uni.reLaunch({
  106. url: '/pages/home/index'
  107. })
  108. }
  109. // 切换扫描模式
  110. const toggleScanMode = () => {
  111. scanMode.value = scanMode.value === 'single' ? 'multiple' : 'single'
  112. uni.showToast({
  113. title: scanMode.value === 'single' ? '已切换到单页模式' : '已切换到多页模式',
  114. icon: 'none',
  115. duration: 1500
  116. })
  117. }
  118. // 切换到单页模式
  119. const switchToSingle = () => {
  120. if (scanMode.value === 'single') return
  121. scanMode.value = 'single'
  122. uni.showToast({
  123. title: '已切换到单页模式',
  124. icon: 'none',
  125. duration: 1500
  126. })
  127. }
  128. // 切换到多页模式
  129. const switchToMultiple = () => {
  130. if (scanMode.value === 'multiple') return
  131. scanMode.value = 'multiple'
  132. uni.showToast({
  133. title: '已切换到多页模式',
  134. icon: 'none',
  135. duration: 1500
  136. })
  137. }
  138. // 相机错误处理
  139. const handleCameraError = (e) => {
  140. console.error('相机错误:', e)
  141. uni.showToast({
  142. title: '相机启动失败',
  143. icon: 'none'
  144. })
  145. }
  146. // 开始实物检测
  147. const startDetection = () => {
  148. // 每隔一段时间进行一次实物检测
  149. scanTimer = setInterval(() => {
  150. detectObject()
  151. }, 300) // 每300ms检测一次
  152. }
  153. // 停止检测
  154. const stopDetection = () => {
  155. if (scanTimer) {
  156. clearInterval(scanTimer)
  157. scanTimer = null
  158. }
  159. }
  160. // 检测实物
  161. const detectObject = () => {
  162. if (!cameraContext) return
  163. // 拍摄临时照片用于检测
  164. cameraContext.takePhoto({
  165. quality: 'low', // 使用低质量以提高检测速度
  166. success: (res) => {
  167. // 分析图片检测实物
  168. analyzeImage(res.tempImagePath)
  169. },
  170. fail: () => {
  171. // 静默失败,继续下一次检测
  172. }
  173. })
  174. }
  175. // 分析图片中的实物
  176. const analyzeImage = (imagePath) => {
  177. // TODO: 调用后端API或图像识别服务检测实物
  178. // 这里需要接入实际的物体检测API,例如:
  179. // - 百度AI文字识别OCR
  180. // - 腾讯云文档识别
  181. // - 阿里云文档检测
  182. // 模拟检测结果 - 检测到1-3个实物
  183. const hasObject = Math.random() > 0.3 // 70%概率检测到实物
  184. if (hasObject) {
  185. const objectCount = Math.floor(Math.random() * 3) + 1 // 1-3个实物
  186. const borders = []
  187. for (let i = 0; i < objectCount; i++) {
  188. // 生成随机位置的实物边框(模拟检测结果)
  189. const x = Math.random() * (canvasWidth.value - 200) + 50
  190. const y = Math.random() * (canvasHeight.value - 300) + 50
  191. const width = Math.random() * 200 + 150
  192. const height = Math.random() * 250 + 200
  193. borders.push({
  194. x,
  195. y,
  196. width,
  197. height,
  198. // 四个角点坐标(用于绘制更精确的边框)
  199. points: [
  200. { x, y }, // 左上
  201. { x: x + width, y }, // 右上
  202. { x: x + width, y: y + height }, // 右下
  203. { x, y: y + height } // 左下
  204. ]
  205. })
  206. }
  207. detectedBorders.value = borders
  208. // 绘制边框
  209. drawBorders(borders)
  210. // 自动对焦到第一个检测到的实物
  211. if (borders.length > 0) {
  212. autoFocus(borders[0])
  213. }
  214. } else {
  215. // 没有检测到实物,清空边框
  216. detectedBorders.value = []
  217. clearCanvas()
  218. }
  219. }
  220. // 绘制检测到的边框
  221. const drawBorders = (borders) => {
  222. if (!canvasContext || !borders || borders.length === 0) return
  223. // 清空画布
  224. canvasContext.clearRect(0, 0, canvasWidth.value, canvasHeight.value)
  225. // 绘制每个检测到的边框
  226. borders.forEach((border, index) => {
  227. // 设置边框样式
  228. canvasContext.setStrokeStyle('#1ec9c9')
  229. canvasContext.setLineWidth(3)
  230. canvasContext.setLineDash([10, 5], 0) // 虚线效果
  231. // 绘制四边形边框
  232. canvasContext.beginPath()
  233. canvasContext.moveTo(border.points[0].x, border.points[0].y)
  234. border.points.forEach((point, i) => {
  235. if (i > 0) {
  236. canvasContext.lineTo(point.x, point.y)
  237. }
  238. })
  239. canvasContext.closePath()
  240. canvasContext.stroke()
  241. // 绘制四个角点
  242. border.points.forEach(point => {
  243. canvasContext.beginPath()
  244. canvasContext.arc(point.x, point.y, 8, 0, 2 * Math.PI)
  245. canvasContext.setFillStyle('#1ec9c9')
  246. canvasContext.fill()
  247. // 角点外圈
  248. canvasContext.beginPath()
  249. canvasContext.arc(point.x, point.y, 12, 0, 2 * Math.PI)
  250. canvasContext.setStrokeStyle('#ffffff')
  251. canvasContext.setLineWidth(2)
  252. canvasContext.stroke()
  253. })
  254. // 添加半透明填充
  255. canvasContext.setFillStyle('rgba(30, 201, 201, 0.1)')
  256. canvasContext.beginPath()
  257. canvasContext.moveTo(border.points[0].x, border.points[0].y)
  258. border.points.forEach((point, i) => {
  259. if (i > 0) {
  260. canvasContext.lineTo(point.x, point.y)
  261. }
  262. })
  263. canvasContext.closePath()
  264. canvasContext.fill()
  265. })
  266. // 绘制到屏幕
  267. canvasContext.draw()
  268. }
  269. // 清空画布
  270. const clearCanvas = () => {
  271. if (!canvasContext) return
  272. canvasContext.clearRect(0, 0, canvasWidth.value, canvasHeight.value)
  273. canvasContext.draw()
  274. }
  275. // 自动对焦到实物
  276. const autoFocus = (position) => {
  277. if (!cameraContext || !position) return
  278. // 计算实物中心点(相对于屏幕的比例)
  279. const centerX = (position.x + position.width / 2) / 750
  280. const centerY = (position.y + position.height / 2) / 1334
  281. // 设置对焦点
  282. console.log('自动对焦到实物:', centerX, centerY)
  283. // 注意:实际对焦效果取决于设备和平台支持
  284. }
  285. // 拍照
  286. const handleCapture = () => {
  287. if (!cameraContext) return
  288. // 暂停检测
  289. stopDetection()
  290. cameraContext.takePhoto({
  291. quality: 'high',
  292. success: (res) => {
  293. console.log('拍照成功:', res.tempImagePath)
  294. uni.showLoading({
  295. title: '处理中...',
  296. mask: true
  297. })
  298. // TODO: 上传图片到服务器
  299. setTimeout(() => {
  300. uni.hideLoading()
  301. uni.showToast({
  302. title: '拍照成功',
  303. icon: 'success'
  304. })
  305. // 恢复检测
  306. startDetection()
  307. }, 1000)
  308. },
  309. fail: (err) => {
  310. console.error('拍照失败:', err)
  311. uni.showToast({
  312. title: '拍照失败',
  313. icon: 'none'
  314. })
  315. // 恢复检测
  316. startDetection()
  317. }
  318. })
  319. }
  320. // 导入图片
  321. const handleSelectImage = () => {
  322. stopDetection()
  323. uni.chooseImage({
  324. count: 1,
  325. sizeType: ['original', 'compressed'],
  326. sourceType: ['album'],
  327. success: (res) => {
  328. console.log('选择图片成功:', res.tempFilePaths)
  329. uni.showLoading({
  330. title: '处理中...',
  331. mask: true
  332. })
  333. // TODO: 上传图片到服务器
  334. setTimeout(() => {
  335. uni.hideLoading()
  336. uni.showToast({
  337. title: '图片导入成功',
  338. icon: 'success'
  339. })
  340. startDetection()
  341. }, 1000)
  342. },
  343. fail: () => {
  344. startDetection()
  345. }
  346. })
  347. }
  348. // 导入文档
  349. const handleSelectFile = () => {
  350. stopDetection()
  351. uni.chooseMessageFile({
  352. count: 1,
  353. type: 'file',
  354. success: (res) => {
  355. console.log('选择文件成功:', res.tempFiles)
  356. uni.showLoading({
  357. title: '处理中...',
  358. mask: true
  359. })
  360. // TODO: 上传文件到服务器
  361. setTimeout(() => {
  362. uni.hideLoading()
  363. uni.showToast({
  364. title: '文档导入成功',
  365. icon: 'success'
  366. })
  367. startDetection()
  368. }, 1000)
  369. },
  370. fail: () => {
  371. startDetection()
  372. }
  373. })
  374. }
  375. </script>
  376. <style lang="scss" scoped>
  377. .scan-page {
  378. width: 100%;
  379. height: 100vh;
  380. display: flex;
  381. flex-direction: column;
  382. background-color: #000000;
  383. // 顶部导航栏
  384. .header-bg {
  385. background: linear-gradient(180deg, #1ec9c9 0%, #1eb8b8 100%);
  386. position: relative;
  387. z-index: 100;
  388. .header-content {
  389. height: 88rpx;
  390. display: flex;
  391. align-items: center;
  392. justify-content: space-between;
  393. padding: 0 24rpx;
  394. .back-btn {
  395. width: 60rpx;
  396. height: 60rpx;
  397. display: flex;
  398. align-items: center;
  399. justify-content: flex-start;
  400. .back-icon {
  401. font-size: 56rpx;
  402. color: #ffffff;
  403. font-weight: 300;
  404. }
  405. }
  406. .header-title {
  407. flex: 1;
  408. text-align: center;
  409. font-size: 32rpx;
  410. font-weight: 600;
  411. color: #ffffff;
  412. }
  413. .placeholder {
  414. width: 60rpx;
  415. }
  416. }
  417. }
  418. // 扫描区域
  419. .scan-area {
  420. flex: 1;
  421. position: relative;
  422. display: flex;
  423. flex-direction: column;
  424. .camera {
  425. flex: 1;
  426. width: 100%;
  427. position: relative;
  428. // 边框检测画布
  429. .border-canvas {
  430. position: absolute;
  431. top: 0;
  432. left: 0;
  433. z-index: 10;
  434. pointer-events: none;
  435. }
  436. }
  437. // 模式切换按钮 - 在底部操作栏上方
  438. .mode-switch-wrapper {
  439. position: absolute;
  440. bottom: 280rpx;
  441. left: 50%;
  442. transform: translateX(-50%);
  443. z-index: 10;
  444. .mode-switch {
  445. display: flex;
  446. background: rgba(0, 0, 0, 0.6);
  447. border-radius: 40rpx;
  448. padding: 6rpx;
  449. backdrop-filter: blur(10rpx);
  450. .mode-btn {
  451. padding: 12rpx 32rpx;
  452. border-radius: 34rpx;
  453. transition: all 0.3s;
  454. .mode-text {
  455. font-size: 26rpx;
  456. color: rgba(255, 255, 255, 0.6);
  457. font-weight: 500;
  458. transition: all 0.3s;
  459. }
  460. &.active {
  461. background: rgba(255, 255, 255, 0.9);
  462. .mode-text {
  463. color: #333333;
  464. font-weight: 600;
  465. }
  466. }
  467. &:active {
  468. transform: scale(0.95);
  469. }
  470. }
  471. }
  472. }
  473. // 底部操作按钮
  474. .action-buttons {
  475. position: absolute;
  476. bottom: 0;
  477. left: 0;
  478. right: 0;
  479. background: #ffffff;
  480. padding: 32rpx 40rpx;
  481. padding-bottom: calc(32rpx + env(safe-area-inset-bottom));
  482. display: flex;
  483. align-items: center;
  484. justify-content: space-between;
  485. .action-btn {
  486. display: flex;
  487. flex-direction: column;
  488. align-items: center;
  489. gap: 12rpx;
  490. .btn-icon {
  491. width: 48rpx;
  492. height: 48rpx;
  493. }
  494. .btn-text {
  495. font-size: 24rpx;
  496. color: #666666;
  497. }
  498. }
  499. .capture-btn {
  500. width: 120rpx;
  501. height: 120rpx;
  502. border-radius: 50%;
  503. background: #1ec9c9;
  504. display: flex;
  505. align-items: center;
  506. justify-content: center;
  507. box-shadow: 0 4rpx 16rpx rgba(30, 201, 201, 0.4);
  508. &:active {
  509. transform: scale(0.95);
  510. }
  511. .capture-inner {
  512. width: 100rpx;
  513. height: 100rpx;
  514. border-radius: 50%;
  515. background: #ffffff;
  516. }
  517. }
  518. }
  519. }
  520. }
  521. </style>