The example of this article describes the class of Jmagick to implement basic image processing. Share to everyone for your reference. The specific analysis is as follows:
Jmagick is an open source API that uses JNI (Java Native Interface) technology to implement the Java Access interface to the ImageMagick API, so it will be faster than a pure Java-implemented picture manipulation function.
/** * 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;
/** * Use Jmagick for image processing * * Class Jmagickhandler {private final static int default_quality = 90; Protected Jmagickhandler () {} private final static threadlocal<mymagickimage> tlocal = new Threadlocal<mymagick
Image> ();
Private String lastsrc = null; /** * Purge Thread local Storage information * * Private synchronized mymagickimage getmagickimage (String src) throws Exception {Mymagicki
Mage 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.ge
Tmagickimage (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 {Boo Lean 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 = has
Unicode (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-generat
Ed method Stub}}
I hope this article will help you with your Java programming.