12 Mart 2017 Pazar

JAI Sınıfı - Java Advanced Imaging

Giriş
Şu satırı dahil ederiz.
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.RenderedOp;
Java Advanced Imaging harflerinin kısaltmasıdır.

create metodu
Örnek
Şöyle yaparız.
RenderingHints hints = ...;
ParameterBlock paramBlock = ...
RenderedOp resizedImage = JAI.create("SubsampleAverage", paramBlock, hints);
BufferedImage img = resizedImage.getAsBufferedImage();      
Örnek
Şöyle yaparız. Bu kodda AWT , Jimi ve JAI kullanılarak resim thumbnail haline getiriliyor.

- AWT ile yapmak için image.getScaledInstance() ile yeni bir Image elde ediliyor. Daha sonra
bir BufferedImage yaratılıyor ve thumbnail olan yeni Image buraya yazılıyor.

- Jimi ile yapmak için Canvas'tan Image. Image'dan BufferedImage. BufferedImage'dan Graphics2D elde ediliyor. Graphics2D.drawImage() ile thumbnail olan yeni Image buraya yazılıyor.

- JAI ile yapmak için  JAI.create("scale", ...); kullanılıyor
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.RenderedImageAdapter;
import javax.media.jai.Interpolation;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageProducer;
import java.awt.image.FilteredImageSource;
import java.awt.image.renderable.ParameterBlock;
import com.sun.jimi.core.filters.AreaAverageScaleFilter;

public class JaiAwtTest {
  private final static int SCALE_DIM = 140;
  public static void main(String args[]) throws Exception {
    if (args.length != 1) {
      System.out.println("Usage: java JaiAwtTest file.jpg");
      System.exit(0);
    }
    File file = new File(args[0]);
    FileInputStream in = new FileInputStream(file);
    ImageInputStream iin = ImageIO.createImageInputStream(in);
    ImageReader reader = ImageIO.getImageReaders(iin).next();
    reader.setInput(iin, true, true);
    BufferedImage original = reader.read(0);
    BufferedImage scaled;
    ImageIO.setUseCache(false);
    FileOutputStream fout;

    scaled = awtScaleImage(original, SCALE_DIM, Image.SCALE_FAST);
    fout = new FileOutputStream("scaleFast.png");
    ImageIO.write(scaled, "png", fout);
    fout.close();

    scaled = awtScaleImage(original, SCALE_DIM, Image.SCALE_SMOOTH);
    fout = new FileOutputStream("scaleSmooth.png");
    ImageIO.write(scaled, "png", fout);
    fout.close();

    scaled = jaiScaleImage(original, SCALE_DIM,
      Interpolation.getInstance(Interpolation.INTERP_BILINEAR));
    fout = new FileOutputStream("bilinear.png");
    ImageIO.write(scaled, "png", fout);
    fout.close();

    scaled = jaiScaleImage(original, SCALE_DIM,
      Interpolation.getInstance(Interpolation.INTERP_BICUBIC));
    fout = new FileOutputStream("bicubic.png");
    ImageIO.write(scaled, "png", fout);
    fout.close();

    scaled = jaiScaleImage(original, SCALE_DIM,
      Interpolation.getInstance(Interpolation.INTERP_BICUBIC_2));
    fout = new FileOutputStream("bicubic2.png");
    ImageIO.write(scaled, "png", fout);
    fout.close();

    scaled = jimiScaleImage(original, SCALE_DIM);
    fout = new FileOutputStream("jimiAreaAverage.png");
    ImageIO.write(scaled, "png", fout);
    fout.close();
  }
  static BufferedImage awtScaleImage(BufferedImage image,
                                     int maxSize, int hint) {
    // We use AWT Image scaling because it has far superior quality
    // compared to JAI scaling.  It also performs better (speed)!
    System.out.println("AWT Scaling image to: " + maxSize);
    int w = image.getWidth();
    int h = image.getHeight();
    float scaleFactor = 1.0f;
    if (w > h)
      scaleFactor = ((float) maxSize / (float) w);
    else
      scaleFactor = ((float) maxSize / (float) h);
    w = (int)(w * scaleFactor);
    h = (int)(h * scaleFactor);
    // since this code can run both headless and in a graphics context
    // we will just create a standard rgb image here and take the
    // performance hit in a non-compatible image format if any
    Image i = image.getScaledInstance(w, h, hint);
    image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = image.createGraphics();
    g.drawImage(i, null, null);
    g.dispose();
    i.flush();
    return image;
  }
  static BufferedImage jimiScaleImage(BufferedImage image,
                                      int maxSize) {
    // We use AWT Image scaling because it has far superior quality
    // compared to JAI scaling.  It also performs better (speed)!
    System.out.println("AWT Scaling image to: " + maxSize);
    int w = image.getWidth();
    int h = image.getHeight();
    float scaleFactor = 1.0f;
    if (w > h)
      scaleFactor = ((float) maxSize / (float) w);
    else
      scaleFactor = ((float) maxSize / (float) h);
    w = (int)(w * scaleFactor);
    h = (int)(h * scaleFactor);
    // since this code can run both headless and in a graphics context
    // we will just create a standard rgb image here and take the
    // performance hit in a non-compatible image format if any
    ImageProducer p = new FilteredImageSource(image.getSource(),
      new AreaAverageScaleFilter(w, h));
    java.awt.Canvas c = new java.awt.Canvas();
    Image i = c.createImage(p);
    image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = image.createGraphics();
    g.drawImage(i, null, null);
    g.dispose();
    i.flush();
    return image;
  }
  static BufferedImage jaiScaleImage(BufferedImage image,
                                     int maxSize, Interpolation interp) {
    System.out.println("JAI Scaling image to: " + maxSize);
    PlanarImage pi = new RenderedImageAdapter(image);
    int w = pi.getWidth();
    int h = pi.getHeight();
    float scaleFactor = 1.0f;
    if (w > h)
      scaleFactor = ((float) maxSize / (float) w);
    else
      scaleFactor = ((float) maxSize / (float) h);
    ParameterBlock pb = new ParameterBlock();
    pb.addSource(pi);
    pb.add(scaleFactor);
    pb.add(scaleFactor);
    pb.add(0f);
    pb.add(0f);
    pb.add(interp);
    pi = JAI.create("scale", pb);
    return pi.getAsBufferedImage();
  }
}

Hiç yorum yok:

Yorum Gönder