zoukankan      html  css  js  c++  java
  • 图片的模糊程度与马赛克的制作

    <!DOCTYPE html>

    <html>

    <head>

    <meta charset="UTF-8">

    <title>canvasMAP</title>

    <style type="text/css">

    #canvas1 {

    border: 2px solid darkgoldenrod;

    }

    #canvas2 {

    border: 2px solid darkgoldenrod;

    }

    </style>

    </head>

    <canvas id="canvas1" width="400" height="400"></canvas>

    <canvas id="canvas2" width="400" height="400"></canvas>

    <butoon id="btn">按钮 </butoon>

    <button id="mohu">模糊</button>

    <button id="mask">马赛克</button>

    //<!--本身=(本身+周围)/总数

    //本身=平均值--> 

    <body>

    </body>

    <script type="text/javascript">

    var canvas1 = document.getElementById("canvas1");

    var canvas2 = document.getElementById("canvas2");

    var context1 = canvas1.getContext("2d");

    var context2 = canvas2.getContext("2d");

    var cw = canvas1.width;

    var ch = canvas2.height;

    //模糊按钮

    mohu.onclick = function() {

    var xunhuanImgData = context1.getImageData(0, 0, 400, 400);

    var xunhuanPxData = xunhuanImgData.data;

    //循环的时候用来修改的

    var xiugaiImgData = context1.getImageData(0, 0, 400, 400);

    var xiugaiPxData = xiugaiImgData.data;

    var quan = 10;

    for(var i = quan; i < cw - quan; i++) {

    for(var j = quan; j < ch - quan; j++) {

    var p = j * cw + i;

    var sumR = 0;

    var sumG = 0;

    var sumB = 0;

    for(var x = -quan; x <= quan; x++) {

    for(var y = -quan; y <= quan; y++) {

    var xx = i + x;

    var yy = j + y;

    var pp = yy * cw + xx;

    sumR += xunhuanPxData[pp * 4 + 0];

    sumG += xunhuanPxData[pp * 4 + 1];

    sumB += xunhuanPxData[pp * 4 + 2];

    }

    }

    var totla1 = (2 * quan + 1) * (2 * quan + 1);

    var avgR = sumR / totla1;

    var avgG = sumG / totla1;

    var avgB = sumB / totla1;

    var p = j * cw + i;

    xiugaiPxData[p * 4 + 0] = avgR;

    xiugaiPxData[p * 4 + 1] = avgG;

    xiugaiPxData[p * 4 + 2] = avgB;

    }

    }

    context2.putImageData(xiugaiImgData, 0, 0, 0, 0, 400, 400);

    }

    //马赛克按钮

    mask.onclick = function() {

    var xunhuanImgData = context1.getImageData(0, 0, 400, 400);

    var xunhuanPxData = xunhuanImgData.data;

    //循环的时候用来修改的

    var xiugaiImgData = context1.getImageData(0, 0, 400, 400);

    var xiugaiPxData = xiugaiImgData.data;

    var quan = 5;

    for(var i = 50; i < 200; i = i + 2 * quan + 1) {

    for(var j = 50; j < 200; j = j + 2 * quan + 1) {

    var p = j * cw + i;

    var sumR = 0;

    var sumG = 0;

    var sumB = 0;

    for(var x = -quan; x <= quan; x++) {

    for(var y = -quan; y <= quan; y++) {

    var xx = i + x;

    var yy = j + y;

    var pp = yy * cw + xx;

    sumR += xunhuanPxData[pp * 4 + 0];

    sumG += xunhuanPxData[pp * 4 + 1];

    sumB += xunhuanPxData[pp * 4 + 2];

    }

    }

    var totla1 = (2 * quan + 1) * (2 * quan + 1);

    var avgR = sumR / totla1;

    var avgG = sumG / totla1;

    var avgB = sumB / totla1;

    for(var x = -quan; x <= quan; x++) {

    for(var y = -quan; y <= quan; y++) {

    var xx = i + x;

    var yy = j + y;

    var pp = yy * cw + xx;

    xiugaiPxData[pp * 4 + 0] = avgR;

    xiugaiPxData[pp * 4 + 1] = avgG;

    xiugaiPxData[pp * 4 + 2] = avgB;

    xiugaiPxData[pp * 4 + 3] = 122;

    }

    }

    }

    }

    context2.putImageData(xiugaiImgData, 0, 0, 0, 0, 400, 400);

    }

    var fengjing = new Image();

    fengjing.src = "img/fengjing.jpg";

    fengjing.onload = function() {

    context1.drawImage(this, 0, 0, 400, 400);

    }

    btn.onclick = function() {

    //获取cavas1  中的图片数据  (imgdate)

    var imgDate = context1.getImageData(0, 0, 400, 400);

    //从 imgdate 中获取像素数据 (pxDAte)

    var pxDate = imgDate.data;

    var cw = 400;

    var ch = 400;

    // 修改像素

    for(var i = 0; i < cw; i++) {

    for(var j = 0; j < ch; j++) {

    var p = (j * cw) + i;

    pxDate[i * 4 + 0] = 255;

    var x = Math.random();

    if(x < 0.1) {

    pxDate[p * 4 + 0] = 255;

    pxDate[p * 4 + 0] = 255;

    pxDate[p * 4 + 0] = 255;

    }

    }

    } // 把修改之后的结果绘制到canvas2中

    context2.putImageData(imgDate, 0, 0, 0, 0, 400, 400);

    }

    </script>

    </html>

  • 相关阅读:
    Java 三大主流 工作流 学习
    有限状态机(FSM)的Java 学习FSM
    OSWorkFlow 学习
    三种分布式对象主流技术——COM、Java和COBRA
    Java对象池技术的原理及其实现
    tomcat,很多时候,可以在服务server.xml中可以实现一些效果
    理解Scala
    CAP原理和BASE思想
    Java 在PDF文档中绘制图形
    Java 处理PDF图章(印章)——图片图章、动态图章
  • 原文地址:https://www.cnblogs.com/sanshao221/p/6207835.html
Copyright © 2011-2022 走看看