|
| 1 | +// Java program to demonstrate read and write of image |
| 2 | +import java.io.File; |
| 3 | +import java.io.IOException; |
| 4 | +import java.awt.image.BufferedImage; |
| 5 | +import javax.imageio.ImageIO; |
| 6 | + |
| 7 | +public class MyImage |
| 8 | +{ |
| 9 | + public static void main(String args[])throws IOException |
| 10 | + { |
| 11 | + int width = 963; //width of the image |
| 12 | + int height = 640; //height of the image |
| 13 | + |
| 14 | + // For storing image in RAM |
| 15 | + BufferedImage image = null; |
| 16 | + |
| 17 | + // READ IMAGE |
| 18 | + try |
| 19 | + { |
| 20 | + File input_file = new File("G:\\Inp.jpg"); //image file path |
| 21 | + |
| 22 | + /* create an object of BufferedImage type and pass |
| 23 | + as parameter the width, height and image int |
| 24 | + type.TYPE_INT_ARGB means that we are representing |
| 25 | + the Alpha, Red, Green and Blue component of the |
| 26 | + image pixel using 8 bit integer value. */ |
| 27 | + image = new BufferedImage(width, height, |
| 28 | + BufferedImage.TYPE_INT_ARGB); |
| 29 | + |
| 30 | + // Reading input file |
| 31 | + image = ImageIO.read(input_file); |
| 32 | + |
| 33 | + System.out.println("Reading complete."); |
| 34 | + } |
| 35 | + catch(IOException e) |
| 36 | + { |
| 37 | + System.out.println("Error: "+e); |
| 38 | + } |
| 39 | + |
| 40 | + // WRITE IMAGE |
| 41 | + try |
| 42 | + { |
| 43 | + // Output file path |
| 44 | + File output_file = new File("D:\\Out.jpg"); |
| 45 | + |
| 46 | + // Writing to file taking type and path as |
| 47 | + ImageIO.write(image, "jpg", output_file); |
| 48 | + |
| 49 | + System.out.println("Writing complete."); |
| 50 | + } |
| 51 | + catch(IOException e) |
| 52 | + { |
| 53 | + System.out.println("Error: "+e); |
| 54 | + } |
| 55 | + }//main() ends here |
| 56 | +}//class ends here |
0 commit comments