HTML5, 原來如此神奇。程序在google瀏覽器中測試通過,感興趣的朋友可以參考本文講解的HTML5組件Canvas實現(xiàn)圖像灰度化的具體步驟,希望對你有所幫助
新建一個html頁面,在body tag之間加入
代碼如下:
<canvas id="myCanvas" >Gray Filter</canvas>
添加一段最簡單的JavaScript 腳本
代碼如下:
<pre name="code" class="javascript">window.onload = function() {
var canvas = document.getElementById("myCanvas");
<span style="white-space:pre"> </span>// TODO: do something here
}
從Canvas對象獲取繪制對象上下文Context的代碼如下:
代碼如下:
var context = canvas.getContext("2d");
在html頁面中加入一幅圖像的html代碼如下
代碼如下:
<img id="imageSource" src="hanjiaren.jpg" alt="Canvas Source" />
從html img對象中獲取image 對象的javascript代碼如下:
代碼如下:
var image = document.getElementById("imageSource");
將得到的圖像繪制在Canvas對象中的代碼如下:
代碼如下:
context.drawImage(image, 0, 0);
從Canvas對象中獲取圖像像素數(shù)據(jù)的代碼如下:
代碼如下:
var canvasData = context.getImageData(0, 0, canvas.width, canvas.height);
讀取像素值與實現(xiàn)灰度計算的代碼如下:
代碼如下:
for ( var x = 0; x < canvasData.width; x++) {
for ( var y = 0; y < canvasData.height; y++) {
// Index of the pixel in the array
var idx = (x + y * canvasData.width) * 4;
var r = canvasData.data[idx + 0];
var g = canvasData.data[idx + 1];
var b = canvasData.data[idx + 2];
// calculate gray scale value
var gray = .299 * r + .587 * g + .114 * b;
// assign gray scale value
canvasData.data[idx + 0] = gray; // Red channel
canvasData.data[idx + 1] = gray; // Green channel
canvasData.data[idx + 2] = gray; // Blue channel
canvasData.data[idx + 3] = 255; // Alpha channel
// add black border
if(x < 8 || y < 8 || x > (canvasData.width - 8) || y > (canvasData.height - 8))
{
canvasData.data[idx + 0] = 0;
canvasData.data[idx + 1] = 0;
canvasData.data[idx + 2] = 0;
}
}
}
其中計算灰度公式為 gray color = 0.299 × red color + 0.578 × green color + 0.114 * blue color
讀取出來的像素值順序為RGBA 分別代表red color, green color, blue color, alpha channel
處理完成的數(shù)據(jù)要重新載入到Canvas中。代碼如下:
context.putImageData(canvasData, 0, 0);
完全源代碼如下:
代碼如下:
<html>
<head>
<script>
window.onload = function() {
var canvas = document.getElementById("myCanvas");
var image = document.getElementById("imageSource");
// re-size the canvas deminsion
canvas.width = image.width;
canvas.height = image.height;
// get 2D render object
var context = canvas.getContext("2d");
context.drawImage(image, 0, 0);
var canvasData = context.getImageData(0, 0, canvas.width, canvas.height);
alert(canvasData.width.toString());
alert(canvasData.height.toString());
// gray filter
for ( var x = 0; x < canvasData.width; x++) {
for ( var y = 0; y < canvasData.height; y++) {
// Index of the pixel in the array
var idx = (x + y * canvasData.width) * 4;
var r = canvasData.data[idx + 0];
var g = canvasData.data[idx + 1];
var b = canvasData.data[idx + 2];
// calculate gray scale value
var gray = .299 * r + .587 * g + .114 * b;
// assign gray scale value
canvasData.data[idx + 0] = gray; // Red channel
canvasData.data[idx + 1] = gray; // Green channel
canvasData.data[idx + 2] = gray; // Blue channel
canvasData.data[idx + 3] = 255; // Alpha channel
// add black border
if(x < 8 || y < 8 || x > (canvasData.width - 8) || y > (canvasData.height - 8))
{
canvasData.data[idx + 0] = 0;
canvasData.data[idx + 1] = 0;
canvasData.data[idx + 2] = 0;
}
}
}
context.putImageData(canvasData, 0, 0); // at coords 0,0
};
</script>
</head>
<body>
<h2>Hello World!</h2>
<img id="imageSource" src="hanjiaren.jpg" alt="Canvas Source" />
<canvas id="myCanvas" >Gray Filter</canvas>
</body>
</html>
代碼中的文件可以替換任意你想要看到的圖片文件
HTML5, 原來如此神奇。程序在google瀏覽器中測試通過,
最后的忠告,千萬不要在本地嘗試運行上面的代碼,google瀏覽器的安全檢查會自動阻止從瀏覽器中讀寫非domain的文件
最好在tomcat或者任意個web container的server上發(fā)布以后從google瀏覽器查看效果即可。