使用 jquery.webcam 進行asp.net 拍照

來源:互聯網
上載者:User

標籤:try   com   request   ebe   oba   jquery   pos   rip   div   

HTML 程式碼 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="JqueryCamera.index" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head >    <title></title>    <script type="text/javascript" src="http://cdn.bootcss.com/jquery/2.1.3/jquery.js"></script>    <script type="text/javascript" src="js/jquery.webcam.js"></script></head><body>        <script type="text/javascript">        $(document).ready(function () {            var image = new Array();            var ctx = null;            var pos = 0;            var w = 320;            var h = 240;            $("#webcam").webcam({                width: 320,                height: 240,                mode: "callback",                swffile: "/js/jscam_canvas_only.swf",                onTick: function (remain) { },                onSave: function (data) {                    image.push(data);                    pos += 4 * w;                    if (pos == 4 * w * h) {                        $.post("CamPost.ashx", { w: w, h: h, pix: image.join(‘|‘) }, function (msg) {                            $("#flash").attr(‘src‘, msg);                            $("#txt_img_path").val(msg);                            pos = 0;                            image = new Array();                        });                    }                },                onCapture: function () { webcam.save(); },                onLoad: function () {                    var x = 0;                    var cams = webcam.getCameraList();                    for (var i in cams) {                        jQuery("#cams").append("裝置資訊:" + cams[i] + 裝置信);                        x++;                    }                    jQuery("#Div1").append("擁有裝置數:" + x + "");              }            });        });    </script>    <div id="cam" style="width: 320px; height:240px; border: solid 1px #ddd; float:left;">        <div id="camBox">            <div id="webcam">            </div>        </div>           </div>           <div style="width: 320px; height: 240px; border: 1px solid #ddd; float:left;">              <img id="flash" />                   </div>     <div style="clear: both;">    </div>      <input type="button" value="拍照" id="btn_pz" name="btn_pz"    onclick="webcam.capture()"/><br />       <input type="text" id="txt_img_path"   style=" width:320px;"/>        <div id="cams"></div>         <div id="Div1"></div>    </body></html>




 CamPost.ashx 代碼

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace JqueryCamera{    /// <summary>    /// CamPost 的摘要說明    /// </summary>    public class CamPost : IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            string wbepath = "";            context.Response.ContentType = "text/plain";             DateTime dt = DateTime.Now;             string fileName =  dt.ToString("yyyyMMdd_HHmmss", System.Globalization.DateTimeFormatInfo.InvariantInfo) + ".jpg";            if (context.Request["pix"] != null )            {                if (!string.IsNullOrEmpty(context.Request["w"]) && !string.IsNullOrEmpty(context.Request["h"]) && !string.IsNullOrEmpty(context.Request["pix"])) // && !string.IsNullOrEmpty(context.Request["planid"]) && !string.IsNullOrEmpty(context.Request["examkey"]                {                    string planid = DateTime.Now.ToString("yyyyMMdd")+  "";                    string examkey = context.Request["examkey"];                    string width = context.Request["w"];                    string height = context.Request["h"];                    string pix = context.Request["pix"];                    int w = int.Parse(width);                    int h = int.Parse(height);                    string savePath = context.Server.MapPath("~/UpLoad/Cam_Img/" + planid + "/");                    try                    {                        System.Drawing.Bitmap bmap = new System.Drawing.Bitmap(w, h);                        string[] rows = pix.Split(new char[] { ‘|‘ }, StringSplitOptions.RemoveEmptyEntries);                        for (int i = 0; i < rows.Length; i++)                        {                            string[] col = rows[i].Split(new char[] { ‘;‘ }, StringSplitOptions.RemoveEmptyEntries);                            for (int j = 0; j < col.Length; j++)                            {                                System.Drawing.Color color = System.Drawing.Color.FromArgb(Convert.ToInt32(col[j]));                                System.Drawing.Color reColor = System.Drawing.Color.FromArgb(255, color);                                bmap.SetPixel(j, i, reColor);                            }                        }                        System.IO.DirectoryInfo dirPath = new System.IO.DirectoryInfo(savePath);                        if (!dirPath.Exists)                        {                            dirPath.Create();                        }                                                                      savePath += fileName;                        bmap.Save(savePath);                    }                    catch (Exception e)                    {                    }                    wbepath = "/UpLoad/Cam_Img/" + planid + "/" + fileName;                    context.Response.Write(wbepath);                    context.Response.End();                }            }        }        public bool IsReusable        {            get            {                return false;            }        }    }}

  

 

使用 jquery.webcam 進行asp.net 拍照

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.