Send java image to javascript

Asked

Viewed 113 times

0

I’m developing a web service Rest with Spring in Java which provides images to front-end, which calls Java with an ajax event. On the server side I am using java Image.

import java.awt.Image;

I create an image based on another

Image img = bimg.getScaledInstance( width , height , BufferedImage.SCALE_SMOOTH );
BufferedImage buffer = toBufferedImage( img );
ByteArrayOutputStream bao = new ByteArrayOutputStream();
ImageIO.write( buffer , image.getMime( ) , bao );
String encoded = Base64.encodeBase64String( bao.toByteArray( ) );

Only that the string encoded always comes empty. The web service is developed with spring boot.

@RestController
public class ImageController {
...
@RequestMapping( value = "/" , method = RequestMethod.GET )
public ImageSearchResults getImages( @RequestParam(value="input", defaultValue="") String input) {
    List< ImageResult > imageResults = getImageResults( input ); 
    return new ImageSearchResults( imageResults );
}

}

getImageResult processes the request and builds the list of objects of type Imageresult that one of the attributes would be the image itself, as well as the height and length of the image.

Any idea what is the best way to pass a backend (web service) image that is only in memory to the front-end(js) ?

  • You can post your full endpoint (or the relevant parts, such as the signature and return) and the form on the front end that you are invoking?

  • Edited with more details about the backend, in turn the front-end is a simple ajax call to the server, then parse the json to retrieve the information.

  • What the bao.toByteArray() returns?

  • Creates a Newly allocated byte array. Its size is the Current size of this output stream and the Valid Contents of the buffer have been copied into it. It is the setting in javadoc, returns an array of buffer bytes

  • I know what the javadoc definition is. What I want to know is if the returned array has the desired data, if it is empty, if it has only a bunch of zeros, if it has the right size, etc.

  • I think you have the desired data. Example of a print: Bao.toByteArray() = [B@41266fe9

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.