如何在java server api中显示作为BuffereImage接收的图像

ig9co6j1  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(404)

我有一个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);
      });
  }
fcg9iug3

fcg9iug31#

你可以改变信仰 InputStreambyte array . 然后以Angular 显示字节数组作为图像。 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;
}

注意:我使用需要应用程序权限的clientcredentialprovider。你可以参考这里。
我的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>

以Angular 显示字节数组作为图像:https://stackoverflow.com/a/53391591/13308381

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

相关问题