Alibaba SimpleImage Scale, cut, add watermark operation

Source: Internet
Author: User
Import Java.awt.Point;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;

Import java.io.IOException;
Import Javax.imageio.ImageIO;

Import Javax.media.jai.PlanarImage;
Import Org.apache.commons.io.IOUtils;

Import Org.apache.commons.lang.StringUtils;
Import Com.alibaba.simpleimage.ImageFormat;
Import Com.alibaba.simpleimage.ImageWrapper;
Import com.alibaba.simpleimage.SimpleImageException;
Import Com.alibaba.simpleimage.render.CropParameter;
Import Com.alibaba.simpleimage.render.ScaleParameter;
Import Com.alibaba.simpleimage.render.WatermarkParameter;
Import Com.alibaba.simpleimage.render.WriteParameter;
Import Com.alibaba.simpleimage.render.ScaleParameter.Algorithm;
Import Com.alibaba.simpleimage.util.ImageCropHelper;
Import Com.alibaba.simpleimage.util.ImageDrawHelper;
Import Com.alibaba.simpleimage.util.ImageReadHelper;
Import Com.alibaba.simpleimage.util.ImageScaleHelper; Import com.alibaba.simpleimage.util.iMagewritehelper;

Import com.ram.RamConstants; /** * Alibaba simpleimage image processing tool class functions such as proportional scaling crop and watermark composite operation * * @author 13643 * * * * public class Imageutils {Publi
	c static String Water_image_url = "D:\\img\\watermark.png";

	protected static ImageFormat OutputFormat = Imageformat.jpeg; public static void Main (string[] args) {//1. Proportional Scaling//Scalenormal ("D:\\img\\src.jpg", "D:\\img\\scalenormal.jpg", 720
		,//1080);
		2. Equal scale watermark//Scalewithwatermark ("d:\\img\\src.jpg",//"D:\\img\\scalewithwatermark.jpg", 720, 1080);
		3. Zoom to the specified width//scalewithwidth ("D:\\img\\src.jpg", "d:\\img\\scalewithwidth.jpg",//400);
		4. Zoom to the specified height//scalewithheight ("D:\\img\\src.jpg", "d:\\img\\scalewithheight.jpg",//600);
		5. Cut into square/cut ("D:\\img\\src.jpg", "d:\\img\\cut.jpg");
6. From the middle cut//Cutcenter ("D:\\img\\src.jpg", "d:\\img\\cutcenter.jpg", 600,800);
Print (40,20, 4,2);
Print (20,40, 4,2);

	Print (40,20, 4,3); }/** * * @param pinput * @param pImGeflag * @return * @throws Exception */public static Boolean Ispicture (String pinput, String pimgeflag) throws Exc
		eption {if (Stringutils.isblank (pinput)) {return false;
		} String tmpname = pinput.substring (Pinput.lastindexof (".") + 1, pinput.length ()); String imgearray[][] = {{"bmp", "0"}, {"Dib", "1"}, {"GIF", "2"}, {"Jfif", "3"}, {"Jpe", "4"}, {"JPEG", "5"}, {"JPG", "6"}
		{"PNG", "7"}, {"TIF", "8"}, {"Tiff", "9"}, {"Ico", "10"}}; for (int i = 0; i < imgearray.length; i++) {if (! Stringutils.isblank (Pimgeflag) && imgearray[i][0].equals (Tmpname.tolowercase ()) && imgearray[i][1]
			. Equals (Pimgeflag)) {return true;
			} if (Stringutils.isblank (Pimgeflag) && imgearray[i][0].equals (Tmpname.tolowercase ())) {return true;
	}} return false; /** * Proportional Zoom will trim part of the content * * @param src * @param target * @param width * @param height */@SuppressWarnings ("Static-access") public final static void scale (String src, String target, int width, int height) {file out = new File (target);//Destination picture FileOutputStream OutStream = null; File in = new file (src);
		Original picture FileInputStream instream = null;
			try {instream = new FileInputStream (in);

			Imagewrapper imagewrapper = Imagereadhelper.read (instream);
			int w = imagewrapper.getwidth ();
			
			int h = imagewrapper.getheight ();
			Float w1= 0f, H1 = 0f;
			float sp = (float) w/h;
			float RP = (float) width/height;
				if (sp > RP) {w1 = (Width * h)/(float) W;

			H1 = width;
				} else if (SP&LT;RP) {h1 = (height * w)/(float) H;
			W1 = width;
				}else{W1=width;
			H1=height; }//1. Zoom Scaleparameter Scaleparam = new Scaleparameter ((int) w1, (int) h1, algorithm.lanczos);
			Scaling parameters Planarimage Planrimage = Imagescalehelper.scale (Imagewrapper.getasplanarimage (), Scaleparam);
			Imagewrapper = new Imagewrapper (planrimage);
			4. Output OutStream = new FileOutputStream (out); String prefix = out.getname (). substRing (Out.getname (). LastIndexOf (".") + 1);
		Imagewritehelper.write (Imagewrapper, OutStream, Outputformat.getimageformat (prefix), new Writeparameter ());
		} catch (IOException e) {e.printstacktrace (); } catch (Simpleimageexception e) {} finally {ioutils.closequietly (instream);//Picture file input/output stream must be remembered to close Ioutils.closequi

		etly (OutStream); }} public final static void Scalenormal (string src, string target, int width, int height) {file out = new file (targe T);
		Purpose picture FileOutputStream outstream = null; File in = new file (src);
		Original picture FileInputStream instream = null;
			try {instream = new FileInputStream (in);
			Imagewrapper imagewrapper = Imagereadhelper.read (instream);
			int w = imagewrapper.getwidth ();
			int h = imagewrapper.getheight ();
			int cw=w, ch=h,x=0,y=0;
			Boolean isdeal=true; if (height>h| |
			WIDTH&GT;W) {isdeal=false;
				}else if ((w-width) > (h-height)) {ch=h;
				cw= (h*width)/height;
				x= (W-CW)/2;
					if (cw>h) {cw=w; Ch=(w*height)/width;
					y= (H-CH)/2;
				x=0;
				}}else if ((w-width) <= (h-height)) {cw=w;
				Ch= (w*height)/width;
				y= (H-CH)/2;
					if (ch>h) {ch=h;
					cw= (h*width)/height;
					x= (W-CW)/2;
				y=0;
			}} System.out.println ("x:" +x+ "y" +y+ "cw:" +cw+ "ch" +ch+ ""); if (isdeal) {Cropparameter Cropparam = new Cropparameter (x, y, CW, ch);//cutting parameters Planarimage Planrimage = Imagecrophe
				Lper.crop (Imagewrapper.getasplanarimage (), Cropparam); Scaleparameter Scaleparam = new Scaleparameter (width, height, algorithm.lanczos);
				Scaling Parameters Planrimage = Imagescalehelper.scale (Planrimage, Scaleparam);
			Imagewrapper = new Imagewrapper (planrimage);
			}//4. Output OutStream = new FileOutputStream (out);
			String prefix = out.getname (). substring (Out.getname (). LastIndexOf (".") + 1);
		Imagewritehelper.write (Imagewrapper, OutStream, Outputformat.getimageformat (prefix), new Writeparameter ());
		} catch (IOException e) {e.printstacktrace (); } catch (Simpleimageexception e)

		{} finally {ioutils.closequietly (instream);//Picture file input/output stream must be remembered to close ioutils.closequietly (OutStream);
	}}/** * Compress the image to a specified size, the image is smaller than the target image (Watermark) * * @param src * @param target * @param width * @param height */ Public final static void Scalewithwatermark (string src, string target, int width, int height) {file out = new file (tar Get);
		Purpose picture FileOutputStream outstream = null; File in = new file (src);
		Original picture FileInputStream instream = null;
			try {instream = new FileInputStream (in);
			Imagewrapper imagewrapper = Imagereadhelper.read (instream);
			int w = imagewrapper.getwidth ();
			int h = imagewrapper.getheight ();
			int cw=w, ch=h,x=0,y=0;
			Boolean isdeal=true; if (height>h| |
			WIDTH&GT;W) {isdeal=false;
				}else if ((w-width) > (h-height)) {ch=h;
				cw= (h*width)/height;
				x= (W-CW)/2;
					if (cw>h) {cw=w;
					Ch= (w*height)/width;
					y= (H-CH)/2;
				x=0; }}else if ((w-width) <= (H-height)) {cw=w;
				Ch= (w*height)/width;
				y= (H-CH)/2;
					if (ch>h) {ch=h;
					cw= (h*width)/height;
					x= (W-CW)/2;
				y=0;
			}} System.out.println ("x:" +x+ "y" +y+ "cw:" +cw+ "ch" +ch+ ""); if (isdeal) {Cropparameter Cropparam = new Cropparameter (x, y, CW, ch);//cutting parameters Planarimage Planrimage = Imagecrophe
				Lper.crop (Imagewrapper.getasplanarimage (), Cropparam); Scaleparameter Scaleparam = new Scaleparameter (width, height, algorithm.lanczos);
				Scaling Parameters Planrimage = Imagescalehelper.scale (Planrimage, Scaleparam);
			Imagewrapper = new Imagewrapper (planrimage);
			}//3. Water stamp BufferedImage waterimage = Imageio.read (new File (Water_image_url));
			Imagewrapper waterwrapper = new Imagewrapper (waterimage); Point P =calculate (Imagewrapper.getwidth (), Imagewrapper.getheight (), Waterwrapper.getwidth (),
			Waterwrapper.getheight ());
			Watermarkparameter param = new Watermarkparameter (Waterwrapper, 1f, (int) p.getx (), (int) p.gety ()); BufferedImage BUFFeredimage = Imagedrawhelper.drawwatermark (Imagewrapper.getasbufferedimage (), param);
			Imagewrapper = new Imagewrapper (bufferedimage);
			4. Output OutStream = new FileOutputStream (out);
			String prefix = out.getname (). substring (Out.getname (). LastIndexOf (".") + 1);
		Imagewritehelper.write (Imagewrapper, OutStream, Outputformat.getimageformat (prefix), new Writeparameter ());
		} catch (IOException e) {e.printstacktrace (); } catch (Simpleimageexception e) {} finally {ioutils.closequietly (instream);//Picture file input/output stream must be remembered to close Ioutils.closequi

		etly (OutStream); }}/** * Zoom to the specified width height adaptive * * @param src * @param target * @param width */@SuppressWarnings ("static-access Public final static void Scalewithwidth (string src, string target, Integer width) {File out = new file (target);
		Purpose picture FileOutputStream outstream = null; File in = new file (src);
		Original picture FileInputStream instream = null;
			try {instream = new FileInputStream (in); Imagewrapper imageWrapper = Imagereadhelper.read (instream);
			int w = imagewrapper.getwidth ();
			int h = imagewrapper.getheight (); 1. Zoom Scaleparameter Scaleparam = new Scaleparameter (W, H, Algorithm.lanczos);
			Zoom parameter if (W < width) {//If the picture width and height are smaller than the target picture then do not zoom Scaleparam = new Scaleparameter (W, H, Algorithm.lanczos);
				} else {int newheight = getheight (W, h, width);
			Scaleparam = new Scaleparameter (width, newheight + 1, algorithm.lanczos);
			} planarimage planrimage = Imagescalehelper.scale (Imagewrapper.getasplanarimage (), Scaleparam);
			Imagewrapper = new Imagewrapper (planrimage);
			4. Output OutStream = new FileOutputStream (out);
			String prefix = out.getname (). substring (Out.getname (). LastIndexOf (".") + 1);
		Imagewritehelper.write (Imagewrapper, OutStream, Outputformat.getimageformat (prefix), new Writeparameter ());
		} catch (IOException e) {e.printstacktrace (); } catch (Simpleimageexception e) {} finally {ioutils.closequietly (instream);//Picture file input/output stream must beRemember to close ioutils.closequietly (OutStream); }}/** * Zoom to the specified height, width adaptive * * @param src * @param target * @param height */public final static void Scalew  Ithheight (string src, string target, Integer height) {file out = new File (target);//Destination picture FileOutputStream OutStream
		= NULL; File in = new file (src);
		Original picture FileInputStream instream = null;
			try {instream = new FileInputStream (in);

			Imagewrapper imagewrapper = Imagereadhelper.read (instream);
			int w = imagewrapper.getwidth ();
			int h = imagewrapper.getheight (); 1. Zoom Scaleparameter Scaleparam = new Scaleparameter (W, H, Algorithm.lanczos);

			Zoom parameter if (W < height) {//If the picture width and height are smaller than the target picture then do not scale processing Scaleparam = new Scaleparameter (W, H, Algorithm.lanczos);
				} else {int newwidth = getwidth (W, h, height);

			Scaleparam = new Scaleparameter (newwidth + 1, height, algorithm.lanczos);
			} planarimage planrimage = Imagescalehelper.scale (Imagewrapper.getasplanarimage (), Scaleparam); ImageWrapper = new Imagewrapper (planrimage);
			4. Output OutStream = new FileOutputStream (out);
			String prefix = out.getname (). substring (Out.getname (). LastIndexOf (".") + 1);
		Imagewritehelper.write (Imagewrapper, OutStream, Outputformat.getimageformat (prefix), new Writeparameter ());
		} catch (IOException e) {e.printstacktrace (); } catch (Simpleimageexception e) {} finally {ioutils.closequietly (instream);//Picture file input/output stream must be remembered to close Ioutils.closequi

		etly (OutStream); }}/** * Height based on width, height and target width * * @param w * @param h * @param width * @return */public static Integer G
	Etheight (integer w, integer h, integer width) {return h * width/w; /** * width, height, and target height, etc. * * @param w * @param h * @param height * @return */public static Integer GETW
	Idth (integer w, integer h, integer height) {return w * height/h; /** * Cut the required size from the middle * * @param src * @param target * @param width * @param height */@SuppressWarnings ("St Atic-acceSS ") public final static void Cutcenter (string src, string target, integer width, integer height) {file out = new file (target);
		Purpose picture FileOutputStream outstream = null; File in = new file (src);
		Original picture FileInputStream instream = null;
			try {instream = new FileInputStream (in);

			Imagewrapper imagewrapper = Imagereadhelper.read (instream);
			int w = imagewrapper.getwidth ();

			int h = imagewrapper.getheight ();

			int x = (w-width)/2;

			int y = (h-height)/2; Cropparameter Cropparam = new Cropparameter (x, y, width, height);//cutting parameters if (x < 0 | | Y < 0) {Cropparam = NE W cropparameter (0, 0, W, h);//Trimming parameters} planarimage planrimage = Imagecrophelper.crop (Imagewrapper.getasplanarimage (
			), Cropparam);
			Imagewrapper = new Imagewrapper (planrimage);
			4. Output OutStream = new FileOutputStream (out);
			String prefix = out.getname (). substring (Out.getname (). LastIndexOf (".") + 1); Imagewritehelper.write (Imagewrapper, OutStream, Outputformat.getImageFormat (prefix), new Writeparameter ());
		} catch (IOException e) {e.printstacktrace (); } catch (Simpleimageexception e) {} finally {ioutils.closequietly (instream);//Picture file input/output stream must be remembered to close Ioutils.closequi
		etly (OutStream); }}/** * cut to body shape * * @param src * @param target */public final static void Cut (string src, string target)
		{File out = new file (target);//Destination picture FileOutputStream OutStream = null; File in = new file (src);
		Original picture FileInputStream instream = null;
			try {instream = new FileInputStream (in);

			Imagewrapper imagewrapper = Imagereadhelper.read (instream);
			int w = imagewrapper.getwidth ();
			int h = imagewrapper.getheight ();
			int width = 0;

			int height = 0;
				if (w >= h) {width = h;
			Height = h;
				} else {width = w;
			Height = w; } cropparameter Cropparam = new Cropparameter (0, 0, width, height);//cutting parameters Planarimage Planrimage = Imagecrophelper.
	Crop (Imagewrapper.getasplanarimage (), Cropparam);		Imagewrapper = new Imagewrapper (planrimage);
			4. Output OutStream = new FileOutputStream (out);
			String prefix = out.getname (). substring (Out.getname (). LastIndexOf (".") + 1);
		Imagewritehelper.write (Imagewrapper, OutStream, Outputformat.getimageformat (prefix), new Writeparameter ());
		} catch (IOException e) {e.printstacktrace (); } catch (Simpleimageexception e) {} finally {ioutils.closequietly (instream);//Picture file input/output stream must be remembered to close Ioutils.closequi
		etly (OutStream); }} public static point calculate (int enclosingwidth, int enclosingheight, int width, int height) {int x = (Enclo
		SINGWIDTH/2)-(WIDTH/2);
		int y = (ENCLOSINGHEIGHT/2)-(HEIGHT/2);
	return new Point (x, y); }

}

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.