Online drawing plug-in -- practical application of wPaint

Source: Internet
Author: User

 


In practical application, this is a good jQuery plug-in for online real-time drawing.

One of the most important functions is to save painted images.

 


An implemented demo (JAVA ).

 


1. Define a wPaint container on the HTML page.


[Html]
<Div id = "wPaint"> </div>

<Div id = "wPaint"> </div> it is best to set a style for the container.


[Html]
# WPaint {
Position: relative;
Width: 680px;
Height: 600px;
Background: # CACACA;
Border: solid black 1px;
Margin-bottom: 10px;
Margin-top: 10px;
Margin-left: 5px;
Float: left;
}

# WPaint {
Position: relative;
Width: 680px;
Height: 600px;
Background: # CACACA;
Border: solid black 1px;
Margin-bottom: 10px;
Margin-top: 10px;
Margin-left: 5px;
Float: left;
} Introduce JS and CSS.


[Html]
<! -- WColorPicker -->
<Link rel = "Stylesheet" type = "text/css" href = "/js/jquery/wPaint/inc/wColorPicker.css"/>
<Script type = "text/javascript" src = "/js/jquery/wPaint/inc/wColorPicker. js"> </script>
 
<! -- WPaint -->
<Link rel = "Stylesheet" type = "text/css" href = "/js/jquery/wPaint/wPaint.css"/>
<Script type = "text/javascript" src = "/js/jquery/wPaint. js"> </script>

<! -- WColorPicker -->
<Link rel = "Stylesheet" type = "text/css" href = "/js/jquery/wPaint/inc/wColorPicker.css"/>
<Script type = "text/javascript" src = "/js/jquery/wPaint/inc/wColorPicker. js"> </script>

<! -- WPaint -->
<Link rel = "Stylesheet" type = "text/css" href = "/js/jquery/wPaint/wPaint.css"/>
<Script type = "text/javascript" src = "/js/jquery/wPaint. js"> </script>

 

2. initialize wPaint. JS Code


[Html]
$ ('# WPaint'). wPaint ({
FillStyle: "# ffffff ",
Image: "<STRONG >$ {dataDto. base64Image} </STRONG> ",
StrokeStyle: "#333333"
});

$ ('# WPaint'). wPaint ({
FillStyle: "# ffffff ",
Image: "$ {dataDto. base64Image }",
StrokeStyle: "#333333"
}); Black part [html] view plaincopyprint?
$ {DataDto. base64Image}

$ {DataDto. base64Image} is the background image data of the drawing container (in png format) and base64 format.


[Html]
Data: image/png; base64, iVBORw0KGgoAAAANSUhEUgAAAfQAAAGQCAYAAABY...

Data: image/png; base64, iVBORw0KGgoAAAANSUhEUgAAAfQAAAGQCAYAAABY...
If you do not need a background, you do not need to set it.

In JAVA, we can use the BASE64Encoder class for conversion.


[Html]
Public static String imageToBase64 (byte [] imageBytes ){
BASE64Encoder encoder = new BASE64Encoder ();
Return encoder. encode (imageBytes );
}

Public static String imageToBase64 (byte [] imageBytes ){
BASE64Encoder encoder = new BASE64Encoder ();
Return encoder. encode (imageBytes );
}
3. After creating an image, you need to save it.

3.1 obtain the image attribute value of wPaint to obtain the image data in base64 format.

 
Var imageData = $ ("# wPaint"). wPaint ("image ");

Var imageData = $ ("# wPaint"). wPaint ("image ");
3.2 In the JAVA background, you can use the BASE64Decoder class to convert base64 data into byte arrays.


[Html]
Public static byte [] base64ToBytes (String bast64Str ){
BASE64Decoder decoder = new BASE64Decoder ();
Try {
Byte [] bytes = decoder. decodeBuffer (bast64Str );
// Adjust abnormal data
For (int I = 0; I <bytes. length; ++ I ){
If (bytes [I] <0 ){
Bytes [I] + = 256;
}
}
Return bytes;
} Catch (IOException e ){
Throw new IllegalStateException ("Convert base64 data error", e );
}
}

Public static byte [] base64ToBytes (String bast64Str ){
BASE64Decoder decoder = new BASE64Decoder ();
Try {
Byte [] bytes = decoder. decodeBuffer (bast64Str );
// Adjust abnormal data
For (int I = 0; I <bytes. length; ++ I ){
If (bytes [I] <0 ){
Bytes [I] + = 256;
}
}
Return bytes;
} Catch (IOException e ){
Throw new IllegalStateException ("Convert base64 data error", e );
}
}
OK. Now it is finished.

 

 
 

 

 

 

 


 

Related Article

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.