• uniapp 实现拍照 相册选取选视频和照片


    在这里插入图片描述

    <view class="img-show-pick" @click="showActionSheet" v-if="isEdit">
    	<u-icon name="plus" color="#999999" size="28"></u-icon>
    </view>
    //选取图片的来源,拍照和相册  
    showActionSheet(){  
      let self = this
      let actionbuttons=[{title:"拍照"},{title:"相册选取"}];  
      let actionstyle={cancel:"取消",buttons:actionbuttons};  
      plus.nativeUI.actionSheet(actionstyle, function(e){  
    		if(e.index==1){  
    			self.getImage();  
    		}else if(e.index==2){  
    			self.handleUpload2();  
    		}  
      } );  
    },  
     // 拍照  
    	 getImage() {  
    		 let self = this
    		var cmr = plus.camera.getCamera();  
    		cmr.captureImage(function(p) {  
    			console.log('captureImage')
    			console.log(p)
    			plus.io.resolveLocalFileSystemURL(p, function(entry) { 
    				console.log('entry')
    				self.fileUploader(entry.toLocalURL())
    				//compressImage(entry.toLocalURL(),entry.name,divid);  
    			}, function(e) {  
    				plus.nativeUI.toast("读取拍照文件错误:" + e.message);  
    			}); 
    		}, function(e) {  
    		}, {  
    			filename: "_doc/camera/",  
    			index: 1  
    		});  
    	},  
    		 handleUpload2(){
    				// #ifdef APP-PLUS
    				//this.getImage()
    				 var lfs = null
    				 let self  = this
    				plus.gallery.pick((e) => {
    					for(var i in e.files){
    						lfs=e.files;
    						console.log(lfs[i])
    	            //根据拿到的图片地址上传到服务器
    						self.fileUploader(lfs[i])
    					}
    				}, function ( e ) {
    					console.log( "取消选择图片" );
    				},{filter:"none",multiple:true,selected:lfs,maximum:9,system:false});// 最多选择3张图片 ,这个配置也参见上面的详情
    				// #endif
    			},
    			fileUploader(filePath){
    				console.log('ddddd')
    				let url = this.$UploadUrl
    				let _self = this
    				_self.upload_cache_list = []
    				let task = plus.uploader.createUpload(url, 
    					  {
    					   method:"POST",
    					   headers: {
                            // 修改请求头Content-Type类型 此类型为文件上传
    						"Content-Type": "multipart/form-data"
    					   }
    					},
    					  ( t, status ) => {
    						// 上传完成
    						if(status == 200){
    						  let responseTextJson = JSON.parse(t.responseText);
    						  console.log('responseTextJson')
    						  console.log(responseTextJson)
    						  if(responseTextJson){
    							  _self.upload_cache_list.push(responseTextJson);
    							  _self.uploadList.push(responseTextJson)
    							  //_self.$emit("update:uploadList", arr);
    							  _self.$emit("uploadHandle", _self.uploadList);
    							  console.log(responseTextJson,'请求成功返回的信息')
    						  }else{
    						   console.log("服务错误,请稍后在试!")
    						  }
    						}else{
    						  console.log(JSON.stringify(t))
    						  console.log("网络错误,请稍后在试!")
    						}
    					  }
    					);
    		       //添加上传文件
    		       // filePath 上传文件的路径(仅支持本地文件路径)详情见 http://www.html5plus.org/doc/zh_cn/uploader.html#plus.uploader.Upload.addFile
    					task.addFile(filePath, {key: "file"} );
    					// 开始上传
    					console.log('end')
    					task.start();
    			},
    
        //压缩图片  
            function compressImage(url,filename,divid){  
                var name="_doc/upload/"+divid+"-"+filename;//_doc/upload/F_ZDDZZ-1467602809090.jpg  
                plus.zip.compressImage({  
                        src:url,//src: (String 类型 )压缩转换原始图片的路径  
                        dst:name,//压缩转换目标图片的路径  
                        quality:20,//quality: (Number 类型 )压缩图片的质量.取值范围为1-100  
                        overwrite:true//overwrite: (Boolean 类型 )覆盖生成新文件  
                    },  
                    function(event) {   
                        //uploadf(event.target,divid);  
                        var path = name;//压缩转换目标图片的路径  
                        //event.target获取压缩转换后的图片url路  
                        //filename图片名称  
                        saveimage(event.target,divid,filename,path);  
                    },function(error) {  
                        plus.nativeUI.toast("压缩图片失败,请稍候再试");  
                });  
            }  
    
    如果需要视频拍摄  使用插件
    https://ext.dcloud.net.cn/plugin?id=453
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
  • 相关阅读:
    如何为 Kubernetes 监控安装 Pixie
    Android Audio Driver基础
    修改设备网络DNS
    C++左右值及引用
    #经典论文 异质山坡的物理模型 2 有效导水率
    这可能是最全面的TCP面试八股文了
    【微服务】微服务初步认识 - 微服务技术如何学习 · 认识微服务架构
    0基础学习VR全景平台篇第120篇:极坐标处理接缝 - PS教程
    工具-Obsidian生产力工具,安装第三方插件(GitHub)教程,以安装Syntax Highlight(代码高亮)为例
    HTML CSS 个人网页设计 WEB前端大作业代码
  • 原文地址:https://blog.csdn.net/dongheng123/article/details/125445440