Java implementation of picture clipping preview function
In the project, we need to do something like Avatar upload, image clipping function, OK to see the article below!
plugins required:jQuery jcrop
Back-End Code:
Package org.csg.upload;import java.awt.rectangle;import java.awt.image.bufferedimage;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.util.iterator;import javax.imageio.imageio;import javax.imageio.imagereadparam;import Javax.imageio.imagereader;import javax.imageio.stream.imageinputstream;public class upload {/** * @author Legends of the night * @param path1 picture original path * @param path2 path to save after cropping * @param x x axis * @param y y Axis * @param w * @param h */public static void cutimage (string PATH1,STRING&NBSP;PATH2,INT&NBSP;X,INT&NBSP;Y,INT&NBSP;W,INT&NBSP;H) {Fileinputstream fileinputstream=null imageinputstream iis=null;try {//read image file, establish file input stream Fileinputstream=new fileinputstream (path1);// Create a picture of a file stream iterator iterator<imagereader>&Nbsp;it = imageio.getimagereadersbyformatname ("JPG"); Imagereader reader=it.next ();//Get Picture flow Establish text map file stream Iis=imageio.createimageinputstream (fileinputstream);//Get Picture default parameter Reader.setinput (iis, true); Magereadparam param=reader.getdefaultreadparam ();//define Clipping area rectangle rect=new rectangle (x,y,w,h); Param.setsourceregion (rect); Bufferedimage bi=reader.read (0,param); Imageio.write (bi, "JPG", new file (path2));} catch (exception e) {e.printstacktrace (); System.out.println ("cropping failed");} Finally{try {if (fileinputstream!=null) {fileinputstream.close ();} if (iis!=null) {iis.close ();}} catch (ioexception e) {e.printstacktrace ();}}}
Access code:
<%@ page language= "java" import= "java.util.*,org.csg.upload.*" pageencoding= "Utf-8"%><%//the relative path of the picture string Imagpath=request.getparameter ("Imgpath"); String Relpath=request.getrealpath ("/");//Get Picture server absolute address string newfilename=new Date (). GetTime () + ". jpg";// The actual picture path string path1=relpath+imagpath;//clipped after the picture path stored to the server string path2=relpath+ "/images/" +newfilename;int x= Integer.parseint (Request.getparameter ("X")); int Y=integer.parseint (Request.getparameter ("Y")); int w= Integer.parseint (Request.getparameter ("w")); int H=integer.parseint (Request.getparameter ("H")); try{ Upload.cutimage (path1, path2, X, Y, W, h); Out.print ("
JSP Code:
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%><! doctype html public "-//w3c//dtd html 4.01 transitional//en" >
:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6F/75/wKiom1Wc6TqRhmlXAAKKtcSO_6A619.jpg "title=" Qq20150708170347.png "alt=" Wkiom1wc6tqrhmlxaakktcso_6a619.jpg "/>
This article from "Promise always attached to the small wood 、、、" blog, please be sure to keep this source http://1936625305.blog.51cto.com/6410597/1672098
Java implementation of picture clipping preview function