【Demo】HTML5 拍照

來源:互聯網
上載者:User

標籤:

本文主要講解 手機瀏覽器 如何拍照

為什麼會有這個需求

最近做一個項目要用到拍照然後上傳照片,但是網頁拍照一般都是用Flash做的,而我們主要是H5頁面,如果在裡面有許可權就可以通過JSSDK調起網路攝影機拍照的。這裡我們主要說下手機端瀏覽器如何調起網路攝影機

H5如何開啟網路攝影機

不需要特別的支援,只需要一行代碼就可以了

<input id="upload" type="file" accept="image/*;" capture="camera" >
如何預覽圖

原理是用js擷取input file的映像流,然後賦給img標籤的src屬性,然後再設定這個img的css,就能得到你要的效果了,代碼如下

;var demo_h5_upload_ops = {    init:function(){        this.eventBind();    },    eventBind:function(){        var that = this;        $("#upload").change(function(){            var reader = new FileReader();            reader.onload = function (e) {                that.compress(this.result);            };            reader.readAsDataURL(this.files[0]);        });    },    compress : function (res) {        var that = this;        var img = new Image(),            maxH = 300;        img.onload = function () {            var cvs = document.createElement('canvas'),                ctx = cvs.getContext('2d');            if(img.height > maxH) {                img.width *= maxH / img.height;                img.height = maxH;            }            cvs.width = img.width;            cvs.height = img.height;            ctx.clearRect(0, 0, cvs.width, cvs.height);            ctx.drawImage(img, 0, 0, img.width, img.height);            var dataUrl = cvs.toDataURL('image/jpeg', 1);            $(".img_wrap").attr("src",dataUrl);            $(".img_wrap").show();            // 上傳略            that.upload( dataUrl );        };        img.src = res;    },    upload:function( image_data ){        /*這裡寫上次方法,圖片流是base64_encode的*/    }};$(document).ready( function(){    demo_h5_upload_ops.init();} );
執行個體示範

地址:http://m.vincentguo.cn/demo/h5_upload   也可以手機掃描


本人IOS 系統 親自測試了UC瀏覽器和內建瀏覽器 完美相容




原文地址:【Demo】HTML5 拍照
標籤:demo   h5   拍照   html5   

智能推薦
  • 營運技能大全 | Devops Tools 周期表
  • 【No.1】監控Linux效能25個命令列工具
  • 【iTerm2】美化你的Terminal 贈佛祖像
  • php 通過exec 建立git分支失敗
  • 【No.2】監控Linux效能25個命令列工具

【Demo】HTML5 拍照

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.