gpt4 book ai didi

ionic-framework - 如何将相机图像转换为 ionic 5 中的 blob?

转载 作者:行者123 更新时间:2023-12-02 16:41:57 25 4
gpt4 key购买 nike

我想发布多部分表单数据,为此,我们可以这样做:

let formData = new FormData()
formData.append('myfile', 'your blob')

this.http.post(url, formData)

但我不知道如何将相机图像转换为 blob。我正在使用原生相机插件,这里是我的代码:

  cameraOptions: CameraOptions = {
quality: 20,
destinationType: this.camera.DestinationType.DATA_URL,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY
}

constructor(public camera: Camera){}

takePhoto() {
return new Promise(resolve => {
this.camera.getPicture(this.cameraOptions).then((imageData) => {
resolve(imageData);
}, (err) => {
resolve(err);
});
});
}

我为 blob 尝试了这段代码:

dataURLtoBlob(dataURL) {
debugger;
// convert base64/URLEncoded data component to raw binary data held in a string
let byteString: string;
if (dataURL.split(',')[0].indexOf('base64') >= 0) {
byteString = atob(dataURL.split(',')[1]);
} else {
byteString = unescape(dataURL.split(',')[1]);
}
// separate out the mime component
let mimeString = dataURL
.split(',')[0]
.split(':')[1]
.split(';')[0];

// write the bytes of the string to a typed array
let ia = new Uint8Array(byteString.length);
for (let i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}

let blobImg = new Blob([ia], { type: mimeString });
console.log(blobImg);
this.blobImage = blobImg;
}

使用这段代码,我能够获取图像数据,但如何在 blob 中进行转换,请帮助...

你好@sergey-rudenko 这是我的输出

获取图片输出:

imageDataURI:  content://com.android.providers.media.documents/document/image%3A78702

this.file.resolveLocalFilesystemUrl 输出:

entry:  

FileEntry {isFile: true, isDirectory: false, name: "image:78702", fullPath: "/com.android.providers.media.documents/document/image:78702", filesystem: FileSystem, …}
filesystem: FileSystem {name: "content", root: DirectoryEntry}
fullPath: "/com.android.providers.media.documents/document/image:78702"
isDirectory: false
isFile: true
name: "image:78702"
nativeURL: "content://com.android.providers.media.documents/document/image%3A78702"
__proto__: Entry

entry.file 输出:

file:  
File {name: "content", localURL: "cdvfile://localhost/content/com.android.providers.media.documents/document/image%3A78702", type: "image/jpeg", lastModified: 1588099237000, lastModifiedDate: 1588099237000, …}
end: 79807
lastModified: 1588099237000
lastModifiedDate: 1588099237000
localURL: "cdvfile://localhost/content/com.android.providers.media.documents/document/image%3A78702"
name: "content"
size: 79807
start: 0
type: "image/jpeg"
__proto__: Object

const blob 输出:

Blob {size: 79807, type: "image/jpeg"}
size: 79807
type: "image/jpeg"
__proto__: Blob

表单数据输出:

FormData {}
__proto__: FormData
append: ƒ append()
arguments: (...)
caller: (...)
length: 2
name: "append"
__proto__: ƒ ()
[[Scopes]]: Scopes[0]
delete: ƒ delete()
entries: ƒ entries()
forEach: ƒ forEach()
get: ƒ ()
getAll: ƒ getAll()
has: ƒ has()
keys: ƒ keys()
set: ƒ ()
values: ƒ values()
constructor: ƒ FormData()
Symbol(Symbol.iterator): ƒ entries()
Symbol(Symbol.toStringTag): "FormData"
__proto__: Object

最佳答案

为了获得 blob,您需要做一些事情:

首先,确保 Camera 插件的选项设置为返回 FILE_URI(图像二进制文件的 URL):

options: CameraOptions = {
quality: 100,
destinationType: this.camera.DestinationType.FILE_URI, // set to FILE_URI
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE
};

其次,由于 FILE_URI 只是一个链接而不是实际文件,因此您需要一种获取它的方法。您可以使用文件插件来做到这一点:

// Import it:

import {File, FileEntry} from '@ionic-native/file/ngx';

// Then use it in the method that Camera plugin has for taking pictures:

getPicture() {
this.camera.getPicture(this.options).then((imageDataURI) => {
this.file.resolveLocalFilesystemUrl(imageDataURI).then((entry: FileEntry) =>
{
entry.file(file => {
console.log(file);
this.readFile(file);
});
});
}, (err) => {
// Handle error
});
};

最后要将它发送到您的服务器,您需要读取文件:

read(file) {
const reader = new FileReader();
reader.onload = () => {
const blob = new Blob([reader.result], {
type: file.type
});
const formData = new FormData();
formData.append('name', 'MyImageBlob');
formData.append('file', blob, file.name);
this.service.upload(formData).subscribe(response => {
console.log(response);
});
};
reader.readAsArrayBuffer(file);
};

如果你可以从这里拿走,请告诉我。

关于ionic-framework - 如何将相机图像转换为 ionic 5 中的 blob?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61480171/

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