简体   繁体   English

Ionic 2相机不在手机上工作

[英]Ionic 2 camera not working on phone

I am relatively new to Ionic 2, Cordova & mobile. 我是Ionic 2,Cordova和移动设备的新手。 I followed a sample app to build a simple camera app. 我按照示例应用程序构建了一个简单的相机应用程序。 It is very similar to the code in a couple of other questions. 它与其他几个问题中的代码非常相似。 Running on my android phone, the camera takes the picture but no image is displayed. 在我的Android手机上运行,​​相机拍摄照片但没有显示图像。 The "takePicture fired" log entry displays but no further log entries. “takePicture fired”日志条目显示但没有其他日志条目。 Is there something I am missing like a permission issue? 是否有像遗产许可一样的遗漏? Can't understand why I am not even seeing a log entry. 无法理解为什么我甚至没有看到日志条目。

Relevant code: 相关代码:

mypage.html 的mypage.html

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

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

mypage.ts 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);
    });
    });

Step 1: In your class.ts 第1步:在你的课堂上

import { Camera } from 'ionic-native';

Step 2: HTML 第2步:HTML

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

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

It's working fine... 它工作得很好......

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM