Camera.uvue 8.9 KB

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