Create BufferedImage from byte array reading from a file - Java File Path IO

Java examples for File Path IO:File Operation

Description

Create BufferedImage from byte array reading from a file

Demo Code

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.imageio.ImageIO;

public class Main {
  public static void main(String[] args) {
    Path ball_path = Paths.get("C:/folder1/photos", "ball.png");

    try {/* ww w .j a va  2 s .  co  m*/
      byte[] ballArray = Files.readAllBytes(ball_path);

      BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(
          ballArray));
      ImageIO.write(bufferedImage, "png",
          (ball_path.resolveSibling("bytes_to_ball.png")).toFile());

    } catch (IOException e) {
      System.out.println(e);
    }

  }
}

Related Tutorials