我有一个api,它调用MicrosoftGraph并接收用户的照片。我不知道如何在angular中读取此api调用以实际显示照片。
java 语:
public BufferedImage getUserPhoto (String accessToken) throws IOException {
IAuthenticationProvider mAuthenticationProvider;
try{
mAuthenticationProvider=request->request.addHeader("Authorization",accessToken);
}catch(Exception e){
throw new Error("Could not create a graph client: "+e.getLocalizedMessage());
}
graphClient=
GraphServiceClient.builder()
.authenticationProvider(mAuthenticationProvider)
.buildClient();
InputStream stream =
graphClient
.me()
.photo()
.content()
.buildRequest()
.get();
System.out.println("Stream"+ stream);
BufferedImage image = ImageIO.read(stream);
return image;
}
棱角分明的
getUserPhoto(){
return this.httpClient.get(`${API_URL}/graph/getPhoto`);
};
编辑:我想出来了。我将缓冲后的图像转换成base64字符串,然后以Angular 接收并显示。
BufferedImage image = ImageIO.read(stream);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(image, "jpeg", bos);
byte[] imageBytes = bos.toByteArray();
BASE64Encoder encoder = new BASE64Encoder();
String imageString = encoder.encode(imageBytes);
bos.close();
棱角分明的
getPhoto(){
return this.httpClient.get(`${API_URL}/graph/getPhoto`,{ responseType: "text" });
}
public getUserPhoto(){
this.graphService.getPhoto().subscribe(
response => {
let objectURL = 'data:image/jpeg;base64,' + response;
this.photo = this.sanitizer.bypassSecurityTrustUrl(objectURL);
});
}
1条答案
按热度按时间fcg9iug31#
你可以改变信仰
InputStream
至byte array
. 然后以Angular 显示字节数组作为图像。getUserPhoto()
microsoft graph sdk函数:注意:我使用需要应用程序权限的clientcredentialprovider。你可以参考这里。
我的pom.xml:
以Angular 显示字节数组作为图像:https://stackoverflow.com/a/53391591/13308381