MyImage.java 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.awt.image.BufferedImage;
  4. import javax.imageio.ImageIO;
  5. public class MyImage{
  6. public static void main(String args[])throws IOException{
  7. int width = 963; //width of the image
  8. int height = 640; //height of the image
  9. BufferedImage image = null;
  10. File f = null;
  11. //read image
  12. try {
  13. f = new File("img/Taj_Mahal3.png"); //image file path
  14. image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  15. image = ImageIO.read(f);
  16. System.out.println("Reading complete.");
  17. } catch(IOException e){
  18. System.out.println("Error: "+e);
  19. }
  20. System.out.println("width = " + image.getWidth() + " height = " +
  21. image.getHeight());
  22. //write image
  23. try {
  24. f = new File("img/output.jpg"); //output file path
  25. ImageIO.write(image, "jpg", f);
  26. System.out.println("Writing complete.");
  27. } catch(IOException e){
  28. System.out.println("Error: "+e);
  29. }
  30. }//main() ends here
  31. }//class ends here