Sponsor Reklam 


19.01.2009 (02:36)

 Java'da JPG ve PNG Formatında Resim Okuma ve Yazma 
Daha önce PGM (Portable Gray Map File) formatında gri seviyeli olan resimleri okuma ve yazmadan şu yazıda bahsetmiştik.

Şimdi de JPG (Joint Photographic Experts Group) veya PNG (Portable Network Graphics) formatlarında resim okumaya ve yazmaya bakalım.

Örnek olarak, JPG veya PNG formatında bir resmin negatifini alalım. Bu amaçla dizini verilen bir resmi hem okuyacağız, hem üzerinde işlem yapacağız hem de kopyasının son halini dizine yazdıracağız.

Main.java:
package kodcu.net;

import java.awt.Color;

public class Main {

  public static final String READ_PATH = "C://kodcu.net.oku.jpg";
  public static final String WRITE_PATH = "C://kodcu.net.yaz.jpg";

  /**
  * main metodu
  * @param args String[]
  */

  public static void main(String[] args) {
    // Resmi okuyalim
    Picture picture = new Picture(READ_PATH);
    // Resmin negatifini alalim
    picture = getNegative(picture);
    // Resmi yazalim
    picture.save(WRITE_PATH);
  }
 
  /**
  * This method finds the negative of the image
  * @param picture Picture
  * @return Picture
  */

  public static Picture getNegative(Picture picture) {   
    // Resmin boyutlarini bulalim
    int width = picture.width();
    int height = picture.height();
    Color color;
    int red; // kirmizi renk
    int green; // yesil renk
    int blue; // mavi renk
    // Tum piksellerin negatifi alinir
    for (int i = 0; i < width; i++) {
      for (int j = 0; j < height; j++) {
        color = picture.get(i, j);
        // RGB values
        red = color.getRed();
        green = color.getGreen();
        blue = color.getBlue();
        // Negative values
        red = 255 - red;
        green = 255 - green;
        blue = 255 - blue;
        color = new Color(red, green, blue);
        picture.set(i, j, color);
      }
    }
    return picture;
  }

}

Picture.java:
package kodcu.net;

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.net.URL;

public final class Picture {
    private BufferedImage image;    // for bitmap
    private String filename;        // name of file

    public Picture(int w, int h) {
        image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        // set to TYPE_INT_ARGB to support transparency
        filename = w + "-by-" + h;
    }

  /**
    * Create a picture by reading in a .png, .gif, or .jpg from
    * the given filename or URL name.
    */

    public Picture(String filename) {
        this.filename = filename;
        try {
            // try to read from file in working directory
            File file = new File(filename);
            if (file.isFile()) {
                image = ImageIO.read(file);
            }

            // now try to read from file in same directory as this .class file
            else {
                URL url = getClass().getResource(filename);
                if (url == null) { url = new URL(filename); }
                image = ImageIO.read(url);
            }
        }
        catch (IOException e) {
            // e.printStackTrace();
            throw new RuntimeException("Could not open file: " + filename);
        }

        // check that image was read in
        if (image == null) {
            throw new RuntimeException("Invalid image file: " + filename);
        }
    }

  /**
    * Create a picture by reading in a .png, .gif, or .jpg from a File.
    */

    public Picture(File file) {
        try { image = ImageIO.read(file); }
        catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Could not open file: " + file);
        }
        if (image == null) {
            throw new RuntimeException("Invalid image file: " + file);
        }
    }

  /**
    * Return the height of the picture (in pixels).
    */

    public int height() {
        return image.getHeight(null);
    }

  /**
    * Return the width of the picture (in pixels).
    */

    public int width() {
        return image.getWidth(null);
    }

  /**
    * Return the Color of pixel (i, j).
    */

    public Color get(int i, int j) {
        return new Color(image.getRGB(i, j));
    }

  /**
    * Set the Color of pixel (i, j) to c.
    */

    public void set(int i, int j, Color c) {
        if (c == null) { throw new RuntimeException("can't set Color to null"); }
        image.setRGB(i, j, c.getRGB());
    }

  /**
    * Save the picture to a file in a standard image format.
    * The filetype must be .png or .jpg.
    */

    public void save(String name) {
        save(new File(name));
    }

  /**
    * Save the picture to a file in a standard image format.
    */

    public void save(File file) {
        this.filename = file.getName();
        String suffix = filename.substring(filename.lastIndexOf('.') + 1);
        suffix = suffix.toLowerCase();
        if (suffix.equals("jpg") || suffix.equals("png") || suffix.equals("gif")) {
            try { ImageIO.write(image, suffix, file); }
            catch (IOException e) { e.printStackTrace(); }
        }
        else {
            System.out.println("Error: filename must end in .jpg or .png");
        }
    }

}

Imza: admin








Yazilar kaynak gosterilmeden kopyalanamaz © www.kodcu.net // Twitter