Html2canvas uploading images to the server (Java)

Source: Internet
Author: User

Function: For some pages of the chat class, the chat record is sent to the server side.

The JS frame used:

Imgareaselect

http://odyniec.net/projects/imgareaselect/

Html2canvas

http://html2canvas.hertzen.com/

Use the Imageareaselect plugin to generate Cancas and upload content to the server using Html2cancas

Interface:


Front-end JS script

$ (function () {var _width = 0;var _height = 0;var _bordercss;function imagecutcancel () {alert (_BORDERCSS)}$ ("#btnCut"). On (' click ', Function () {_BORDERCSS = $ (' #photo01 '). Get (0). style.border;$ (' #photo01 '). CSS (' border ', ' 1px solid #FF0000 ')        . Imgareaselect ({handles:true, Onselectend:function (IMG, selection) {_width = Selection.width;        _height = Selection.height; $ (' input[name= ' x1 "]). Val (selection.x1); $ (' Input[name=" y1 "] '). Val (selection.y1); $ (' Input[name=" x2 "] '). Val (    SELECTION.X2); $ (' Input[name= "y2"] '). Val (selection.y2);        }    }); }); $ ("#btnCancel"). On (' click ', Function () {_width = 0;_height = 0;$ (' #photo01 '). CSS (' border ', _bordercss). Imgareaselect ({remove:true});}); $ ("#btnOk"). On (' click ', function () {if (_width<10 && _height<10) return False;html2canvas ($ (' div# Photo01 '), {onrendered:function (canvas) {var imgcxt = Canvas.todataurl (); $.post (' ${pagecontext.request.contextpath} /image/upload.do ', {"Data": Imgcxt}, Function (JSON) {alert (JSON.MSG);}, ' json ');d ocument.body.appendChild (canvas);},width: _width,height: _height}); $ ("#btnCancel"). Click ();});}); 
Java Script

@Controller @requestmapping ("/image") public class imagecontroller{@RequestMapping ("/upload") public void Uplod ( HttpServletRequest request, HttpServletResponse response) {String data = Request.getparameter ("data"); int result = 1; String msg = "Upload succeeded"; Base64 base64 = new Base64 ()//base64 decode imagebyte[] b = Base64.decode (Data.substring ("Data:image/png;base64,". Length ()). GetBytes ()); String fileName = string.valueof (System.currenttimemillis ()),//image pathstring FilePath = System.getproperty (" Zokee.root ") + File.separator +" DATAS "+ file.separator + fileName +". png ";//write imagefile file = new file (filePath); t ry {fileutils.writebytearraytofile (file, b);} catch (IOException e) {result = 0;msg = "Upload Failed"; E.printstacktrace ();} Jsonobject json = new Jsonobject (); Json.accumulate ("result", result); Json.accumulate ("msg", MSG); Response.setcontenttype ("Text/json"); Response.setcharacterencoding ("Utf-8"); try {printwriter out = Response.getwriter (); Out.print (json.tostring ());} catch (IOException e) {e.Printstacktrace ();}}} 

Reference article: http://leobluewing.iteye.com/blog/2020145


Html2canvas uploading images to the server (Java)

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.