引入原生相机
import { Camera ,CameraOptions } from '@ionic-native/camera';
点击添加照片 触发 showPhotoRadio事件:
showPhotoRadio() { let alert = this.alertCtrl.create(); alert.setTitle(''); alert.addInput({ type: 'radio', label: '来自本地', value: 'local', checked: true }); alert.addInput({ type: 'radio', label: '来自相机', value: 'camera', checked: false }); alert.addButton('Cancel'); alert.addButton({ text: 'OK', handler: data => { this.photoRadioOpen = false; if (data == "local") { this.getPhotoByLocal(); } else { this.takePhoto(); } } }); if(this.writeAble ==true){ alert.present(); } }
本地获取照片:
getPhotoByLocal() { const options: CameraOptions = { quality: 20, targetHeight: 500, targetWidth: 500, destinationType: this.camera.DestinationType.FILE_URI, // 默认返回图片路径:DATA_URL:0,base64字符串,FILE_URI:1,图片路径 sourceType: this.camera.PictureSourceType.PHOTOLIBRARY, allowEdit: false, correctOrientation: true, encodingType: this.camera.EncodingType.JPEG, saveToPhotoAlbum: false, mediaType: this.camera.MediaType.PICTURE, cameraDirection: 0, } this.camera.getPicture(options).then((imageData) => { console.log('一开始的imageData', imageData); let filename = imageData.substring(imageData.lastIndexOf('/') + 1); console.log('imageData.includes("?")', imageData.includes("?")); if (imageData.includes("?")) { console.log('编辑前的filename', filename); filename = filename.substring(0, filename.lastIndexOf('?')); console.log('编辑后的filename', filename); } let path = imageData.substring(0, imageData.lastIndexOf('/') + 1); this.imgURL.push({ filename, path, }); //用 file.readAsDataURL 将图片转为 base64 便于页面显示 this.file.readAsDataURL(path, filename).then(res => this.imgData.push(res));this.file.readAsDataURL(path, filename).then(res => console.log('方法返回64的res+++++', res) );this.outImg.emit( this.imgData); //将数据传递给父组件 this.outUri.emit( this.imgURL); //将数据传递给父组件 }) }
照片来自相机
takePhoto() { const options: CameraOptions = { quality: 20, //是否对照片编辑 allowEdit: false, //sourceType 为 this.camera.PictureSourceType.CAMERA 时调用照相机 sourceType: this.camera.PictureSourceType.CAMERA, saveToPhotoAlbum: true, correctOrientation: true, mediaType: this.camera.MediaType.PICTURE, encodingType: this.camera.EncodingType.JPEG, //FILE_URI 获取Android的图片位置,DATA_URL获取 base64 编码,NATIVE_URI获取 IOS 系统的图片位置 destinationType: this.camera.DestinationType.FILE_URI } this.camera.getPicture(options).then((imageData) => { let filename = imageData.substring(imageData.lastIndexOf('/') + 1); let path = imageData.substring(0, imageData.lastIndexOf('/') + 1); this.imgURL.push({ filename, path, }); //用 file.readAsDataURL 将图片转为 base64 便于页面显示 this.file.readAsDataURL(path, filename).then(res => this.imgData.push(res)); this.outImg.emit( this.imgData); //将数据传递给父组件 this.outUri.emit( this.imgURL); //将数据传递给父组件 }) }
 
                    
                     
                    
                 
                    
                 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号