HTML5, 原來如此神奇。程式在google瀏覽器中測試通過,有興趣的朋友可以參考本文講解的HTML5元件Canvas實現圖像灰度化的具體步驟,希望對你有所幫助
新建一個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 }
程式碼如下:
程式碼如下:
var context = canvas.getContext("2d");
<img id="imageSource" src="hanjiaren.jpg" alt="Canvas Source" />
從Canvas物件取得繪製物件上下文Context的程式碼如下:
程式碼如下:
程式在html頁中加入一頁圖片的html程式碼如下 程式碼如下:
var image = document.getElementById("imageSource");
context.drawImage(image, 0, 0);
程式碼如下:
var canvasData = context.getImageData(0, 0, canvas.width, canvas.height);
將所得的影像繪製在Canvas物件中的程式碼如下:
程式碼>
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
處理完成的資料要重新載入到Canvas中。程式碼如下:
context.putImageData(canvasData, 0, 0);
:
<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>Hello World!
<img id="imageSource" src="hanjiaren.jpg" alt="Canvas Source" /> <canvas id="myCanvas" >Gray Filter</canvas>