如何在Java server api中以BuffereImage的形式显示10中接收的图像

如何在Java server api中以BuffereImage的形式显示10中接收的图像,java,angular,microsoft-graph-api,Java,Angular,Microsoft Graph Api,我有一个调用MicrosoftGraph并接收用户照片的api。我不知道如何读取这个api调用来实际显示照片 Java: public BufferedImage getUserPhoto (String accessToken) throws IOException { IAuthenticationProvider mAuthenticationProvider; try{ mAuthenticationProvider=request->reque

我有一个调用MicrosoftGraph并接收用户照片的api。我不知道如何读取这个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字符串,然后以角度接收并显示

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);
      });
  }

您可以将
输入流
转换为
字节数组
。然后以角度显示字节数组作为图像

getUserPhoto()
与Microsoft Graph SDK的函数:

public byte[] getUserPhoto() throws IOException {

    String CLIENT_ID = "";
    List<String> SCOPES = Arrays.asList("scope1", "scope2");
    String CLIENT_SECRET = "";
    String TENANT_GUID = "";
    String userId = "";

    ClientCredentialProvider authProvider = new ClientCredentialProvider(CLIENT_ID, SCOPES, CLIENT_SECRET, TENANT_GUID, NationalCloud.Global);
    IGraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();

    InputStream stream = graphClient.users(userId).photo().content().buildRequest().get();
    byte[] imageArray = new byte[stream.available()];
    stream.read(imageArray);

    return imageArray;
}
public byte[]getUserPhoto()引发IOException{
字符串CLIENT_ID=“”;
List SCOPES=Arrays.asList(“scope1”、“scope2”);
字符串CLIENT_SECRET=“”;
字符串租户_GUID=“”;
字符串userId=“”;
ClientCredentialProvider authProvider=新的ClientCredentialProvider(客户端ID、作用域、客户端机密、租户GUID、NationalCloud.Global);
IGraphServiceClient graphClient=GraphServiceClient.builder().authenticationProvider(authProvider.buildClient();
InputStream=graphClient.users(userId).photo().content().buildRequest().get();
byte[]imageArray=新字节[stream.available()];
读取(图像阵列);
返回图像阵列;
}
注意:我使用需要应用程序权限的ClientCredentialProvider。你可以参考

My Pom.xml:

    <dependency>
        <groupId>com.microsoft.graph</groupId>
        <artifactId>microsoft-graph</artifactId>
        <version>2.5.0</version>
    </dependency>
    <dependency>
        <groupId>com.microsoft.graph</groupId>
        <artifactId>microsoft-graph-auth</artifactId>
        <version>0.2.0</version>
    </dependency>
    <dependency>
        <groupId>com.microsoft.graph</groupId>
        <artifactId>microsoft-graph-core</artifactId>
        <version>1.0.6</version>
    </dependency>

com.microsoft.graph

var bytes=[…];//从服务器获取
var uints=新的UInt8Array(字节);
var base64=btoa(String.fromCharCode(null,uints));
var url='数据:图像/jpeg;base64,“+base64;//在绑定中使用此选项

应该可以工作。只需在组件上放置具有正确路径的photoPath对不起,您能再详细说明一下吗?iI获取错误:获取404(未找到)@nabeelh21尝试查看此。我想您在转换方面有问题。当我将InputStream转换为byte[]并打印到控制台时,它显示为[B@5bb14560Also,从服务器接收此api调用的响应类型是什么?如何使用
System.out.println(imageArray);
System.out.println(Arrays.toString)打印字节[](imageArray));
?响应类型为。
var bytes = [ ... ]; // get from server
var uints = new UInt8Array(bytes);
var base64 = btoa(String.fromCharCode(null, uints));
var url = 'data:image/jpeg;base64,' + base64; // use this in <img src="..."> binding