Camera.uvue 9.3 KB

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