myVideo.vue 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <view class="camera-container">
  3. <!-- camera组件,全屏显示 -->
  4. <camera class="camera" device-position="back" flash="off" @error="error" style="width: 200px;height: 200px"></camera>
  5. <!-- 自定义底部操作栏 -->
  6. <view class="controls">
  7. <button @tap="switchCamera">切换摄像头</button>
  8. <!-- 长按录制视频,点击拍照 -->
  9. <button @touchstart="startRecord" @touchend="stopRecord">按住录制</button>
  10. <button @tap="takePhoto">拍照</button>
  11. </view>
  12. <!-- 用于显示拍下的照片 -->
  13. <image v-if="photoPath" :src="photoPath" mode="widthFix"></image>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. data() {
  19. return {
  20. cameraContext: null,
  21. photoPath: '', // 拍照得到的图片路径
  22. isRecording: false
  23. };
  24. },
  25. onReady() {
  26. // 页面渲染完成后,创建相机上下文
  27. this.cameraContext = uni.createCameraContext();
  28. },
  29. methods: {
  30. // 拍照
  31. takePhoto() {
  32. this.cameraContext.takePhoto({
  33. quality: 'high',
  34. success: (res) => {
  35. this.photoPath = res.tempImagePath;
  36. console.log('拍照成功,照片路径:', this.photoPath);
  37. },
  38. fail: (err) => {
  39. console.error('拍照失败:', err);
  40. }
  41. });
  42. },
  43. // 开始录制视频
  44. startRecord() {
  45. this.isRecording = true;
  46. this.cameraContext.startRecord({
  47. success: () => {
  48. console.log('开始录制');
  49. }
  50. });
  51. },
  52. // 结束录制视频
  53. stopRecord() {
  54. if (!this.isRecording) return;
  55. this.isRecording = false;
  56. this.cameraContext.stopRecord({
  57. success: (res) => {
  58. console.log('录制完成,视频路径:', res.tempThumbPath); // 封面
  59. console.log('视频文件路径:', res.tempVideoPath);
  60. // res.tempVideoPath 是视频的临时文件路径
  61. }
  62. });
  63. },
  64. // 切换前后摄像头
  65. switchCamera() {
  66. // 需要通过变量动态改变camera组件的device-position属性
  67. // 这部分逻辑需要你在data中定义变量并切换'front'和'back'
  68. }
  69. }
  70. };
  71. </script>
  72. <style>
  73. .camera {
  74. width: 100%;
  75. height: 100vh; /* 全屏 */
  76. }
  77. .controls {
  78. position: fixed;
  79. bottom: 50px;
  80. width: 100%;
  81. display: flex;
  82. justify-content: space-around;
  83. }
  84. </style>