The example of this article tells the class that Jmagick implements basic image processing. Share it for everyone for your reference. The specific analysis is as follows:
Jmagick is an open source API. Using JNI (Java Native Interface) technology to realize the Java access interface of the ImageMagick API, it will also be faster than the picture operation function implemented by pure Java.
/** * jmagickhandler.Java * * * * * *Inter Lau * 2007-7-11 */Package com.eportal.img; Import java.awt.dimension; Import Java.file; Import pache.commons.io .Fileutils; Import com.gif4j.gifdecoder; Import com.gif4j.gifencoder; Import com.gif4j.gifimage; Import com.gif4j.giftransFormer; On.util.Stringutils; Import Magick.imageInfo; Import Magick.magickxception; Import Magick.magickImage;/*** Use jmagick to perform image processing*/class jmagickhandler {Private Final Int default_quality = 90; ProteCTED Ler () {} Private Final Static ThreadLocal <mymagickImage> TLOCAL = New ThreadLocal <MymagickImage> (); Private String Lastsrc = NULL; / *** Clear Local Storage Information* / Private Synchronized MyMagickImage Getmagickimage (String SRC) Thrown Exception { Ge 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 (in fo); tlocal.set (mi); Return mi;}} @Override Public Void Cleanup () {MyMagickimage Mi = TLOCAL.GET (); if (mi! = Null) mi.Destroyimages (); tlocal.remove (); @o Verride Public ImageExtinfo GetimageInfo (String SRC) Throws Exception {Try {Magickimage Image = This.Getmagickimage (SRC); ImageExtinfo EXT = New ImageExtinfo (); Dimension Dim = Image.getdimen sign (); ext.SetWidth ((int) dia.getWidth ()); ext.Setheight ( (int) dim.getHeight ()); Ext.Setsize (Image.sizeblob ()); Ext.SetanImated (Image.isanimatedImage ()); Return EXT; e) {Return New Purejavahandler (). Getimageinfo ( SRC);}} Private Final Static Boolean Hasunicode (String Str) {Return Str.getBytes (). Length! = Str.Length (); Ring 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.CREATETETE mpfile ("jmagick_s_" + src.hashcode (), null); Fileutils.copyfile ( New file (src), sRCTMP); Image = this.getmagickimage (sRCTMP.GetabsolutePath ()); Ageinfo (). SetQuality (Quality> 0? Quality: Default_quility );; Try {if (image.isanimatedImage ()) {gifimage gifimage = gifdecode (SRC)); Gif = giftransformer.Resize (gifimage, width, height, false); gifencoder. ENCODE (newGif, New File (DEST));} Else {// Others scaledImage = Image.scaleImage (width, height); scaledImage.setImageFormat ("JPEG"); image.profileimage ("*", null); Boolean u_dest = HASUNICODE (DEST); if (u_dest) {file tmp = file.createTempfile ("jmagick_d_" + Dest.hashcode (), null); scaledImage.SetFilename lutepath ()); ); Fileutils.copyfile (TMP, New File (Dest)); Fileutils.Forcedelete (TMP);} Else {scaledImage.SetFilename (DEST); Age.getImageinfo ());}}} Finally {if (SRCTMP! = NULL) FileUtils.Forcedelete (SRCTMP); if (scaledImage! = NULL) scaledImage.destroyimages (); G 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); .copyfile (new file (src), sRCTMP); Image = this.getmagickImage (sRCTMP.Getabsolutepath ());} else image = this.getmagickimage (src); try {magickimage rotateimg = image.rotate Image (deGrees); rotateimg.profileimage ("*", null); Boolean u_dest = hasunicode (Dest); if (u_dest) {file tmp = file.createTempfile ("jmagick_d_" + Dest.hashcode (), null); Convert to png rotateimg.writeimage .getImageInfo ()); Rotateimg.Destroyimages (); Fileutils.copyfile (TMP, New File (DEST)); Fileutils.Forcedelete (tmp);} else {rotateimg.setfi lename (Dest); // Convert to png rotateimg.writeimage (Image.getImageinfo ()); Rotateimg.destroyimages ();}} Finally {if (sRCTMP! = NULL) Fileutils.Forcedelete (sRCTMP);}}} s mymagickimage Extends MagickImage {Private ImageInfo Imageinfo; Public ImageInfo GetimageInfo () {Return Imageinfo;} Public MyMagickImage (ImageInfo Info) Throws Magickexception {Super (Info); this.imageinfo = Info; LIC VOID Cropimage (String SRC, Int Width, Int Height, Int Point2, String DST ) Throws Exception {// Todo Auto-Gonement Method Stub}}}
It is hoped that this article is helpful to everyone's Java program design.