Js+java-Cut Drawing

Source: Internet
Author: User

This article mainly introduces the Java level of the process of cropping the picture, the following instances are not animated, such as animation and other requirements can be used Jcrop

Jcrop Address: http://deepliquid.com/content/Jcrop.html

The concrete examples are as follows:

Take the photo, start with (0,0) coordinates, take the shorter edge as a square edge, and intercept the largest square.

JS Code:

function Cutimage (name) {//name is the cropped picture name
var image = new Image ();
IMAGE.SRC = Imgurl + "/" + name; Picture full path
Image.onload = function () {
var s;
if (image.width <= image.height) {
s = image.width;
} else {
s = image.height;
}
var param = {};
param.x = 0;
PARAM.Y = 0;
PARAM.W = s;
Param.h = s;
Param.bigimage = IMAGE.SRC;
$.ajax ({
Url:config. Server_url + '/common/cutimage ',
Type: ' POST ',
ContentType: ' application/x-www-form-urlencoded ',
Data:param,
Success:function (E, data) {
if (e.status! = 200) {
Alert ("Upload failed");
Return
}
Alert ("Upload success");
},
Error:function (e) {
Alert ("Upload failed");
}
});
}
}

Background Java:

@RequestMapping (value = "/cutimage", method = requestmethod.post, consumes =mediatype.application_form_urlencoded_value)/** "Crop Picture"
* Bigimage: Need to crop the picture path
* x: Coordinate x-axis start
* Y: Coordinates y-axis starting point
* W: Capture width
* H: Intercept height
*/ PublicSimpleresponse Cutimage (@RequestParam ("Bigimage") String Bigimage, @RequestParam ("X") String x, @RequestParam ("Y") string y, @RequestParam ("W") string W, @RequestParam ("H") (String h) {File file=NULL; Try { intXint =integer.valueof (x); intYint =integer.valueof (y); intWInt =integer.valueof (W); intHInt =integer.valueof (h); //official path to fileString ImagePath =Bigimage; //Cutting PicturesImagecut Imagecut =Newimagecut (); File=imagecut.cutimage (ImagePath, Xint, Yint, WInt, hInt); byte[] bytes =Fileutils.readfiletobytearray (file); //Upload a trimmed file returnuploadpicture (bytes); } Catch(Exception e) {Throw Newliveexception (Livecode.file_upload_error); } finally{fileutils.deletequietly (file); } }

Tool class:

 Packagecom.utils;ImportJavax.imageio.ImageIO;Importorg.apache.commons.io.FileUtils;Importorg.apache.commons.io.IOUtils;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.util.LinkedMultiValueMap;ImportOrg.springframework.util.MultiValueMap;ImportJava.awt.Graphics;ImportJava.awt.Image;ImportJava.awt.Toolkit;ImportJava.awt.image.BufferedImage;ImportJava.awt.image.CropImageFilter;ImportJava.awt.image.FilteredImageSource;ImportJava.awt.image.ImageFilter;ImportJava.io.ByteArrayOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.net.URL;Importjava.net.URLConnection; Public classImagecut {/*** Picture Cutting *@paramImagePath Original Address *@paramx target slice coordinate x-axis start *@paramY- target slice coordinate y-axis start *@paramw Target Slice width *@paramh target Slice height*/       PublicFile cutimage (String ImagePath,intXintYintWinth) {File File=NULL; File ImageFile=NULL; Try{Image img;              ImageFilter Cropfilter; ImageFile=LoadFile (ImagePath); Because the image address is a URL, you need to download the image to a local//Read source imageBufferedImage bi =Imageio.read (ImageFile); intSrcwidth = Bi.getwidth ();//Source Graph Width            intSrcheight = Bi.getheight ();//Source Graph Height//Wakahara the size of the chart is larger than the slice size, cut            if(Srcwidth >= w && srcheight >=h) {image Image=bi.getscaledinstance (Srcwidth, Srcheight,image.scale_default); Cropfilter=Newcropimagefilter (x, Y, W, h); IMG= Toolkit.getdefaulttoolkit (). CreateImage (NewFilteredImageSource (Image.getsource (), cropfilter)); BufferedImage Tag=NewBufferedImage (w, H,BUFFEREDIMAGE.TYPE_INT_RGB); Graphics g=Tag.getgraphics (); G.drawimage (IMG,0, 0,NULL);//draw a zoomed-out diagramG.dispose (); //Output to FileFile =NewFile (System.getproperty ("User.dir") + "final" + system.currenttimemillis () + ". jpg"); Using timestamps as filenames avoids concurrency problems imageio.write (tag,"JPEG", file); }          } Catch(IOException e) {e.printstacktrace (); }finally{fileutils.deletequietly (imagefile); }        returnfile; }           PublicFile loadFile (String imagePath) {urlconnection con=NULL; OutputStream OS=NULL; InputStream is=NULL; File File=NULL; Try {            //construct URLURL url =NewURL (ImagePath); //Open ConnectionCon =url.openconnection (); //input Streamis =Con.getinputstream (); //1K of data buffering            byte[] bs =New byte[1024]; //length of data read            intLen; File=NewFile (System.getproperty ("User.dir") + "template" + system.currenttimemillis () + ". jpg"); //the output file streamOS =Newfileoutputstream (file); //start reading             while(len = Is.read (BS))! =-1) {os.write (BS,0, Len);        } os.flush (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{            //complete, close all linksioutils.close (con);            Ioutils.closequietly (IS);        ioutils.closequietly (OS); }                returnfile; }    }

Js+java-Cut Drawing

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.