Camera.uvue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <template>
  2. <view class="camera-container">
  3. <!-- 全屏相机预览 -->
  4. <camera class="camera-preview" :resolution="'medium'" :device-position="devicePosition" :flash="flash"
  5. :frame-size="frameSize" @stop="handleStop" @error="handleError" @initdone="handleInitDone">
  6. </camera>
  7. <!-- 左上角:闪光灯图标按钮 -->
  8. <view class="top-left">
  9. <button class="flash-btn" @click="switchFlash">
  10. <text class="icon">{{ flash === 'torch' ? '💡' : '🔦' }}</text>
  11. </button>
  12. </view>
  13. <!-- 右上角:图像质量设置 -->
  14. <view class="top-right">
  15. <view class="quality-setting">
  16. <text class="setting-label">成像质量</text>
  17. <radio-group class="quality-group" @change="takePhotoQualityChange">
  18. <radio class="quality-radio" value="normal" :checked="quality === 'normal'">普通</radio>
  19. <radio class="quality-radio" value="high" :checked="quality === 'high'">高清</radio>
  20. <radio class="quality-radio" value="original" :checked="quality === 'original'">原图</radio>
  21. </radio-group>
  22. </view>
  23. </view>
  24. <!-- 底部中间:圆形拍照按钮 -->
  25. <view class="bottom-center">
  26. <button class="shoot-btn" @click="handleTakePhoto">
  27. <view class="shoot-inner"></view>
  28. </button>
  29. </view>
  30. <!-- 左下角:相册预览 -->
  31. <view class="bottom-left">
  32. <view class="album-preview" @click="handleScanCode">
  33. <image class="preview-img" v-if="imageSrc" :src="imageSrc"></image>
  34. <text class="preview-tip" v-else>浏览图片</text>
  35. </view>
  36. </view>
  37. <!-- 下方:缩放控制(位置保持不变) -->
  38. <view class="zoom-control">
  39. <text class="setting-label">预览缩放</text>
  40. <view class="zoom-container">
  41. <slider class="zoom-slider" :disabled="maxZoom <= 1" :show-value="true" :min="1" :max="maxZoom"
  42. :value="1" @change="zoomSliderChange" />
  43. </view>
  44. </view>
  45. </view>
  46. </template>
  47. <script>
  48. export default {
  49. data() {
  50. return {
  51. devicePosition: "back",
  52. flash: "off",
  53. frameSize: "medium",
  54. listener: null as CameraContextCameraFrameListener | null,
  55. maxZoom: 0,
  56. imageSrc: "",
  57. quality: "normal",
  58. timeout: 30,
  59. compressed: false,
  60. videoSrc: "",
  61. startRecordStatus: false,
  62. remain: 0,
  63. intervalId: -1,
  64. timeoutStr: '30',
  65. recordId: 0,
  66. senum: 0,
  67. maxcount: 0,
  68. }
  69. },
  70. onLoad(options) {
  71. let recordId = options?.['id'] ?? ''
  72. let senum = options?.['senum'] ?? ''
  73. let maxcount = options?.['maxcount'] ?? ''
  74. if(recordId!=''){
  75. this.recordId = parseInt(recordId)
  76. }
  77. if(senum!=''){
  78. this.senum = parseInt(senum)
  79. }
  80. if(maxcount!=''){
  81. this.maxcount = parseInt(maxcount)
  82. }
  83. },
  84. methods: {
  85. handleScanCode() {
  86. if(this.imageSrc!=""){
  87. let params = "path="+this.imageSrc+"&recordId="+this.recordId+"&senum="+this.senum+"&maxcount="+this.maxcount
  88. uni.navigateTo({
  89. url: "/pages/work/record/camera-scan-code?"+params
  90. })
  91. }else{
  92. uni.showToast({
  93. title: '没有拍照文件',
  94. icon: 'error',
  95. duration: 2000
  96. });
  97. }
  98. },
  99. switchDevicePosition() {
  100. this.devicePosition = this.devicePosition == "back" ? "front" : "back"
  101. },
  102. switchFlash() {
  103. this.flash = this.flash == "torch" ? "off" : "torch"
  104. },
  105. handleStop(e : UniCameraStopEvent) {
  106. console.log("stop", e.detail);
  107. },
  108. handleError(e : UniCameraErrorEvent) {
  109. console.log("error", e.detail);
  110. },
  111. handleInitDone(e : UniCameraInitDoneEvent) {
  112. console.log("initdone", e.detail);
  113. this.maxZoom = e.detail.maxZoom ?? 0
  114. },
  115. zoomSliderChange(event : UniSliderChangeEvent) {
  116. const value = event.detail.value
  117. const context = uni.createCameraContext();
  118. context?.setZoom({
  119. zoom: value,
  120. success: (e : any) => {
  121. console.log(e);
  122. }
  123. } as CameraContextSetZoomOptions)
  124. },
  125. handleTakePhoto() {
  126. const context = uni.createCameraContext();
  127. context?.takePhoto({
  128. quality: this.quality,
  129. selfieMirror: false,
  130. success: (res : CameraContextTakePhotoResult) => {
  131. console.log("res.tempImagePath", res.tempImagePath);
  132. this.imageSrc = res.tempImagePath ?? ''
  133. },
  134. fail: (e : any) => {
  135. console.log("take photo", e);
  136. }
  137. } as CameraContextTakePhotoOptions)
  138. },
  139. takePhotoQualityChange(event : UniRadioGroupChangeEvent) {
  140. this.quality = event.detail.value
  141. console.log("quality", this.quality);
  142. },
  143. setOnFrameListener() {
  144. const context = uni.createCameraContext();
  145. this.listener = context?.onCameraFrame((frame : CameraContextOnCameraFrame) => {
  146. console.log("OnFrame :", frame);
  147. })
  148. },
  149. startFrameListener() {
  150. this.listener?.start({
  151. success: (res : any) => {
  152. console.log("startFrameListener success", res);
  153. }
  154. } as CameraContextCameraFrameListenerStartOptions)
  155. },
  156. stopFrameListener() {
  157. this.listener?.stop({
  158. success: (res : any) => {
  159. console.log("stopFrameListener success", res);
  160. }
  161. } as CameraContextCameraFrameListenerStopOptions)
  162. },
  163. startRecord() {
  164. const context = uni.createCameraContext();
  165. let timeout = this.getTimeout()
  166. this.timeout = timeout
  167. context?.startRecord({
  168. timeout: timeout,
  169. selfieMirror: false,
  170. timeoutCallback: (res : any) => {
  171. console.log("timeoutCallback", res);
  172. this.startRecordStatus = false
  173. if (typeof res != "string") {
  174. const result = res as CameraContextStartRecordTimeoutResult
  175. this.videoSrc = result.tempVideoPath ?? ''
  176. }
  177. clearInterval(this.intervalId)
  178. },
  179. success: (res : any) => {
  180. this.startRecordStatus = true
  181. console.log("start record success", res);
  182. this.remain = timeout
  183. this.intervalId = setInterval(() => {
  184. if (this.remain <= 0) {
  185. clearInterval(this.intervalId)
  186. } else {
  187. this.remain--
  188. }
  189. }, 1000)
  190. },
  191. fail: (res : any) => {
  192. console.log("start record fail", res);
  193. this.startRecordStatus = false
  194. this.remain = 0
  195. clearInterval(this.intervalId)
  196. }
  197. } as CameraContextStartRecordOptions)
  198. },
  199. stopRecord() {
  200. this.startRecordStatus = false
  201. const context = uni.createCameraContext();
  202. context?.stopRecord({
  203. compressed: this.compressed,
  204. success: (res : CameraContextStopRecordResult) => {
  205. console.log("stop record success", res);
  206. this.videoSrc = res.tempVideoPath ?? ''
  207. },
  208. fail: (res : any) => {
  209. console.log("stop record fail", res);
  210. }
  211. } as CameraContextStopRecordOptions)
  212. clearInterval(this.intervalId)
  213. this.remain = 0
  214. },
  215. startRecordCompressChange(event : UniRadioGroupChangeEvent) {
  216. this.compressed = parseInt(event.detail.value) == 1
  217. },
  218. timeoutInput(event : UniInputEvent) {
  219. this.timeoutStr = event.detail.value
  220. },
  221. getTimeout() : number {
  222. let value = parseInt(this.timeoutStr)
  223. if (isNaN(value)) {
  224. return 30
  225. } else {
  226. if (value < 1) {
  227. return 1
  228. } else if (value > 60 * 5) {
  229. return 60 * 5
  230. } else {
  231. return value
  232. }
  233. }
  234. }
  235. }
  236. }
  237. </script>
  238. <style scope>
  239. /* 基础容器 */
  240. .camera-container {
  241. width: 100%;
  242. height: 100%;
  243. position: relative;
  244. overflow: hidden;
  245. background-color: #000;
  246. }
  247. /* 相机预览 */
  248. .camera-preview {
  249. width: 100%;
  250. height: 100%;
  251. position: absolute;
  252. top: 0;
  253. left: 0;
  254. }
  255. /* 左上角:闪光灯按钮 */
  256. .top-left {
  257. position: absolute;
  258. top: 40rpx;
  259. left: 40rpx;
  260. z-index: 10;
  261. }
  262. .flash-btn {
  263. width: 100rpx;
  264. height: 100rpx;
  265. border-radius: 100rpx;
  266. background-color: rgba(0, 0, 0, 0.4);
  267. display: flex;
  268. justify-content: center;
  269. align-items: center;
  270. padding: 0;
  271. border: none;
  272. }
  273. .icon {
  274. font-size: 48rpx;
  275. color: #e0e0e0;
  276. /* 银色 */
  277. }
  278. /* 右上角:图像质量设置 */
  279. .top-right {
  280. position: absolute;
  281. top: 40rpx;
  282. right: 40rpx;
  283. z-index: 10;
  284. background-color: rgba(0, 0, 0, 0.4);
  285. padding: 20rpx 30rpx;
  286. border-radius: 16rpx;
  287. }
  288. /* 底部中间:拍照按钮 */
  289. .bottom-center {
  290. position: absolute;
  291. bottom: 240rpx;
  292. left: 50%;
  293. transform: translateX(-50%);
  294. z-index: 10;
  295. }
  296. .shoot-btn {
  297. width: 180rpx;
  298. height: 180rpx;
  299. border-radius: 180rpx;
  300. background-color: #ffffff;
  301. display: flex;
  302. justify-content: center;
  303. align-items: center;
  304. padding: 0;
  305. border: 8rpx solid rgba(255, 255, 255, 0.3);
  306. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0);
  307. }
  308. .shoot-inner {
  309. width: 140rpx;
  310. height: 140rpx;
  311. border-radius: 140rpx;
  312. background-color: #f0f0f0;
  313. }
  314. /* 左下角:相册预览 */
  315. .bottom-left {
  316. position: absolute;
  317. bottom: 240rpx;
  318. left: 40rpx;
  319. z-index: 10;
  320. }
  321. .album-preview {
  322. width: 130rpx;
  323. height: 130rpx;
  324. border: 4rpx solid #e0e0e0;
  325. border-radius: 16rpx;
  326. overflow: hidden;
  327. display: flex;
  328. justify-content: center;
  329. align-items: center;
  330. background-color: rgba(255, 255, 255, 0.1);
  331. }
  332. .preview-img {
  333. width: 100%;
  334. height: 100%;
  335. object-fit: cover;
  336. }
  337. .preview-tip {
  338. color: #e0e0e0;
  339. /* 银色 */
  340. font-size: 26rpx;
  341. }
  342. /* 下方:缩放控制 */
  343. .zoom-control {
  344. position: absolute;
  345. bottom: 40rpx;
  346. left: 0;
  347. width: 100%;
  348. padding: 0 40rpx;
  349. box-sizing: border-box;
  350. z-index: 10;
  351. }
  352. .setting-label {
  353. display: block;
  354. color: #e0e0e0;
  355. /* 银色 */
  356. font-size: 28rpx;
  357. margin-bottom: 16rpx;
  358. font-weight: 500;
  359. }
  360. /* 质量选择样式 */
  361. .quality-group {
  362. display: flex;
  363. gap: 10;
  364. }
  365. .quality-radio {
  366. color: #e0e0e0;
  367. /* 银色 */
  368. font-size: 26rpx;
  369. display: flex;
  370. align-items: center;
  371. gap: 10;
  372. }
  373. /* 缩放控制样式 */
  374. .zoom-container {
  375. width: 100%;
  376. padding: 10rpx 0;
  377. }
  378. .zoom-slider {
  379. width: 100%;
  380. height: 4px;
  381. }
  382. </style>