HTML 5 Canvas element in the increase, with JS flexible syntax, the picture processing from easy, with no need to write a lot of code on the client using C / C ++, for those familiar with the JS programmers only need to consider treatment the logic of the picture.
If you want the canvas with pictures ImageData needs to use this object, the canvas is to a certain area of the image to RGBA way preserved, saved as a two-dimensional array.
- ctx.getImageData (X, Y, W, H) / / acquired ImageData
- ctx.putImageData (X, Y, W, H) / / a is plotted on the canvas ImageData
Write a simple processing based image may be flipped / ashing / to color / highlight / monochrome setting value
- / **
- * @ Author Norris Hall
- * /
- Var PS = function (configuration) {
- / / $ Extension (this configuration);
- Return this;
- }
- PS.prototype = {
- / / Image ashing
- Gray: Function (CTX, as imageData) {
- 无功 W = imageData.width,
- H = imageData.height,
- RET = ctx.createImageData(W,H);
- For (I = 0; I
- {
- It is (J = 0; Ĵ
- {
- Var index = (I * High + J) * 4;
- Reactive Red = imageData.data [index];
- Var green = imageData.data [index 1];
- Reactive Blue = imageData.data [index 2];
- Reactive letters = imageData.data [index 3];
- Average = var (Red + Green + Blue) / 3;
- ret.data [Index] = average;
- ret.data [index 1] = average;
- ret.data [index 2] = average;
- ret.data [index 3] = alpha;
- }
- }
- Return RET;
- },
- / / Generated ImageData
- The createImageData: function (CTX, ORI, from, W, H) {
- 无功 RET = ctx.createImageData(W,H);
- Total reactive = W * H * 4;
- = W * from the * 4;
- Is (I = 0 var I "always, i ++) {
- ret.data reason [i] = ori.data [从 + I];
- }
- Return RET;
- },
- / / Generated ImageData
- / / Image reversal symmetry
- createImageDataTurn: function (CTX, ORI, from, W, H) {
- 无功 RET = ctx.createImageData(W,H);
- Total reactive = W * H * 4;
- = W * from the * 4;
- It is (var J = 0; Ĵ
- As (var I = 0; I
- Var A = (ten * W + I) * 4,
- From B = + A,
- C = (ten * W + W-1) * 4;
- ret.data [C + +] = ori.data [B + +];
- ret.data [C + +] = ori.data [B + +];
- ret.data [C + +] = ori.data [B + +];
- ret.data [C + +] = ori.data [B + +];
- }
- }
- Return RET;
- },
- / / The whole picture as a color value
- setColorR: function (CTX, as in imageData, n) {
- 无功 W = imageData.width,
- H = imageData.height,
- RET = ctx.createImageData(W,H);
- Total reactive = W * H * 4;
- As (var I = 0; I
- ret.data a [i] = N; / / to imageData [I];
- ret.data [I +1] = imageData.data reason [i +1];
- ret.data [I 2] = imageData.data [I + 2];
- ret.data [I +3] = imageData.data [I + 3];
- }
- Return RET;
- },
- / / The whole picture as a color value
- setColorG: function (CTX, as in imageData, n) {
- 无功 W = imageData.width,
- H = imageData.height,
- RET = ctx.createImageData(W,H);
- Total reactive = W * H * 4;
- As (var I = 0; I
- Reactive Red = imageData.data [I],
- Green = imageData.data [I +1],
- Blue = imageData.data a [i +1];
- Var A = (Red + Green + Blue) / 3;
- ret.data reason [i] = A;
- ret.data reason [i +1] = A + N;
- ret.data [I 2] = a;
- ret.data [I +3] = imageData.data [I + 3];
- }
- Return RET;
- },
- / / The whole picture as a color value
- setColorB: function (CTX, as in imageData, n) {
- 无功 W = imageData.width,
- H = imageData.height,
- RET = ctx.createImageData(W,H);
- Total reactive = W * H * 4;
- As (var I = 0; I
- ret.data reason [i] = imageData.data reason [i];
- ret.data [I +1] = imageData.data reason [i +1];
- ret.data [I 2] = N;
- ret.data [I +3] = imageData.data [I + 3];
- }
- Return RET;
- },
- / / To highlight the entire picture
- Projection: function (CTX, as imageData, N) {
- 无功 W = imageData.width,
- H = imageData.height,
- RET = ctx.createImageData(W,H);
- Total reactive = W * H * 4;
- As (var I = 0; I
- ret.data reason [i] = imageData.data reason [i] + N;
- ret.data reason [i +1] = imageData.data reason [i +1] + N;
- ret.data reason [i +1] = imageData.data [I + 2] + N;
- ret.data [I +3] = imageData.data [I + 3];
- }
- Return RET;
- },
- / / Purple color to 247,0,255
- removeColor: function (CTX, as imageData, R, G, B) {
- 无功 W = imageData.width,
- H = imageData.height,
- RET = ctx.createImageData(W,H);
- Total reactive = W * H * 4;
- As (var I = 0; I
- Reactive Red = imageData.data [I],
- Green = imageData.data [I +1],
- Blue = imageData.data a [i +1];
- / / Are equal transparent
- If the correlation (R & lt == == g && && red green blue == b) {
- ret.data reason [i +3] = 0;
- } Else {
- ret.data a [i] = red;
- ret.data a [i +1] = green;
- ret.data a [i +1] = blue;
- ret.data [I +3] = imageData.data [I + 3];
- }
- }
- Return RET;
- }
- };
- The PS PS = new ();
Through a series of operations, after rendering a good image, we need to use the following code saves the image to the canvas picture
- / / Outputs the image of the compressed base64 string Default image / PNG
- Var data = canvas.toDataURL ();
- / / Message before deleting a string "data: image / PNG; the base64"
- Variant B64 = data.substring (22);
- / / POST to the server, generates pictures
- After $ ( "save.aspx", {data: B64, name: filename}, function () {
- / / OK
- });
Create a front-end learning qun438905713, most in the group are zero-based learners, we help each other, answer each other, and also to prepare a lot of learning materials, welcomed the zero-based junior partner to the exchange.
save.aspx code as follows:
- Protected void of Page_Load (object sender, EventArgs sent)
- {
- If (Request [ "name"]! = Null)
- {
- Request String name = [ "name"];
- Use string SAVEPATH = Server.Mappath ( "~ / video / output /");
- try
- {
- The FileStream FS = File.Create (SAVEPATH + "/" + name);
- Byte [] bytes = Convert.FromBase64String (Request [ "data"]);
- fs.Write (bytes, 0, bytes.Length);
- fs.Close();
- }
- Catch (subject to exceptions)
- {
- }
- }
- }
PS: Due to limitations of the sandbox, want to deposit directly to the local picture by JS on the browser side, it seems unlikely that, now more eclectic way for online
window.location.href = "images / octet-stream" data +
However, this method can not specify where to save the file name, the default in the FF is xxxxx.part