【问题标题】:Ionic 2 camera not working on phoneIonic 2 相机无法在手机上使用
【发布时间】:2016-11-05 16:39:44
【问题描述】:

我对 Ionic 2、Cordova 和移动设备比较陌生。我按照示例应用程序构建了一个简单的相机应用程序。它与其他几个问题中的代码非常相似。在我的安卓手机上运行,​​相机拍照但不显示图像。 “takePicture 已触发”日志条目显示,但没有其他日志条目。有什么我缺少的东西,比如权限问题吗?不明白为什么我什至没有看到日志条目。

相关代码:

mypage.html

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

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

我的页面.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);
    });
    });

【问题讨论】:

    标签: android cordova camera ionic2


    【解决方案1】:

    第 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));
                });
    }
    

    一切正常……

    干杯!

    【讨论】:

      猜你喜欢
      • 2022-11-24
      • 1970-01-01
      • 1970-01-01
      • 2019-01-12
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      • 2021-11-08
      • 2019-12-12
      相关资源
      最近更新 更多