code: private static byte[] getByteArray(BufferedImage image) {
      checkNotNull(image);
  
      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      try {
        ImageIO.write(image, "png", stream);
      } catch (IOException e) {
        throw new IllegalStateException(e);
      }
  
      return stream.toByteArray();
    }
docs: Get a byte array of the data of a buffered image.
  
  @param image the image to retrieve a byte array of
  @return a byte array of the buffered image's data