简体   繁体   English

如何使用Java在浏览器上显示图像?

[英]How do I display an image on the browser using Java?

I have a web service which returns a BufferedImage value, but I am unable to display the image in my browser, knowing that I have already converted it from byte[] to BufferedImage . 我有它返回一个Web服务BufferedImage价值,但我无法在我的浏览器中显示的图像,知道我已经把它转换从byte[]BufferedImage

I receive a result like following. 我收到如下结果。

This is how I convert the byte[] data (I can see the data in byte[] ): 这是我转换byte[]数据的方式(我可以在byte[]看到数据):

ResponseEntity<byte[]> result =  new RestTemplate(messageConverters).exchange(new URI(url), HttpMethod.GET, httpEntity,  byte[].class );
try {
    BufferedImage img = ImageIO.read(new ByteArrayInputStream(result.getBody()));
    return img;
} catch (IOException e) {
    e.printStackTrace();
}

And I get something like this: 我得到这样的东西:

{  
   "accelerationPriority":0.5,
   "colorModel":{  
      "transparency":3,
      "numComponents":4,
      "numColorComponents":3,
      "colorSpace":{  
         "type":5,
         "numComponents":3,
         "profile":{  
            "mediaWhitePoint":[  
               0.9504547,
               1.0,
               1.0890503
            ],
            "matrix":[  
               [  
                  0.43606567,
                  0.3851471,
                  0.1430664
               ],
               [  
                  0.2224884,
                  0.71687317,
                  0.06060791
               ],
               [  
                  0.013916016,
                  0.097076416,
                  0.71409607
               ]
            ],
            "data":"AAAMSGxjbXMCEAAAbW50clJHQiBYWVogB84AAgAJAAYAMQAAYWNzcE1TRlQAAAAASUVDIHNSR0IAAAAAAAAAAAAAAAAAAPbWAAEAAAAA0y1sY21zAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARY3BydAAAAVAAAAAzZGVzYwAAAYQAAABsd3RwdAAAAfAAAAAUYmtwdAAAAgQAAAAUclhZWgAAAhgAAAAUZ1hZWgAAAiwAAAAUYlhZWgAAAkAAAAAUZG1uZAAAAlQAAABwZG1kZAAAAsQAAACIdnVlZAAAA0wAAACGdmlldwAAA9Q...

If I understand correctly, the first byte[] you already have from the rest template is the full in memory representation of the source image. 如果我理解正确,那么其余模板中已有的第一个byte[]是源映像的完整内存表示形式。 If this is the case, why involve a BufferedImage whatsoever? 如果是这样,为什么还要包含BufferedImage Can't you just return the byte[] directly from your controller and be sure to set an appropriate mime type in the http response header. 您不能直接从控制器直接返回byte []并确保在http响应标头中设置适当的mime类型。

 @GetMapping("/")
    public ResponseEntity<byte[]> getImage() {
        final ResponseEntity<byte[]> result =  new RestTemplate(messageConverters).exchange(new URI(url), HttpMethod.GET, httpEntity,  byte[].class );
        return ResponseEntity.status(OK)
                .contentType(MediaType.IMAGE_JPEG)
                .body(result.getBody());
    }

On a separate note, if you are just proxying the image via your service back to the client there might be better solutions - such as spring cloud zuul or having the client directly access the resource. 另外,如果您只是通过服务将映像代理回客户端,则可能会有更好的解决方案-例如spring cloud zuul或让客户端直接访问资源。 Might help to know a little about the image that you are fetching in terms of your requirements. 可能会帮助您了解一些有关您要提取的图像的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM