Java supports image cropping and preview.
In the project, we need to do something similar to the Avatar upload and image cropping functions. OK, read the following article!
Plug-ins required:JQuery Jcrop
Backend 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 the legend of the night * @ param path1 original path * @ param path2 the path stored after cropping * @ param x axis * @ param y axis * @ param w * @ param h */public static void CutImage (String path1, string path2, int x, int y, int w, int h) {FileInputStream fileInputStream = null; ImageInputStream iis = null; try {// read image files, create a file input stream fileInputStream = new FileInputStream (path1); // create an image file stream Iterator
It = ImageIO. getImageReadersByFormatName (jpg); ImageReader reader = it. next (); // obtain the image stream to create the image file stream iis = ImageIO. createImageInputStream (fileInputStream); // gets the default image parameter reader. setInput (iis, true); ImageReadParam param = reader. getDefaultReadParam (); // defines the cropping 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 %> <% // relative path of the image String imagPath = request. getParameter (imgPath); String relPath = request. getRealPath (/); // obtain the absolute address of the image Server String newFileName = new date().gettime()).jpg; // The actual image path String path1 = relPath + imagPath; // specifies the image path 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 (
);} Catch (Exception e) {e. printStackTrace (); out. print (image cropping failed);} %>
Jsp code:
<%@ page language=java import=java.util.* pageEncoding=utf-8%>
<Script type = text/javascript src = js/jquery. min. js> </script> <script type = text/javascript src = js/jquery. jcrop. min. js> </script> <Script type = text/javascript> $ (function () {var jcrop_api, boundx =, boundy =, $ preview = $ ('# preview-pane '), $ pcnt = $ ('# preview-pane. preview-iner '), $ pimg = $ (' # preview-pane. preview-container img '), xsize = $ pcnt. width (), ysize = $ pcnt. height (); $ ('# cutimage '). jcrop ({onChange: showCoords, // obtain the selected value onSelect: showCoords, // obtain the drag value aspectRatio: xsize/ysize}, function () {var bounds = this. getBounds (); boundx = bounds [0]; boundy = bounds [1]; jcrop_api = this; $ preview. appendTo (jcrop_api.ui.holder) ;}); function showCoords (c) {var x = c. x; var y = c. y; var w = c. w; var h = c. h; $ (# x1 ). val (parseInt (x); $ (# y1 ). val (parseInt (y); $ (# w ). val (parseInt (w); $ (# h ). val (parseInt (h); if (parseInt (c. w)> 0) {var rx = xsize/c. w; var ry = ysize/c. h; Define pimg.css ({width: Math. round (rx * boundx) + 'px ', height: Math. round (ry * boundy) + 'px ', marginLeft:'-'+ Math. round (rx * c. x) + 'px ', marginTop:'-'+ Math. round (ry * c. y) + 'px '}) ;}}}); </script> Java development QQ Avatar Cropping System
Well, let's look at the source code below?