gpt4 book ai didi

ionic-framework - 无法显示来自 FILE URI、Ionic 框架的图像

转载 作者:行者123 更新时间:2023-12-02 04:28:09 25 4
gpt4 key购买 nike

我正在尝试显示从相机拍摄的图像并将其显示在 View 中。我在许多网站上搜索过这个答案,但没有任何效果。我尝试过 DomSanitizer、Base64 甚至 photo-library,但没有显示从它们返回的图像。

我的 home.ts 文件是

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Camera, CameraOptions } from '@ionic-native/camera';
import { DomSanitizer } from '@angular/platform-browser';

@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {

myPhoto:any;
image;
constructor(public navCtrl: NavController, private camera: Camera, public DomSanitizer: DomSanitizer) {

}

takePhoto(){
const options: CameraOptions = {
quality: 100,
targetHeight: 320,
targetWidth: 320,
destinationType: this.camera.DestinationType.FILE_URI,
sourceType: this.camera.PictureSourceType.CAMERA,
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):
this.myPhoto = 'data:image/jpeg;base64,' + imageData;
this.image = imageData;
console.log(this.myPhoto);
alert(this.myPhoto);
}, (err) => {
// Handle error
});
}
}

这是我的 home.html 代码
<ion-header>
<ion-navbar>
<ion-title>
Ionic Blank
</ion-title>
</ion-navbar>
</ion-header>

<ion-content padding>
<button ion-button (click)="takePhoto()" >Take Photo</button>

<!-- <img src="{{myPhoto}}"/> -->
<!-- <img src="{{image}}"/> -->
<!-- <img [src]="DomSanitizer.bypassSecurityTrustUrl(myPhoto)"/> -->
<!-- <img data-ng-src="{{myPhoto}}"/> -->
<img src="data:image/jpeg;base64,file:///storage/sdcard0/Android/data/io.ionic.starter/cache/1533211220154.jpg"/>
</ion-content>

这里注释的代码是我尝试过但没有成功。

最佳答案

我使用了文件插件及其方法 readAsDataURL。它对我来说很好。希望它会帮助那里的人:)

const options: CameraOptions = {
quality: 100,
allowEdit: true,
sourceType: this.camera.PictureSourceType.CAMERA ,
saveToPhotoAlbum: true,
correctOrientation: true,
encodingType: this.camera.EncodingType.JPEG,
destinationType: this.camera.DestinationType.FILE_URI
}

this.camera.getPicture(options).then((imageData) => {

//imageData will hold the full file path so we need to extract filename and path using file plugin
var filename = imageData.substring(imageData.lastIndexOf('/')+1);
var path = imageData.substring(0,imageData.lastIndexOf('/')+1);
//then use it in the readAsDataURL method of cordova file plugin
//this.file is declared in constructor file :File
this.file.readAsDataURL(path, filename).then(res=>{
item.pic = res;
});
}, (err) => {
alert(err);
});

link for ionic cordova file plugin

关于ionic-framework - 无法显示来自 FILE URI、Ionic 框架的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51706809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com