2016-11-05 84 views
1

我对Ionic 2,Cordova &手机相对较新。我遵循示例应用程序构建了一个简单的相机应用程序。它与其他几个问题中的代码非常相似。在我的android手机上运行,​​相机拍摄照片,但不显示图像。显示“takePicture fired”日志条目,但没有其他日志条目。有什么我失踪像权限问题?无法理解为什么我甚至没有看到日志条目。Ionic 2相机不能在手机上工作

相关代码:

的mypage.html

<button ion-button block color="primary" (click)="takePicture()">Take Picture</button> 
... 

<img [src]="base64Image" *ngIf="base64Image" /> 

mypage.ts

export class MyPage { 

    public base64Image: string; 

    constructor(public navCtrl: NavController) { 
} 

    takePicture() { 
    console.log("takePicture fired."); 
    Camera.getPicture({ 
     destinationType: Camera.DestinationType.DATA_URL, 
     sourceType: Camera.PictureSourceType.CAMERA, 
     encodingType: Camera.EncodingType.JPEG, 
     targetWidth: 1000, 
     targetHeight: 1000 
    }).then((imageData) => { 
     // imageData is a base64 encoded string 
     this.base64Image = "data:image/jpeg;base64," + imageData; 
     if (this.base64Image) { 
     console.log("base64Image = " + this.base64Image); 
     } 
     else { 
     console.log("base64Image = NULL"); 
     } 
    }, (err) => { 
     console.log("An error occurred."); 
     console.error(err); 
    }); 
    }); 

回答

0

第1步:在您的class.ts

import { Camera } from 'ionic-native'; 

步骤2:HTML

<img [src]="base64Image" *ngIf="base64Image" (click)="captureImage()"/> 

第3步:

captureImage(){ 

     Camera.getPicture({ 
       quality: 100, 
       saveToPhotoAlbum: true, 
       destinationType: Camera.DestinationType.FILE_URI, 
       sourceType: Camera.PictureSourceType.CAMERA, 
       allowEdit: true, 
       correctOrientation: false 
       }) 
       .then((result) => { 
        this.base64Image = result; 
        console.log('Image URI: ' + this.base64Image); 
       }, (error) => { 
       console.log("ERROR -> " + JSON.stringify(error)); 
      }); 
} 

它的正常工作......

干杯!