1

I am having trouble interpreting this question.

I receive a img in JSON formated in Base64, for web developers its only do: "".

How I can make it in Java?

rodrigo
  • 342
  • 1
  • 2
  • 12

2 Answers2

0

You can use apache commons-codec for converting byte array. to base64 and from base64 to byte array. Actually, you can use guava. This artifact has base64 libraries and json. Just add com.google.guava in your maven's project.

For creating image, you can use:

InputStream in = new ByteArrayInputStream(yourbytearray);
BufferedImage bImageFromConvert = ImageIO.read(in);
ImageIO.write(bImageFromConvert, "jpg", new File("c:/yourimage.jpg"));
Oleksandr Loushkin
  • 1,479
  • 12
  • 21
  • 1
    Or, you could use [regular Java SE](http://docs.oracle.com/javase/8/docs/api/java/util/Base64.html) and avoid the burden of a third-party library. – VGR Aug 27 '15 at 20:02
  • OK, I do it. but, how I create a image from the byte array? – rodrigo Aug 27 '15 at 20:15
0
    //JSON funtion
    String cmd_getPhoto = so.cmd_getPhoto();

    //for remove ":"data:image\/png;base64,"
    String imageDataBytes = cmd_getPhoto.substring(cmd_getPhoto.indexOf(",") + 1);
    //for decode
    Base64 b = new Base64();
    byte[] decode = b.decode(imageDataBytes.getBytes());

    //create the stream
    InputStream stream = new ByteArrayInputStream(decode);

    try {
        //set the stream for a bufferedImage and do what your will with it
        BufferedImage bitmap = ImageIO.read(stream);
        jLabel6.setIcon(new ImageIcon(bitmap));
    } catch (IOException ex) {    }
rodrigo
  • 342
  • 1
  • 2
  • 12