Jmagick class instance for realizing basic image processing

Source: Internet
Author: User

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.

?

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 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 5, 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 11 9 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; /** * Use Jmagick for image processing */class Jmagickhandler {private final static int default_quality = protected Jmagickhandler () { Private final static threadlocal<mymagickimage> tlocal = new threadlocal<mymagickimage> (); Private String lastsrc = null; /** * Clear Thread local storage information/private synchronized mymagickimage getmagickimage (String src) throws Exception {mymagickimage mi = TL Ocal.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 (sr c); 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); M Ymagickimage 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 (sr c); 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 = Hasun Icode (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}}

I hope this article will help you with your Java programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.