这期完成了图像的一些简单的滤镜,问题就在于,关于rgb颜色之间的一些转换。
第二期完成视频的人脸识别
附上代码
gif过大 就不上传了
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.wrap {
display: flex;
}
.btnlist {}
.btnlist .fuyuan,
.black_white,
.test1,
.test4 {
display: block;
margin-bottom: 10px;
}
.Rline {
margin-top: 10px;
width: 200px;
height: 5px;
background: #ccc;
position: relative;
}
.Rlinebg {
width: 200px;
height: 20px;
background: transparent;
position: absolute;
top: 50%;
transform: translateY(-50%);
left: 0;
z-index: 10;
}
.Rtuozhuai {
width: 5px;
height: 20px;
background: lightblue;
position: absolute;
right: -5px;
top: 50%;
transform: translateY(-50%);
cursor: pointer;
}
.Rcolorline {
position: absolute;
left: 0;
top: 0;
width: 0px;
height: 5px;
background: lightblue;
}
.Bline {
margin-top: 10px;
width: 200px;
height: 5px;
background: #ccc;
position: relative;
}
.Blinebg {
width: 200px;
height: 20px;
background: transparent;
position: absolute;
top: 50%;
transform: translateY(-50%);
left: 0;
z-index: 10;
}
.Btuozhuai {
width: 5px;
height: 20px;
background: lightblue;
position: absolute;
right: -5px;
top: 50%;
transform: translateY(-50%);
cursor: pointer;
}
.Bcolorline {
position: absolute;
left: 0;
top: 0;
width: 0px;
height: 5px;
background: lightblue;
}
.progress {
display: none;
}
</style>
<body>
<img style="width: 300px;height: 206px;display: none;" id="image" src="./timg.jpeg" alt="">
<div class="wrap">
<div style="border: 1px solid #ccc;width: 500px;height: 500px;margin-right: 20px;">
<canvas width="500" height="500" id="canvas"></canvas>
</div>
<div class="btnlist">
<button class="fuyuan">复原</button>
<button class="black_white">变黑白</button>
<button class="test1">反色</button>
<button class="test2">冷色</button>
<button class="test3">暖色</button>
<div class="progress">
<div>r:10~30 当前:<span class="redColor"></span></div>
<div class="Rline">
<div class="Rcolorline">
<div class="Rtuozhuai"></div>
</div>
<div class="Rlinebg"></div>
</div>
<div style="margin-top: 30px;">g:10~30 当前:<span class="blueColor"></span></div>
<div class="Bline">
<div class="Bcolorline">
<div class="Btuozhuai"></div>
</div>
<div class="Blinebg"></div>
</div>
</div>
<button class="test4" style="margin-top: 10px;">冰冻效果</button>
<button class="test5">熔铸效果</button>
<button class="test6">连环画效果</button>
</div>
</div>
</body>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script>
// window.onload = function () {
$(function () {
var canvas = document.getElementById('canvas')
var context = canvas.getContext('2d'),
image = document.getElementById('image'),
imageData, data, i, len, average, red, green, blue, alpha
var aaaaa = function () {
console.log(1111)
}
const imageWidth = image.width;
const imageHeight = image.height;
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const imageWHRate = imageWidth / imageHeight;
const canvasWHRate = canvasWidth / canvasHeight;
const calHeight = canvasWidth * (1 / imageWHRate);
const calWidth = canvasHeight * imageWHRate;
var newWidth = '';
var newHeight = '';
var xData = ''
var yData = ''
if (calHeight > canvasHeight) {
newWidth = calWidth
newHeight = canvasHeight
xData = (canvasWidth - calWidth) / 2;
yData = 0
} else {
newWidth = canvasWidth
newHeight = calHeight
yData = (canvasHeight - calHeight) / 2;
xData = 0
// 绘制原始图像
}
context.drawImage(image, xData, yData, newWidth, newHeight);
// 取得图像数据
imageData = context.getImageData(xData, yData, newWidth, newHeight);
data = imageData.data;
// 变黑白
$('.black_white').click(function () {
fuyuanImage()
for (i = 0, len = data.length; i < len; i += 4) {
red = data[i];
green = data[i + 1];
blue = data[i + 2];
alpha = data[i + 3];
// 求得平均值
average = Math.floor((red + blue + green) / 3);
data[i] = average;
data[i + 1] = average;
data[i + 2] = average;
}
imageData.data = data;
// 回写图像数据并显示结果
context.putImageData(imageData, xData, yData);
})
//////////////////////////////// 反色
$('.test1').click(function () {
fuyuanImage()
for (i = 0, len = data.length; i < len; i += 4) {
red = data[i];
green = data[i + 1];
blue = data[i + 2];
alpha = data[i + 3];
// 求得平均值
average = Math.floor((red + blue + green) / 3);
data[i] = (255 - red);
data[i + 1] = (255 - green);
data[i + 2] = (255 - blue);
}
imageData.data = data;
// 回写图像数据并显示结果
context.putImageData(imageData, xData, yData);
})
// 复原图片
$('.fuyuan').click(function () {
fuyuanImage()
// context.drawImage(image, xData, yData, newWidth, newHeight);
})
// 复原图片
function fuyuanImage() {
context.drawImage(image, xData, yData, newWidth, newHeight);
// 取得图像数据
imageData = context.getImageData(xData, yData, newWidth, newHeight);
data = imageData.data;
}
// ///////////// 冷色拖拽条
var Rtag = false;
var colorRRange = 10;
var isCold = false;
$('.redColor').html(colorRRange)
$('.Rlinebg').mousedown(function (e) {
console.log('按下', e.offsetX)
$('.Rcolorline').width(e.offsetX);
// $('.tuozhuai').css('left', e.offsetX)
Rtag = true;
})
$('.Rlinebg').mousemove(function (e) {
if (Rtag) {
console.log(e.target == this)
if (e.target == this) {
$('.Rcolorline').width(e.offsetX);
console.log(e.offsetX / 200 * 20 + 10)
colorRRange = Math.floor(e.offsetX / 200 * 20 + 10)
$('.redColor').html(colorRRange)
if (isCold) {
getCold()
} else {
getWarm()
}
}
}
})
$('.Rlinebg').mouseup(function (e) {
Rtag = false;
})
$('.Rlinebg').mouseleave(function (e) {
Rtag = false;
})
/////////////// 暖色拖拽条
var Btag = false;
var colorBRange = 10;
$('.blueColor').html(colorBRange)
$('.Blinebg').mousedown(function (e) {
console.log('按下', e.offsetX)
$('.Bcolorline').width(e.offsetX);
// $('.tuozhuai').css('left', e.offsetX)
Btag = true;
})
$('.Blinebg').mousemove(function (e) {
if (Btag) {
console.log(e.target == this)
if (e.target == this) {
$('.Bcolorline').width(e.offsetX);
console.log(e.offsetX / 200 * 20 + 10)
colorBRange = Math.floor(e.offsetX / 200 * 20 + 10)
$('.blueColor').html(colorBRange)
if (isCold) {
getCold()
} else {
getWarm()
}
}
}
})
$('.Blinebg').mouseup(function (e) {
Btag = false;
})
$('.Blinebg').mouseleave(function (e) {
Btag = false;
})
////////////////////////////////// 冷色 r- b+
$('.test2').click(function () {
isCold = true;
$(this).css({ background: '#ff5000', color: '#fff' })
$('.test3').css({ background: '', color: '' })
$('.progress').css({ display: 'block' })
getCold()
})
////////////////////////////////// 暖色 r+ b-
$('.test3').click(function () {
isCold = false;
$(this).css({ background: '#ff5000', color: '#fff' })
$('.test2').css({ background: '', color: '' })
$('.progress').css({ display: 'block' })
getWarm()
})
function getCold() {
fuyuanImage()
for (i = 0, len = data.length; i < len; i += 4) {
red = data[i];
green = data[i + 1];
blue = data[i + 2];
alpha = data[i + 3];
data[i] = (red - colorRRange) <= 255 ? (red - colorRRange) : 255;
data[i + 2] = (blue + colorBRange) <= 255 ? (blue + colorBRange) : 255;
}
imageData.data = data;
// 回写图像数据并显示结果
context.putImageData(imageData, xData, yData);
}
function getWarm() {
fuyuanImage()
for (i = 0, len = data.length; i < len; i += 4) {
red = data[i];
green = data[i + 1];
blue = data[i + 2];
alpha = data[i + 3];
data[i] = (red + colorRRange) <= 255 ? (red + colorRRange) : 255;
data[i + 2] = (blue - colorBRange) <= 255 ? (blue - colorBRange) : 255;
}
imageData.data = data;
// 回写图像数据并显示结果
context.putImageData(imageData, xData, yData);
}
// ///////////// 冰冻效果
$('.test4').click(function () {
fuyuanImage()
for (i = 0, len = data.length; i < len; i += 4) {
red = data[i];
green = data[i + 1];
blue = data[i + 2];
alpha = data[i + 3];
// 求得平均值
average = Math.floor((red + blue + green) / 3);
data[i] = Math.abs(red - green - blue) * 3 / 2;
data[i + 1] = Math.abs(green - red - blue) * 3 / 2;
data[i + 2] = Math.abs(blue - red - green) * 3 / 2;
}
imageData.data = data;
// 回写图像数据并显示结果
context.putImageData(imageData, xData, yData);
})
// ///////////// 熔铸效果
$('.test5').click(function () {
fuyuanImage()
for (i = 0, len = data.length; i < len; i += 4) {
red = data[i];
green = data[i + 1];
blue = data[i + 2];
alpha = data[i + 3];
data[i] = red * 128 / (green + blue + 1);
data[i + 1] = green * 128 / (red + blue + 1);
data[i + 2] = blue * 128 / (green + red + 1);
}
imageData.data = data;
// 回写图像数据并显示结果
context.putImageData(imageData, xData, yData);
})
// ///////////// 连环画效果
$('.test6').click(function () {
fuyuanImage()
for (i = 0, len = data.length; i < len; i += 4) {
red = data[i];
green = data[i + 1];
blue = data[i + 2];
alpha = data[i + 3];
data[i] = Math.abs(green - blue + green + red) * red / 500;
data[i + 1] = Math.abs(blue - green + blue + red) * red / 500;
data[i + 2] = Math.abs(blue - green + blue + red) * green / 500;
}
imageData.data = data;
// 回写图像数据并显示结果
context.putImageData(imageData, xData, yData);
})
})
</script>
</html>
网友评论