companyvideo.nvue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. <<template>
  2. <view class="u-page">
  3. <KJ-Camera ref='Camera' class="KJ-Camera" :style="{width:'100vw',height:cameraHeight}">
  4. </KJ-Camera>
  5. <view class="view-content" :style="contentStyle">
  6. <!-- <image :showLoading="true" src="@/static/video-bgc.png" @click="click" class="img-bgc"
  7. :style="contentStyle" mode='widthFix'></image> -->
  8. <!-- <image :showLoading="true" src="@/static/video-bgc.png" @click="click" class="img-bgc"
  9. :style="contentStyle"></image> -->
  10. <view class="content" :style="{width:width}">
  11. <view class="title">
  12. 请阅读以下文字
  13. </view>
  14. <view class="text">
  15. 我已同意段时间放假啦硕大的房间里方法是打开立方爱睡懒觉发的房间拉法史莱克激发熟练度拉收费记录卡士大夫积分代暗室逢灯金坷垃似懂非懂接口了
  16. </view>
  17. </view>
  18. <view class="btn-view" :style="{width:width}">
  19. <u-button @click="startVideoCapture" class="btn" v-if='!isAlreadyRecord' type="primary" text="开始录像"></u-button>
  20. <u-button @click="stopVideoCapture" class="btn" v-if='isAlreadyRecord' text="停止录像" type="primary"></u-button>
  21. <!-- <u-button @click="saveVideo" class="btn" v-if='isSaveVideo' type="primary" text="保存视频"></u-button> -->
  22. <u-button :disabled="changeCamera" @click="switchCamera" class="btn" type="primary" text="切换摄像头"></u-button>
  23. <!-- <button @click="setZoomFactor" class="btn" type="primary">
  24. 设置焦距
  25. </button> -->
  26. </view>
  27. <!-- <button @click="captureImage" class="btn" type="primary">
  28. 拍照
  29. </button>
  30. <button @click="startVideoCapture" class="btn" type="primary">
  31. 开始录像
  32. </button>
  33. <button @click="stopVideoCapture" class="btn" type="primary">
  34. 停止录像
  35. </button>
  36. <button @click="switchCamera" class="btn" type="primary">
  37. 前后摄像头切换
  38. </button>
  39. <button @click="setZoomFactor" class="btn" type="primary">
  40. 设置焦距
  41. </button>
  42. <button @click="setFlash" class="btn" type="primary">
  43. 设置闪光灯模式
  44. </button>
  45. <button @click="setTorch" class="btn" type="primary">
  46. 设置手电筒
  47. </button>
  48. <button @click="ios_getSessionPresets" class="btn" type="primary">
  49. ios获取支持的分辨率
  50. </button>
  51. <button @click="ios_setSessionPreset" class="btn" type="primary">
  52. ios设置分辨率
  53. </button>
  54. <button @click="android_getSupportedPictureSizes" class="btn" type="primary">
  55. android获取照片支持分辨率列表
  56. </button>
  57. <button @click="android_getSupportedPreviewSizes" class="btn" type="primary">
  58. android获取预览支持分辨率列表
  59. </button>
  60. <button @click="android_getSupportedVideoSizes" class="btn" type="primary">
  61. android获取视频支持分辨率列表
  62. </button>
  63. <button @click="android_setCameraParams" class="btn" type="primary">
  64. android设置相机参数
  65. </button>
  66. <button @click="setRotation" class="btn" type="primary">
  67. android设置旋转角度
  68. </button>
  69. <button @click="initCamera" class="btn" type="primary">
  70. 初始化相机
  71. </button>
  72. <image :src="src" style="width: 50px;height: 50px;"></image> -->
  73. </view>
  74. </view>
  75. </template>
  76. <script>
  77. import uploadImage from '@/components/ossutil/uploadFile.js';
  78. import permision from "@/js_sdk/wa-permission/permission.js"
  79. var videoFilePath;
  80. export default {
  81. data() {
  82. return {
  83. isSaveVideo: false,
  84. isAlreadyRecord: false,
  85. changeCamera:false,
  86. cameraWidth: "100vw",
  87. cameraHeight: "100vh",
  88. src: '',
  89. contentStyle: {
  90. width: 0,
  91. height: 'auto',
  92. },
  93. width: 0,
  94. height: 0,
  95. videoSrc: ''
  96. }
  97. },
  98. onLoad() {
  99. this.get_camera_permission()
  100. },
  101. onShow() {
  102. uni.getSystemInfo({
  103. success: (res) => {
  104. console.log(res)
  105. this.width = res.windowWidth
  106. this.height = res.windowHeight
  107. this.contentStyle = {
  108. width: res.windowWidth,
  109. // height: res.windowHeight,
  110. }
  111. }
  112. })
  113. },
  114. async onReady() {
  115. console.log("onReady")
  116. await this.$refs.Camera.initCamera();
  117. await this.$refs.Camera.setZoomFactor(5)
  118. await this.$refs.Camera.switchCamera();
  119. await this.android_setCameraParams(); //因为andorid前后摄像头,设置相机参数不一样,需要重置
  120. },
  121. methods: {
  122. async get_camera_permission() {
  123. var photol=await permision.requestAndroidPermission("android.permission.RECORD_AUDIO")
  124. if(photol == false){
  125. uni.showModal({
  126. title: '提示',
  127. content: '您未开启音频权限,去设置',
  128. success: function (res) {
  129. if (res.confirm) {
  130. permision.gotoAppPermissionSetting()
  131. // plus.runtime.openURL("app-settings:");
  132. } else if (res.cancel) {
  133. console.log('用户点击取消');
  134. }
  135. }
  136. });
  137. }
  138. },
  139. saveVideo() {
  140. uni.$u.route('/pages/mine/company/addcompany');
  141. },
  142. /**
  143. * 初始化相机 默认加载插件的时候已初始化
  144. * 在横竖屏切换的时候,需要重新初始化,要不然画面是颠倒的
  145. * */
  146. // async initCamera() {
  147. // let Camera = await this.$refs.Camera.initCamera();
  148. // console.log('Camera',Camera)
  149. // // setTimeout(() => {
  150. // // this.switchCamera()
  151. // // this.setZoomFactor()
  152. // // }), 1000
  153. // },
  154. /**
  155. * 拍照
  156. * */
  157. captureImage() {
  158. var _this = this;
  159. var dic = {
  160. "savePath": plus.io.convertLocalFileSystemURL("_doc/KJ-Camera"), //保存图片位置,一定要是_doc绝对路径
  161. "format": "jpeg", //保存的图片格式 有效值:png、jpeg
  162. "quality": 80 //andorid才可用 保存的图片质量,取值1-100
  163. }
  164. this.$refs.Camera.captureImage(dic, (res) => {
  165. console.log("filePath: " + res)
  166. //_this.src = "file://" + res.filePath;
  167. uni.saveImageToPhotosAlbum({
  168. filePath: plus.io.convertAbsoluteFileSystem(res.filePath),
  169. success: function() {
  170. console.log('save success');
  171. },
  172. fail: function(e) {
  173. console.log(JSON.stringify(e));
  174. }
  175. });
  176. }, (res) => {
  177. _this.src = "data:image/png;base64," + res.base64;
  178. console.log("base64: " + res)
  179. });
  180. },
  181. /**
  182. * 开始录像
  183. * */
  184. startVideoCapture() {
  185. var _this = this;
  186. var dic = {
  187. "savePath": plus.io.convertLocalFileSystemURL("_doc/KJ-Camera"), //保存视频位置,一定要是_doc绝对路径
  188. "isAddAudio": true, //是否添加音频,默认是true
  189. "videoEncoder": 2, //andorid有效,DEFAULT = 0;H263 = 1;H264 = 2;MPEG_4_SP = 3;VP8 = 4;HEVC = 5;
  190. "audioEncoder": 3, //andorid有效,DEFAULT = 0;AMR_NB = 1;AMR_WB = 2;AAC = 3;HE_AAC = 4;AAC_ELD = 5;VORBIS = 6;
  191. }
  192. this.$refs.Camera.startVideoCapture(dic, (res) => {
  193. console.log("startVideoCapture: " + JSON.stringify(res))
  194. if (res.status == "start") {
  195. this.changeCamera=true
  196. console.log("开始录像")
  197. setTimeout(()=>{
  198. this.isAlreadyRecord = true
  199. },1000)
  200. } else if (res.status == "end") {
  201. console.log("结束录像")
  202. uni.showLoading({
  203. title: '压缩中...',
  204. mask:true
  205. });
  206. var that = this;
  207. uni.compressVideo({
  208. src: res.filePath,
  209. quality: 'low', //'low':低,'medium':中,'high':高
  210. success: function (res){
  211. console.log('压缩后',res)
  212. videoFilePath = res.tempFilePath;
  213. uni.showLoading({
  214. title:"保存视频中"
  215. })
  216. uploadImage('mp4', videoFilePath, 'appData/',
  217. result => {
  218. uni.hideLoading()
  219. // 上传成功回调函数
  220. console.log('视频地址', result)
  221. uni.setStorageSync("videoSrc",result)
  222. uni.navigateBack({
  223. delta:1
  224. })
  225. // uni.$u.route('/pages/mine/company/addcompany', {
  226. // videoSrc: result,
  227. // });
  228. }
  229. )
  230. },
  231. fail: function (err) {
  232. uni.showToast({
  233. title:'视频压缩失败',
  234. icon:'none'
  235. },2000)
  236. }
  237. })
  238. // uni.saveVideoToPhotosAlbum({
  239. // filePath: plus.io.convertAbsoluteFileSystem(videoFilePath),
  240. // success: function(e) {
  241. // console.log('save success');
  242. // console.log(e);
  243. // }
  244. // });
  245. }
  246. });
  247. },
  248. /**
  249. * 停止录像
  250. * */
  251. stopVideoCapture() {
  252. this.$refs.Camera.stopVideoCapture();
  253. this.isSaveVideo = true
  254. },
  255. /**
  256. * 前后摄像头切换
  257. * */
  258. switchCamera() {
  259. if(!this.changeCamera){
  260. this.$refs.Camera.switchCamera();
  261. this.android_setCameraParams(); //因为andorid前后摄像头,设置相机参数不一样,需要重置
  262. }
  263. },
  264. /**
  265. * 设置焦距
  266. * */
  267. setZoomFactor() {
  268. this.$refs.Camera.getZoomFactor((res_zoom) => {
  269. console.log("获取当前焦距:" + res_zoom.value);
  270. this.$refs.Camera.getMaxZoomFactor((res_max) => {
  271. console.log("可设置的最大焦距:" + res_max.value);
  272. this.$refs.Camera.getMinZoomFactor((res_min) => {
  273. console.log("可设置的最小焦距:" + res_min.value);
  274. this.$refs.Camera.setZoomFactor(1);
  275. });
  276. });
  277. });
  278. },
  279. /**
  280. * 设置闪光灯模式 默认是auto
  281. * auto-自动
  282. * on-开
  283. * off-关
  284. * */
  285. setFlash() {
  286. this.$refs.Camera.setFlash("off");
  287. },
  288. /**
  289. * 设置手电筒 默认是auto
  290. * auto-自动
  291. * on-开
  292. * off-关
  293. * */
  294. setTorch() {
  295. this.$refs.Camera.setTorch("on");
  296. },
  297. /**
  298. * ios获取支持的分辨率
  299. * */
  300. ios_getSessionPresets() {
  301. this.$refs.Camera.ios_getSessionPresets((res) => {
  302. console.log("ios_getSessionPresets:" + JSON.stringify(res));
  303. });
  304. },
  305. /**
  306. * ios设置分辨率 默认是high
  307. * photo-适用于高分辨率照片质量输出
  308. * high-适用于高质量视频和音频输出
  309. * medium-适用于中等质量输出
  310. * low-适用于低质量输出
  311. * 其它。。。
  312. * */
  313. ios_setSessionPreset() {
  314. this.$refs.Camera.ios_setSessionPreset("low"); //从ios_getSessionPresets方法获取
  315. },
  316. /**
  317. * android获取照片支持分辨率列表
  318. * 只用于设置照片分辨率 或 视频分辨率
  319. * */
  320. android_getSupportedPictureSizes() {
  321. this.$refs.Camera.android_getSupportedPictureSizes((res) => {
  322. console.log("android_getSupportedPictureSizes:" + JSON.stringify(res));
  323. });
  324. },
  325. /**
  326. * android获取预览支持分辨率列表
  327. * 只用于设置预览分辨率
  328. * */
  329. android_getSupportedPreviewSizes() {
  330. this.$refs.Camera.android_getSupportedPreviewSizes((res) => {
  331. console.log("android_getSupportedPreviewSizes:" + JSON.stringify(res));
  332. });
  333. },
  334. /**
  335. * android获取视频支持分辨率列表
  336. * 只用于视频分辨率
  337. * */
  338. android_getSupportedVideoSizes() {
  339. this.$refs.Camera.android_getSupportedVideoSizes((res) => {
  340. console.log("android_getSupportedVideoSizes:" + JSON.stringify(res));
  341. });
  342. },
  343. /**
  344. * android设置旋转角度
  345. * 值参考:0,90,180,270 为null,则为自动设置
  346. * */
  347. setRotation() {
  348. this.$refs.Camera.setRotation(null);
  349. },
  350. /**
  351. * android设置相机参数
  352. * 照片和预览分辨率等于0,默认使用从支持分辨率列表中选取合适的分辨率
  353. * 其它等于0的话,不设置这个参数
  354. * */
  355. android_setCameraParams() {
  356. uni.showLoading({
  357. title: '加载中',
  358. mask: true
  359. })
  360. const res = uni.getSystemInfoSync();
  361. var screenBiLi = res.screenWidth / res.screenHeight
  362. screenBiLi = Math.floor(screenBiLi * 10) / 10;
  363. console.log("screenBiLi:" + screenBiLi)
  364. //设置最大分辨率
  365. this.$refs.Camera.android_getSupportedPictureSizes((PictureSizes) => {
  366. this.$refs.Camera.android_getSupportedPreviewSizes((PreviewSizes) => {
  367. this.$refs.Camera.android_getSupportedVideoSizes((VideoSizes) => {
  368. var maxPictureSizesIndex = 0;
  369. var maxPictureSizeWidthHeight = 0;
  370. for (var i = 0; i < PictureSizes.length; i++) {
  371. var item = PictureSizes[i];
  372. var PictureSizeBiLi = item.height / item.width;
  373. PictureSizeBiLi = Math.floor(PictureSizeBiLi * 10) / 10;
  374. console.log("PictureSizeBiLi:" + PictureSizeBiLi)
  375. if (PictureSizeBiLi == screenBiLi) {
  376. if (maxPictureSizeWidthHeight < item.width * item.height) {
  377. maxPictureSizeWidthHeight = item.width * item.height;
  378. maxPictureSizesIndex = i;
  379. }
  380. }
  381. }
  382. var maxPreviewSizesIndex = 0;
  383. var maxPreviewSizeWidthHeight = 0;
  384. for (var i = 0; i < PreviewSizes.length; i++) {
  385. var item = PreviewSizes[i];
  386. var PreviewBiLi = item.height / item.width;
  387. PreviewBiLi = Math.floor(PreviewBiLi * 10) / 10;
  388. console.log("PreviewBiLi:" + PreviewBiLi)
  389. if (PreviewBiLi == screenBiLi) {
  390. if (maxPreviewSizeWidthHeight < item.width * item.height) {
  391. maxPreviewSizeWidthHeight = item.width * item.height;
  392. maxPreviewSizesIndex = i;
  393. }
  394. }
  395. }
  396. var maxVideoSizesIndex = 0;
  397. var maxVideoSizesWidthHeight = 0;
  398. for (var i = 0; i < VideoSizes.length; i++) {
  399. var item = VideoSizes[i];
  400. // var PreviewBiLi = item.height / item.width;
  401. // PreviewBiLi = Math.floor(PreviewBiLi * 10) / 10;
  402. console.log("PreviewBiLi:" + PreviewBiLi)
  403. //if (PreviewBiLi == screenBiLi) {
  404. if (maxVideoSizesWidthHeight < item.width * item.height) {
  405. maxVideoSizesWidthHeight = item.width * item.height;
  406. maxVideoSizesIndex = i;
  407. }
  408. //}
  409. }
  410. var dic = {
  411. picSizeWidth: PictureSizes[maxPictureSizesIndex]
  412. .width, //照片分辨率宽 从android_getSupportedPictureSizes方法获取
  413. picSizeHeight: PictureSizes[maxPictureSizesIndex]
  414. .height, //照片分辨率高 从android_getSupportedPictureSizes方法获取
  415. jpegQuality: 100, //照片质量 取值1-100
  416. preSizeWidth: PreviewSizes[maxPreviewSizesIndex]
  417. .width, //预览分辨率宽 从android_getSupportedPreviewSizes方法获取
  418. preSizeHeight: PreviewSizes[maxPreviewSizesIndex]
  419. .height, //预览分辨率高 从android_getSupportedPreviewSizes方法获取
  420. videoSizeWidth: VideoSizes[maxVideoSizesIndex]
  421. .width, //视频分辨率宽 从android_getSupportedVideoSizes方法获取
  422. videoSizeHeight: VideoSizes[maxVideoSizesIndex]
  423. .height, //视频分辨率高 从android_getSupportedVideoSizes方法获取
  424. videoEncodingBitRate: 5 * VideoSizes[maxVideoSizesIndex]
  425. .width * VideoSizes[maxVideoSizesIndex]
  426. .height, //视频编码比特率,如果设置了视频分辨率,这个一定要设置,否则视频不清晰
  427. }
  428. this.$refs.Camera.android_setCameraParams(dic);
  429. setTimeout(()=>{
  430. uni.hideLoading()
  431. },1000)
  432. console.log(JSON.stringify(dic));
  433. });
  434. });
  435. });
  436. }
  437. }
  438. }
  439. </script>
  440. <style lang="scss">
  441. .KJ-Camera {
  442. width: 100vw;
  443. position: fixed;
  444. top: 0px;
  445. bottom: 0px;
  446. left: 0;
  447. right: 0;
  448. }
  449. .view-content {
  450. position: fixed;
  451. top: 0;
  452. }
  453. .img-bgc {
  454. position: fixed;
  455. top: -50px;
  456. }
  457. .content {
  458. position: fixed;
  459. bottom: 200rpx;
  460. // top: 800rpx;
  461. // background: red;
  462. background-color: rgba(169, 151, 151, 0.7);
  463. }
  464. .title {
  465. flex-direction: row;
  466. justify-content: center;
  467. padding: 5rpx;
  468. }
  469. .text {
  470. flex-direction: column;
  471. flex-wrap: wrap;
  472. justify-content: center;
  473. padding: 20rpx 10rpx 0rpx 10rpx;
  474. }
  475. .btn-view {
  476. position: fixed;
  477. bottom: 20rpx;
  478. flex-direction: row;
  479. justify-content: space-between;
  480. padding: 0 20rpx;
  481. }
  482. .btn{
  483. flex-direction: row;
  484. justify-content: center;
  485. width: 300rpx;
  486. height: 100rpx;
  487. align-items: center;
  488. background: #F5BA3C;
  489. color: #FFFFFF;
  490. }
  491. .u-button--disabled{
  492. background:#ccc;
  493. border:1px solid #ccc;
  494. }
  495. </style>