node.js - Microsoft 认知服务 API 的原始二进制数据
问题描述
对于 Microsoft Azure Cognitive Services API,图像需要以这种格式传递
在 POST 正文中传递的输入。支持的输入法:原始图像二进制。
因此,我对如何将用户上传的图像转换为该格式并发出 API 请求感到非常迷茫。我在前端使用 ReactJS 和 NodeJs 后端。有人可以帮我获取正确格式的图像吗?我不确定是否必须将其作为数组缓冲区读取?
import React, { Component } from 'react';
import Button from 'react-bootstrap/Button';
class Dashboard extends Component {
constructor(props) {
super(props);
this.state ={
file: null
}
}
onSubmit = () => {
console.log(this.state.file);
// console.log(window.atob(this.state.file));
}
onChange = (e) => {
const file = e.target.files[0];
const reader = new FileReader()
reader.addEventListener("load", () => {
// convert image file to base64 string
console.log(reader);
// if (reader.result.includes("data:image/png;base64,")) {
// img = reader.result.replace("data:image/png;base64,", "");
// } else {
// img = reader.result.replace("data:image/jpeg;base64,", "");
// }
//this.setState({file: img});
}, false);
if (file) {
reader.readAsArrayBuffer(file);
}
}
render() {
return(
<div>
<h3 style={{padding: '20px', textAlign: 'center', color: 'white', fontWeight: '100'}}>
Customize your playlist based on your mood!
</h3>
<h5 style={{margin: '30px', padding: '0px',textAlign: 'center', color: 'grey', display:'block'}}>
Click a picture of your surroundings or simply upload one based on what you're currently in the mood for and
<br />
<br />
TuneIn will add a playlist according to your liking!
</h5>
<form onSubmit={this.onSubmit}>
<h1>File Upload</h1>
<input type="file" accept="image/png, image/jpeg" onChange={this.onChange}/>
<button type="submit">Upload</button>
</form>
</div>
);
}
}
export default Dashboard;
解决方案
Here is the sample for Analyzing a local image using the Computer Vision REST API and javascript.
推荐阅读
- mysql - 'where 子句'中的未知列'david'
- c# - 如何使用 0 获取一个月中的天数列表?
- java - 多租户系统的 Spring 验证查询
- python - 使用 f2py 从 Fortran 修改 Numpy 字符串数组
- javascript - 如何使用 for 循环更改字体大小
- postgresql - Postgres Notify 不适用于逻辑复制
- javascript - 为什么我们不能在不调用子类中的超级构造函数的情况下访问超级类属性?
- java - 在 AppCompactActivity 中设置布局元素分为片段
- regex - 为什么这个正则表达式不适用于 Analytics?
- mysql - 也许如何从选择中选择