In the project, we need to do some avatars uploading, picture cutting functions, OK read the following article!
I need plug -in: jquery jcrop
Back -end code:
package org.csg.upload; Import Java.awt.Rectangle; Import Java.awt.image.buffRedimage; Import Java. File; Import java.fileInputStream; I mport java.io.ioException; Import Java.util.itrator ; Import javax.imageio.imageio; Import Javax.imageio.imageAdParam; Import Javax.imageio.imageReader; Import javax.imageio. Stream.imageInp UTSTREAM; Public Class upload { /*** @Author Xiaoye's legend* @param Path1 picture original Path* @Param Path2 The path stored after cutting* @Param XX shaft* @param yy shaft* @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 the picture files, create file input stream FileInputStream = New FileInputStream (Path1); // Create a file file file file Iterator <IMageReader> IT = Imageio.getImageReadersByFormatname ("JPG"); Imagereader Reader = it.next (); // Get the picture stream to establish a text chart file flow iIS = Imageio.CreateImageInputStream (FileInputStream) ; // Get the picture default parameter Reader.Setinput (IIS, TRUE ).; edimage bi = Reader.read (0, param); Imageio.Write (BI, "JPG", New File (PATH2));} Catch (Exception E) {e.printstacktrace (); System.out.println ("Cutting Failure");} Finally {try (file InputStream ! = Null) {fileinputstream.close ();} if (is! = Null) {iis.close ();}} Catch (IOEXCETION E) {e.printstacktrace ();}}}}}}}}}}}}}}
Access code:
<%@page Language = "Java" Import = "Java.util.*, ORG.CSG.UPLOAD.*" Pageencoding = "UTF-8"%> <%// The relative path string ER (ER "IMGPATH"); String RELPATH = Request.getRealpath ("/"); // Get the picture server absolute address string newfilename = new date (). Gettime ()+". JPG"; inh +Imagpath; // The picture path of storage to the server after cutting string path2 = RELPATH+"/Images/"+NewFilename; int x = Integer.parseint ("Request.GetParameter (" X ")); nteger.parseint (request .getParameter ("y"); int W = Integer.parseint (request.getparameter ("w"); int h = Integer.parseint (request.getparameter ("h"); try {upload.c.c Utimage (PATH1 , PATH2, X, Y, W, H); Out.print ("<IMG SRC = 'Images/"+NewFilename+"'/>");} Catch (Exception E) {e.printstacktrace (); out.print ("Pictures Failure Failure");}%>
JSP code:
<%@ page language = "java" image = "java.util.*" pageEncoding = "UTF-8"%> <! Doctype html public "-// w3c // dtd html 4.01 ml > <Head> <Title> JSP Develop Avatar Cutting </Title> <Meta Http-Equiv = "Pragma" Content = "No-Cache"> <meta Http-Equiv = "Cache-Control" Content = "No-Cache" > <meta http-equiv = "expires" content = "0"> <meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3"> <meta http-equiv = "description pTion "content =" this is my my Page "> <link rel =" styleSheet "href =" css/jquery.jcrop.css "type =" text/css "/> <script type =" text/javascript "src =" js/jquery.min.js " > </script> <script type = "text/javascript" src = "js/jquery.jcrop.min.js"> </script> <alle type = "text/css"> *{margin: 0; padding: 0;} .cut {margin-top: 20px;} #Preview-PANE {Display: Block; Position: AbSolute; Z-index: 2000; TOP: 10px; Right: -280px; Padding: 6px; Bord ER: 1px RGBA ( 0,0,0, .4) Solid; Background-COLOR: White; -webkit-Border-Radius: 6px; -moz-Border-Radius: 6px; Border-Radius: 6px; x 1px 5px 2px RGBA (0, 0, 0, 0.2); -moz-Box-shadow: 1px 1px 5px 2px RGBA (0, 0, 0, 0.2); , 0.2);} #Preview-Pane .preview-Container {width: 250px; height: 170px; overflow: hidden;} </style> <script type = "text/javascript"> nction () {var jcrop_api, Boundx = "", Boundy = "", $ Preview = $ ('#Preview-Pane'), $ pcNT = $ ('#Preview-PANE .preview-Container'), $ PIMG = $ ('#Preview-Pane .preview-controlainer img '), xsize = $ pcnt.width (), ysize = $ pcnt.head (); $ ('#cutimage '). onSelect: ShowCoords , // Get the dragging value aspectratio: xsize/ysize}, function () {var bounds = this.getbounds (); boundx = bounds [0]; = this; $ preview.appendto (Jcrop_api.ui.holder);}); Function showcoords (c) {var x = cx; var y y = cy; var w = cw; var h = ch; $ ("#x1"). Valit (x ); $ ("#y1"). Val (PARSEINT (y)); $ ("#w"). Val (PARSEINT (w))). ; if (PARSEINT (CW)> 0) {var rx = xsize / cw; var ry = ysize / ch; $ pimg.css ({width: math.round (rx * boundx) + 'px', height: math. Round (Ry * Boundy) + 'PX', Marginleft: '-' + Math.round (RX * CX) + 'PX', Margintop: ' + Math.Round (Ry * Cy) +' PX '}) ;}}}); </SCRIPT> </Head> <body> <H1> Java development QQ avatar cutting system </h1> <img ID = "cutimage" src = "images/1.jpg"> <div ID = "Preview-Pane"> <DIV> <IMG SRC = "Images/1.jpg"/> </DIV> </DIV> </DIV> <Form Action = "Success.jsp" Method = " Post "> <input type =" text "value =" Images/1.jpg "name =" IMGPATH "> X axis: <input type =" text "size =" 4 "id =" x1 "name =" x " /> y axis: <input type = "text" size = "4" id = "y1" name = "y"/> Width: <input type = "text" size = "4" id = "w" name = "w"/> height: <input type = "text" size = "4" id = "h" name = "h"/> <input type = "submit" value = "cut"/> </form> < /body> </html>
Effect map:
The above is all the contents of this article. I hope everyone can like it.