简体   繁体   English

Ionic 4 - 原生相机插件问题

[英]Ionic 4 - Native camera plugin issues

I have developed an android application using Ionic4. 我使用Ionic4开发了一个Android应用程序。 I am facing some issues with Ionic Native Camera plugin. 我正面临着Ionic Native Camera插件的一些问题。 The following is my code. 以下是我的代码。 The issues that i am facing is given below. 我面临的问题如下。 The version if camera plugin i am using is "@ionic-native/camera": "^5.3.0", . 我使用的相机插件的版本是"@ionic-native/camera": "^5.3.0",

Issues 问题

  1. Gallery is not opening 画廊没有开放
  2. Captured image is not returning. 捕获的图像没有返回。
  3. Application crashes after taking picture 拍照后应用程序崩溃

html HTML

<img [src]="studentImage!==null ? studentImage: 'assets/icon/ic_avatar.png'" class="add-picture" (click)="addImage()">

.ts .TS

 public addImage() {
    this.genericServices.presentActionSheet(this.openGallery, this.openCamera);
  }

private openCamera = () => {
    this.studentImage = this.genericServices.selectPicture('camera');
    console.log('Captured Image:=>' + this.studentImage);
  }
  private openGallery() {
    this.studentImage = this.genericServices.selectPicture('gallery');
  }

service 服务

  public async selectPicture(source) {
    let base64Image = null;
    const cameraOptions: CameraOptions = {
      quality: 75,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.PNG,
      mediaType: this.camera.MediaType.PICTURE,
      sourceType: source === 'camera' ? this.camera.PictureSourceType.CAMERA : this.camera.PictureSourceType.PHOTOLIBRARY,
      correctOrientation: true
    };

    await this.camera.getPicture(cameraOptions).then((imageData) => {
      console.log('Returned Image=>' + base64Image);
      return base64Image = 'data:image/jpeg;base64,' + imageData;
    }).catch(() => {
    });
  }

Hard to say what your problem is. 很难说你的问题是什么。 I would probably write the code slightly different, like this: 我可能会编写略有不同的代码,如下所示:

async selectImage() {

    const actionSheet = await this.actionCtrl.create({
        header: "Select Image source",
        buttons: [{
                text: 'Load from Library',
                handler: () => {
                  this.takePicture(this.camera.PictureSourceType.PHOTOLIBRARY);
                }
            },
            {
                text: 'Use Camera',
                handler: () => {
                    this.takePicture(this.camera.PictureSourceType.CAMERA);
                }
            },
            {
                text: 'Cancel',
                role: 'cancel'
            }
        ]
    });
    await actionSheet.present(); 
    }

And then in the takePicture() method decide what destinationType should be, default is FILE_URI (1). 然后在takePicture()方法中决定destinationType应该是什么,默认为FILE_URI (1)。

 takePicture(sourceType: PictureSourceType) {
    var options: CameraOptions = {
        quality: 80,
        sourceType: sourceType,
        saveToPhotoAlbum: false,
        correctOrientation: true,
        destinationType: 1,
        targetWidth: 1240,
        targetHeight: 768,
    };
    this.camera.getPicture(options)
    .then((imageData) => {
      // do something with the imageData, should be able to bind it to a variable and 
      // show it in your html file. You might need to fix file path, 
      // remember to import private win: any = window, and use it like this.

      this.imagePreview = this.win.Ionic.WebView.convertFileSrc(imageData);

    }).catch((err) => {
      console.warn("takePicture Error: " + err);
    });
  }

This should work fine... i just tested it. 这应该工作正常...我只是测试它。 But as i said, there could be several things wrong with your setup. 但正如我所说,你的设置可能有几个问题。 Hope it helps in one way or another... otherwise create a fiddle, and i will gladly look at the code for you. 希望它以某种方式帮助...否则创建一个小提琴,我很乐意为您查看代码。

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

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