Camera.uvue 9.0 KB

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