gpt4 book ai didi

ionic-framework - IONIC-我无法转换 'image to base64'

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

我使用应用程序相机拍摄的照片显示为黑屏。我看了其他的问题,尝试了一些答案,但是我无法将图片翻译成base64。你能帮忙吗?

您可以从链接中看到完整的代码。 https://pastebin.ubuntu.com/p/4FwYdk5fvD/



takePicture(sourceType: PictureSourceType)
{
var options: CameraOptions = {
quality: 100,
sourceType: sourceType,
saveToPhotoAlbum: false,
correctOrientation: true
};

this.camera.getPicture(options).then(imagePath => {
if (this.plt.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY)
{
this.filePath.resolveNativePath(imagePath)
.then(filePath => {
let correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'));
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
});
}
else
{
var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
var correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
}
});

}

最佳答案

我有点遇到同样的问题。我还想要名称(虽然不是路径),我还想要一个 base 64...所以我做了什么:

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) => {
this.imgService.convertFilePathToBlob(imageData).subscribe(blob => {
this.image = blob;
});

let correctPath = imageData; // or subStr the parts you want
let currentName = this.image[1]; // grab the name
let currentImage = this.image[0]; // the image itself as base64


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

和转换函数...记得从 '@ionic-native/file/ngx' 导入 File, FileEntry

convertFilePathToBlob(filePath:string) {
return from(this.file.resolveLocalFilesystemUrl(filePath)).pipe(
mergeMap((fileEntry: FileEntry) => {
return Observable.create(observer => {
fileEntry.file(file => {
const reader = new FileReader();
reader.onloadend = () => {
const imgBlob = new Blob([reader.result], { type: file.type });
observer.next([imgBlob, file.name]); // the name can be grabbed now
observer.complete();
};
reader.readAsArrayBuffer(file);
}, error => {
observer.error(error);
});
});
}));}

这在我的情况下有效,但我发现我必须做所有这些是疯狂的,并且需要一些调试和谷歌搜索......我希望它以某种方式帮助你...... :)

关于ionic-framework - IONIC-我无法转换 'image to base64',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55554830/

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