Artikel ini membagikan kode spesifik SpringMVC yang dikombinasikan dengan JCrop untuk mewujudkan pemangkasan gambar untuk referensi Anda. Konten spesifiknya adalah sebagai berikut
1. JSP Halaman:
<Form name = "Form" action = "<%= request.getContextPath ()%>/unggahdemo/unggaheadimage" metode = "post" enctype = "multipart/form-data"> <div> <div> <Div> <input type = "file" name = "imgfile" id = "fcuLoad" slpeMe = "name =" this (IMGFILE "ID =" fCUPLOAD "" "name =" this (IMGFILE "ID =" fCUPLOAD "" id="cutimg"/> <input type="hidden" id="x" name="x"/> <input type="hidden" id="y" name="y"/> <input type="hidden" id="w" name="w"/> <input type="hidden" id="h" name="h"/> </div> </div> </div> <div> <button id="submit" OnClick = ""> Unggah </button> </div> </form>
2. Situasi Referensi Komponen JCrop:
<tautan rel = "stylesheet" href = "<c: nilai url ="/sumber daya/unggahplugin/css/jquery.jcrop.css "/>" type = "text/css"> </link> <script type = "text/javascript" src = "<c: url value = "/sumber daya/unggahplugin/skrip/jQuery-1.8.3.js"/> "> </script> <script type =" text/javascript "src =" <c: url value = "/sumber daya/unggah/skrip/jquery.jcrop.js"/"> </skrip>
3. Cara menggunakan jcrop
<script type = "text/javascript"> // Tentukan API global, yang lebih fleksibel untuk mengoperasikan var API = null; fungsi readurl (input) {if (input.files && input.files [0]) {var reader = new filereader (); reader.readasdataurl (input.files [0]); reader.onload = function (e) {$ ('#cutimg'). removeAttr ('src'); $ ('#cutimg'). attr ('src', e.target.result); API = $ .jcrop ('#cutimg', {setSelect: [20, 20, 200, 200], Aspectratio: 1, OnSelect: UpdateCoords}); }; if (API! = Undefined) {API.Destroy (); }} function updateCoords (obj) {$ ("#x"). val (obj.x); $ ("#y"). Val (obj.y); $ ("#w"). Val (obj.w); $ ("#h"). Val (obj.h); }; } </script> 4. Kode Latar Belakang:
@RequestMapping (value = "/unggaheadimage") string publik unggaheadimage (permintaan httpservletrequest, @RequestParam (value = "x") string x, @requestparam (value = "y") string y, @requestparam (value = "h") string h, @requestparam (value = "(value =" h ") string h, @requestparam (value =" (value = "h") h, @requestparam) (value = "(value =" h ") h, @requestparam (value =" w, "h") (value = "h, @requestparam (value =" h ") MultipartFile ImageFile) melempar Exception {System.out.println ("============================================================================================================================================================== "Sumber daya/unggahan/"; fileName.substring(0,end); File dir = new File(realPath + resourcePath); if(!dir.exists()){ dir.mkdirs(); } File file = new File(dir,saveName+"_src.jpg"); imageFile.transferTo(file); String srcImagePath = realPath + resourcePath + saveName; int imageX = Integer.parseint (x); int imageH = integer.parseint (h); int imagew = integer.parseint (w); // 这里开始截取操作 System.out.println ("========== ImagecutStart =============="); Imagecut.imgcut (srcimagePath, ImageX, Imagey, Imagew, ImageH); System.out.println ("========== Imagecutend =============="); }} return "user/unggah/tes"; } 5. Imagecut.java Kelas Alat:
/** * Intercepted image* @param srcImageFile Original image address* @param x x coordinates during intercepting* @param y y coordinates during intercepting* @param desWidth Intercepted width* @param desHeight Intercepted height*/ public static void imgCut(String srcImageFile, int x, int y, int desWidth, int desHeight) { try { Image IMG; Imagefilter cropfilter; BufferedImage Bi = ImageIo.read (file baru (srcimageFile+"_ src.jpg")); int srcwidth = bi.getWidth (); int srcheight = bi.getHeight (); if (srcwidth> = deswidth && srcheight> = desheight) {gambar gambar = bi.getscaledInstance (srcwidth, srcheight, image.scale_default); cropfilter = cropimagefilter baru (x, y, deswidth, desheight); img = toolkit.getDefaultToolKit (). createImage (new filteredImageSource (image.getSource (), cropFilter)); BufferedImage Tag = BufferedImage baru (Deswidth, Desheight, bufferedImage.type_int_rgb); Grafik g = tag.getGraphics (); G.DrawImage (IMG, 0, 0, NULL); g.dispose (); // file output Imageo.write (tag, "jpeg", file baru (srcimageFile+"_ cut.jpg")); }} catch (Exception e) {e.printstacktrace (); }} 6. Dua cara untuk menggunakan jcrop:
1.
jQuery ('#cropbox'). jcrop ({Onchange: showCoords, onSelect: showCoords});2.
var API = $ .jcrop ('#cropbox', {onchange: showpreview, onSelect: showpreview, aspectratio: 1});Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.