我的網站歡迎來訪
原來作項目遇到了調用攝像頭功能,php小白遇到這狀況馬上就去網上搜索,最後用的
https://www.helloweba.com/vie...,太爛了,做者也沒說如何去使用,若是用的是框架開發更是很麻煩今天再次發現一款比較靈活的插件jQuery webcam plugin,資源連接:http://www.xarg.org/project/j...【demo】javascript
部分參考: http://www.cnblogs.com/loyung...
jQuery("#webcam").webcam({ width: 320, height: 240, mode: "callback", swffile: "/jscam_canvas_only.swf", // canvas only doesn't implement a jpeg encoder, so the file is much smaller onTick: function(remain) { if (0 == remain) { jQuery("#status").text("Cheese!"); } else { jQuery("#status").text(remain + " seconds remaining..."); } }, onSave: function(data) { var col = data.split(";"); // Work with the picture. Picture-data is encoded as an array of arrays... Not really nice, though =/ }, onCapture: function () { webcam.save(); // Show a flash for example }, debug: function (type, string) { // Write debug information to console.log() or a div, ... }, onLoad: function () { // Page load var cams = webcam.getCameraList(); for(var i in cams) { jQuery("#cams").append("<li>" + cams[i] + "</li>"); } } });
上面的js代碼再定義一個<div id="webcam"></div>,就能夠打開攝像頭了,可是要和php交互還要作一些修改
下面代碼中的註釋僅是我的理解,並不是做者原有,僅供參考
<!doctype html> <html> <head> <meta charset="utf-8"> <title>jQuery-webcam-master</title> <link href="cs.css" rel="stylesheet" type="text/css"> <script src="jquery-1.7.2.min.js"></script> <script src="jquery.webcam.min.js"></script> <script type="text/javascript"> $(function() { var pos = 0, ctx = null, saveCB, image = []; //建立畫布指定寬度和高度 var canvas = document.createElement("canvas"); canvas.setAttribute('width', 320); canvas.setAttribute('height', 240); //若是畫布成功建立 if (canvas.toDataURL) { //設置畫布爲2d,將來可能支持3d ctx = canvas.getContext("2d"); //截圖320*240,即整個畫布做爲有效區(cutx?) image = ctx.getImageData(0, 0, 320, 240); saveCB = function(data) { //把data切割爲數組 var col = data.split(";"); var img = image; //繪製圖像(這裏不是很理解算法) //參數data 只是每行的數據 ,例如320*240 大小的照片,一張完整的照片下來須要240個data,每一個data有320個rgb for(var i = 0; i < 320; i++) { //轉換爲十進制 var tmp = parseInt(col[i]); img.data[pos + 0] = (tmp >> 16) & 0xff; img.data[pos + 1] = (tmp >> 8) & 0xff; img.data[pos + 2] = tmp & 0xff; img.data[pos + 3] = 0xff; pos+= 4; } //當繪製320*240像素的圖片時發給後端php if (pos >= 4 * 320 * 240) { //把圖像放到畫布上,輸出爲png格式 ctx.putImageData(img, 0, 0); $.post("upload.php", {type: "data", image: canvas.toDataURL("image/png")}); pos = 0; } }; } else { saveCB = function(data) { //把數據一點點的放入image[] image.push(data); pos+= 4 * 320; if (pos >= 4 * 320 * 240) { $.post("upload.php", {type: "pixel", image: image.join('|')}); pos = 0; } }; } $("#webcam").webcam({ width: 320, height: 240, mode: "callback", swffile: "jscam_canvas_only.swf", onSave: saveCB, onCapture: function () { webcam.save(); }, debug: function (type, string) { console.log(type + ": " + string); } }); // /** // * 獲取canvas畫布的內容 getImageData // * 內容放回到canvas畫布 putImageData // * 獲取ImgData的每個像素 ImgData.data // * getImageData(起始點的橫座標, 起始點的縱座標, 獲取的寬度, 獲取的高度) // * putImageData(繪製點的橫座標, 繪製點點縱座標, imgData的起始點橫座標, imgData的起始點縱座標, 寬度, 高度) // */ }); </script> </head> <body> <div id="webcam"></div> <input type="button" onclick="webcam.capture();" value="點擊觸發" > </body> </html>
後臺是涉及的是php繪圖技術,在php.ini中開啓extension=php_gd2.dll,若是是框架開發,在上面的js中$.post異步給框架【TP】中控制器的某個方法
<?php //$str = file_get_contents("php://input"); //file_put_contents("upload.jpg", pack("H*", $str)); //var_dump($_POST['image']); if ($_POST['type'] == "pixel") { // input is in format 1,2,3...|1,2,3...|... $im = imagecreatetruecolor(320, 240); foreach (explode("|", $_POST['image']) as $y => $csv) { foreach (explode(";", $csv) as $x => $color) { imagesetpixel($im, $x, $y, $color); } } } else { // input is in format: data:image/png;base64,... $im = imagecreatefrompng($_POST['image']); } imagepng($im,"circle".time().".png");//保存,指定路徑 imagedestroy($im); // do something with $im
該插件有個缺點就是像素大小不能調整,若是要調整像素大小須要編譯 src目錄下的源碼,我沒有親自測試,提供編譯成功的例子 http://www.cnblogs.com/iasp/p...【jQuery Webcam Plugin jscam.swf文件反編譯工具使用說明】
原做者本人【https://github.com/infusion/j...】php
我本身的demo:css