JMagick实现基本图像处理的类实例

时间:2022-02-17 01:26:48

本文实例讲述了JMagick实现基本图像处理的类。分享给大家供大家参考。具体分析如下:

JMagick是一个开源API,利用JNI(Java Native Interface)技术实现了对 ImageMagick API的Java访问接口,因此也将比纯Java实现的图片操作函数在速度上要快。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
 * 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
  }
}

希望本文所述对大家的java程序设计有所帮助。