JMagick이 기본 이미지 처리를 실현하는 클래스 실례

6290 단어 JMagick이미지
본고는 JMagick이 기본적인 이미지 처리를 실현하는 클래스를 실례로 다루고 있다.여러분에게 참고할 수 있도록 나누어 드리겠습니다.구체적인 분석은 다음과 같다.
JMagick은 오픈소스 API로 JNI(Java Native Interface) 기술을 이용하여 ImageMagick API에 대한 Java 액세스 인터페이스를 실현하기 때문에 순수 Java가 실현하는 이미지 조작 함수보다 속도가 빠르다.

/**
 * JMagickHandler.java
 * 
 * Author: Winter Lau
 * 2007-7-11
 */
package com.eportal.img;
import java.awt.Dimension;
import java.io.File;
import org.apache.commons.io.FileUtils;
import com.gif4j.GifDecoder;
import com.gif4j.GifEncoder;
import com.gif4j.GifImage;
import com.gif4j.GifTransformer;
import dlog.common.util.StringUtils;
import magick.ImageInfo;
import magick.MagickException;
import magick.MagickImage;
/**
 *  JMagick 
 */
class JMagickHandler {
  private final static int DEFAULT_QUALITY = 90;
  protected JMagickHandler(){}
  private final static ThreadLocal<MyMagickImage> tLocal = new ThreadLocal<MyMagickImage>();
  private String lastSrc = null;
  /**
   *  
   */
  private synchronized MyMagickImage getMagickImage(String src) throws Exception {
    MyMagickImage mi = tLocal.get();
    if(mi != null && StringUtils.equals(src, lastSrc))
      return mi;
    else if(mi != null)
      tLocal.remove();
    this.lastSrc = src;
    ImageInfo info = new ImageInfo(src);
    mi = new MyMagickImage(info);
    tLocal.set(mi);
    return mi;
  }
  @Override
  public void cleanup() {
    MyMagickImage mi = tLocal.get();
    if(mi != null)
      mi.destroyImages();
    tLocal.remove();
  }
  @Override
  public ImageExtInfo getImageInfo(String src) throws Exception {
    try{
      MagickImage image = this.getMagickImage(src);
      ImageExtInfo ext = new ImageExtInfo();
      Dimension dim = image.getDimension();
      ext.setWidth((int)dim.getWidth());
      ext.setHeight((int)dim.getHeight());
      ext.setSize(image.sizeBlob());
      ext.setAnimated(image.isAnimatedImage());
      return ext;
    }catch(MagickException e){
      return new PureJavaHandler().getImageInfo(src);
    }
  }
  private final static boolean hasUnicode(String str){
    return str.getBytes().length != str.length();
  }
  @Override
  public void resize(String src, String dest, int width, int height, int quality) throws Exception {
    boolean u_src = hasUnicode(src);
    MyMagickImage image;
    File srctmp = null;
    if(u_src){
      srctmp = File.createTempFile("jmagick_s_" + src.hashCode(), null);
      FileUtils.copyFile(new File(src), srctmp);
      image = this.getMagickImage(srctmp.getAbsolutePath());
    }
    else
      image = this.getMagickImage(src);
    image.getImageInfo().setQuality((quality>0)?quality:DEFAULT_QUALITY);
    MagickImage scaledimage = null;
    try{
      if(image.isAnimatedImage()){
        GifImage gifImage = GifDecoder.decode(new File(src));
        GifImage newGif = GifTransformer.resize(gifImage, width, height, false);        
        GifEncoder.encode(newGif, new File(dest));
      }
      else{//others
        scaledimage = image.scaleImage(width, height);
        scaledimage.setImageFormat("JPEG");
        scaledimage.profileImage("*", null);
        boolean u_dest = hasUnicode(dest);
        if(u_dest){
          File tmp = File.createTempFile("jmagick_d_" + dest.hashCode(), null);
          scaledimage.setFileName(tmp.getAbsolutePath()); 
          scaledimage.writeImage(image.getImageInfo());
          FileUtils.copyFile(tmp, new File(dest));
          FileUtils.forceDelete(tmp);
        }
        else{
          scaledimage.setFileName(dest); 
          scaledimage.writeImage(image.getImageInfo());
        }
      }      
    }finally{
      if(srctmp != null) FileUtils.forceDelete(srctmp);
      if(scaledimage != null)
        scaledimage.destroyImages();      
    }
  }
  @Override
  public void rotate(String src, String dest, double degrees) throws Exception {
    boolean u_src = hasUnicode(src);
    MyMagickImage image;
    File srctmp = null;
    if(u_src){
      srctmp = File.createTempFile("jmagick_s_" + src.hashCode(), null);
      FileUtils.copyFile(new File(src), srctmp);
      image = this.getMagickImage(srctmp.getAbsolutePath());
    }
    else
      image = this.getMagickImage(src);
    try{
      MagickImage rotateImg = image.rotateImage(degrees);
      rotateImg.profileImage("*", null);
 
      boolean u_dest = hasUnicode(dest);
      if(u_dest){
        File tmp = File.createTempFile("jmagick_d_" + dest.hashCode(), null);
        rotateImg.setFileName(tmp.getAbsolutePath()); // convert to png
        rotateImg.writeImage(image.getImageInfo());
        rotateImg.destroyImages();
        FileUtils.copyFile(tmp, new File(dest));
        FileUtils.forceDelete(tmp);
      }
      else{
        rotateImg.setFileName(dest); // convert to png
        rotateImg.writeImage(image.getImageInfo());
        rotateImg.destroyImages();       
      }
    }finally{
      if(srctmp != null)
        FileUtils.forceDelete(srctmp);
    }
  }
  private static class MyMagickImage extends MagickImage {
    private ImageInfo imageInfo;
    public ImageInfo getImageInfo() {
      return imageInfo;
    }
    public MyMagickImage(ImageInfo info) throws MagickException{
      super(info);
      this.imageInfo = info;
    }
  }
  @Override
  public void cropImage(String src, int width, int height, int point,
      int point2, String dst) throws Exception {
    // TODO Auto-generated method stub
  }
}

본고에서 기술한 것이 여러분의 자바 프로그램 설계에 도움이 되기를 바랍니다.

좋은 웹페이지 즐겨찾기