【问题标题】:ionic native camera taking picture but not showing it .i want to take picture and show it on the same page离子原生相机拍照但不显示。我想拍照并在同一页面上显示
【发布时间】:2019-06-04 00:37:24
【问题描述】:

我正在尝试 ion native camera api ,那是相机出现时拍摄的照片,我可以单击图像,但图像没有显示出来

我正在使用 android oreo 、ionic 3 和最新的 cordova 并在真实设备上运行应用程序

<-- file home.html-->`
<ion-header>
  <ion-navbar>
    <ion-title>
      Ionic Blank
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
    <button ion-button round (click)="eventofClick()">Round Button</button>

  <p><img src="{{image}}" /> </p> 

{{图片}}

</ion-content>
<--home.ts-- ------------------------------------------------------>

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Camera, CameraOptions } from '@ionic-native/camera';
@Component({
  selector: 'page-home',
  templateUrl: 'home.html',
})
export class HomePage {
  image: any;

  constructor(public navCtrl: NavController, public camera: Camera) {
    console.log("constructor");

  }
eventofClick() {
    console.log("called");
    console.log('inside');
    const options: CameraOptions = {
quality: 100,
destinationType: this.camera.DestinationType.FILE_URI,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE
}


     this.camera.getPicture(options).then((imageData) => {
   // imageData is either a base64 encoded string or a file URI
   // If it's base64 (DATA_URL):
   let base64Image = 'data:image/jpeg;base64,' + imageData;
  this.image = 'data:image/jpeg;base64,' + imageData;
  }, (err) => {
   // Handle error
  });
}
}

我想在拍完照片后一起展示 我得到的输出是

在控制台我得到1549019043619.jpg:1 GET unsafe:data:image/jpeg;base64,file:///storage/emulated/0/Android/data/com.catalyst.android.safra/cache/1549019043619.jpg 404 (Not Found)

【问题讨论】:

  • 你能得到任何错误或尝试控制台记录 ImageData 并检查你得到什么
  • @kevalnayak 我已经更新了问题以及控制台上的错误以及我得到的输出。

标签: android cordova ionic-framework ionic3


【解决方案1】:

试试看,这将返回您图像的 FILE_URI。如果您将图像上传到服务器,请尝试在上传后显示来自服务器的图像。或者您可以直接使用 base 64 显示它。对于 Base64,您必须将 CameraOptions 中的 destinationType: this.camera.DestinationType.FILE_URI 更改为 destinationType: this.camera.DestinationType.DATA_URL。 上传图像并显示它。

takePhoto(){
      const options: CameraOptions = {
              quality: 80,
              destinationType: this.camera.DestinationType.FILE_URI,
              encodingType: this.camera.EncodingType.JPEG,
              mediaType: this.camera.MediaType.PICTURE,
              sourceType: this.camera.PictureSourceType.PHOTOLIBRARY
            }
     this.camera.getPicture(options).then((imageData) => {
             // imageData is either a base64 encoded string or a file URI
             // If it's base64 (DATA_URL):
             console.log(imageData);
               this.imageUpload(imageData) / pass your URL in upload function
            }, (err) => {
             // Handle error
            });
    }


imageUpload(path) {
      const fileTransfer: FileTransferObject = this.transfer.create();
      let options: FileUploadOptions = {
          fileKey: 'image',
          fileName: '.png',
          chunkedMode: false,
          //mimeType: "image/jpeg",
        }
        fileTransfer.upload(path, 'Server_URL', options)
          .then((data) => {
          let res = JSON.parse(data.response);
          if (res.success == true) {
              //server will return you image name Push that name into an array and show it on your View
          }
        }, (err) => {
          console.log(err);
        });
    }

【讨论】:

  • 它给了我一些类似 content://com.android.providers.media.documents/document/image%3A4112 的路径,但我想在选择图像后显示它。跨度>
  • Ionic 不支持原生 URI 直接显示图像。您只想显示图像还是还想将图像上传到服务器?
  • 两者,如果你能指导我
  • 上传到服务器,然后在你的应用中展示。
  • 我已经通过上传代码更新了我的答案。使用这个插件ionicframework.com/docs/v3/native/file-transfer
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-17
  • 2013-11-08
  • 1970-01-01
相关资源
最近更新 更多