0


uniapp实现自定义相机

自定义相机

起因

由于最近用uniapp调用原生相机容易出现闪退问题,找了很多教程又是压缩图片又是优化代码,我表示并没有太大作用!!

于是开启了我的解决之路

  • 利用livePusher实现

实现自定义相机

拓展性挺强的,可以实现自定义水印、身份证拍摄、人像拍摄等
这里我简单实现一个相机功能主要用于解决闪退

  1. Tip:这里需要创建nvue文件哦~

创建

  1. camera.nvue
  1. <template><view class="pengke-camera":style="{ width: windowWidth, height: windowHeight }"><live-pusher
  2. id="livePusher"
  3. ref="livePusher"class="livePusher"
  4. mode="FHD"
  5. beauty="0"
  6. whiteness="0":aspect="aspect"
  7. min-bitrate="1000"
  8. audio-quality="16KHz"
  9. device-position="back":auto-focus="true":muted="true":enable-camera="true":enable-mic="false":zoom="false"
  10. @statechange="statechange":style="{ width: windowWidth, height: windowHeight }"></live-pusher><view class="menu"><!--底部菜单区域背景--><cover-image class="menu-mask" src="/static/live-camera/bar.png"></cover-image><!--返回键--><cover-image class="menu-back" @tap="back" src="/static/live-camera/back.png"></cover-image><!--快门键--><cover-image class="menu-snapshot" @tap="snapshot" src="/static/live-camera/shutter.png"></cover-image><!--反转键--><cover-image class="menu-flip" @tap="flip" src="/static/live-camera/flip.png"></cover-image></view></view></template><script>
  11. let _this = null;exportdefault{data(){return{
  12. poenCarmeInterval:null,//打开相机的轮询
  13. aspect:'2:3',//比例
  14. windowWidth:'',//屏幕可用宽度
  15. windowHeight:'',//屏幕可用高度
  16. camerastate:false,//相机准备好了
  17. livePusher: null,//流视频对象
  18. snapshotsrc: null,//快照};},onLoad(e){
  19. _this =this;this.initCamera();},onReady(){this.livePusher = uni.createLivePusherContext('livePusher',this);this.startPreview();//开启预览并设置摄像头this.poenCarme();},
  20. methods:{//轮询打开poenCarme(){//#ifdef APP-PLUSif(plus.os.name =='Android'){this.poenCarmeInterval =setInterval(function(){
  21. console.log(_this.camerastate);if(!_this.camerastate) _this.startPreview();},2500);}//#endif},//初始化相机initCamera(){
  22. uni.getSystemInfo({
  23. success:function(res){
  24. _this.windowWidth = res.windowWidth;
  25. _this.windowHeight = res.windowHeight;
  26. let zcs = _this.aliquot(_this.windowWidth,_this.windowHeight);
  27. _this.aspect =(_this.windowWidth/zcs)+':'+(_this.windowHeight/zcs);// console.log('画面比例:'+_this.aspect);}});},//整除数计算aliquot(x, y){if(x % y ==0)return y;returnthis.aliquot(y, x % y);},//开始预览startPreview(){this.livePusher.startPreview({
  28. success: a =>{
  29. console.log(a)}});},//停止预览stopPreview(){this.livePusher.stopPreview({
  30. success: a =>{
  31. _this.camerastate =false;}});},//状态statechange(e){//状态改变
  32. console.log(e);if(e.detail.code ==1007){
  33. _this.camerastate =true;}elseif(e.detail.code ==-1301){
  34. _this.camerastate =false;}},//返回back(){
  35. uni.navigateBack();},//抓拍snapshot(){//震动
  36. uni.vibrateShort({
  37. success:function(){
  38. console.log('success');}});//拍照this.livePusher.snapshot({
  39. success: e =>{
  40. _this.snapshotsrc = e.message.tempImagePath;
  41. _this.stopPreview();
  42. _this.setImage();
  43. uni.navigateBack();}});},//反转flip(){this.livePusher.switchCamera();},//设置setImage(){
  44. let pages =getCurrentPages();
  45. let prevPage = pages[pages.length -2];
  46. prevPage.$vm.setImage({ path: _this.snapshotsrc});}}};</script><style lang="less">.pengke-camera {
  47. justify-content: center;
  48. align-items: center;.menu {
  49. position: absolute;
  50. left:0;
  51. bottom:0;
  52. width:750rpx;
  53. height:180rpx;
  54. z-index:98;
  55. align-items: center;
  56. justify-content: center;.menu-mask {
  57. position: absolute;
  58. left:0;
  59. bottom:0;
  60. width:750rpx;
  61. height:180rpx;
  62. z-index:98;}.menu-back {
  63. position: absolute;
  64. left:30rpx;
  65. bottom:50rpx;
  66. width:80rpx;
  67. height:80rpx;
  68. z-index:99;
  69. align-items: center;
  70. justify-content: center;}.menu-snapshot {
  71. width:130rpx;
  72. height:130rpx;
  73. z-index:99;}.menu-flip {
  74. position: absolute;
  75. right:30rpx;
  76. bottom:50rpx;
  77. width:80rpx;
  78. height:80rpx;
  79. z-index:99;
  80. align-items: center;
  81. justify-content: center;}}}</style>

这里用了一些图片作为图标布局画面美观,例如返回图标,拍摄图标

使用

在点击拍照的时候跳转到

  1. camera页面

即可
在需要使用的页面中编写

  1. setImage

方法,即可拿到返回过来的图片临时路径
再通过uniapp自带的上传图片api进行上传至服务器即可
这样就避免了调用原生相机

  1. setImage(e){//e.path即是图片临时路径
  2. uni.uploadFile({url:'上传接口的路径',filePath: e.path,name:'imageFile',success:function(res){//服务器返回的图片地址url},error:function(err){
  3. console.log(err)}}

效果图

在这里插入图片描述

拓展

如果既要实现从相册选又要手机拍呢?该如何实现
这里相册选调用的uniapp的api,
手机拍跳转到自定义相机页面即可

这里可以写一个弹窗,让它选择,如果选择了从相册选图片则

  1. uni.chooseImage({
  2. count: size,//默认9
  3. sizeType:['original','compressed'],//可以指定是原图还是压缩图,默认二者都有
  4. sourceType:['album'],//从相册选择
  5. success:function(res){
  6. console.log(res)//拿到临时路径再向后端发送上传请求....}});

如果用相机拍则跟上方步骤一致

实现多种自定义相机

这里的话我贴上效果图,如果需要就在我的博客资源中获取吧

水印相机

在这里插入图片描述

身份证相机

在这里插入图片描述

人像相机

在这里插入图片描述
这样我就成功解决了闪退问题~,有问题评论区d我


本文转载自: https://blog.csdn.net/m_xiaozhilei/article/details/129406183
版权归原作者 碰磕 所有, 如有侵权,请联系我们删除。

“uniapp实现自定义相机”的评论:

还没有评论