Camera.uvue 9.2 KB

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