H5 苹果安卓拍照demo

   近期公司要上一个现场拍照方案,从SAP获取质量拍照任务,然后现场人员从自己手机端拍照,上传到系统中,最后出质量客户报告。
   废话不多,上代码:::
<html>
<head>
    <title>图片上传登陆</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes" />

    <!-- 引入 layui.css -->
    <link rel="stylesheet" href="../../css/layui.css">
    <!-- 引入 layui.js -->
    <script src="../../js/layui.js"></script>
    <script src="../../js/jquery-2.1.1.min.js" type="text/javascript"></script>  
</head>
<body>
<style>
    body{
        font-size: 14px;
    }
    #snap{
        width: 50px;
        height: 50px;
        background: #59A869;
        box-shadow: 0 0  5px #fff;
        border-radius: 50px;
        position: fixed;
        bottom: 40px;
        left: 85%;
        margin-left: -25px;
    }
    #backEnd{
        width: 50px;
        height: 50px;
        background: #fff;
        box-shadow: 0 0  5px #fff;
        border-radius: 50px;
        position: fixed;
        bottom: 40px;
        left: 55%;
        margin-left: -25px;
    }
    #zoomIn{
        width: 50px;
        height: 50px;
        background: #fff;
        box-shadow: 0 0  5px #fff;
        border-radius: 50px;
        position: fixed;
        bottom: 40px;
        left: 40%;
        margin-left: -25px;
    }
    #zoomOut{
        width: 50px;
        height: 50px;
        background: #fff;
        box-shadow: 0 0  5px #fff;
        border-radius: 50px;
        position: fixed;
        bottom: 40px;
        left: 25%;
        margin-left: -25px;
    }
    #back{
        width: 50px;
        height: 50px;
        background: #fff;
        box-shadow: 0 0  5px #fff;
        border-radius: 50px;
        position: fixed;
        bottom: 40px;
        left: 10%;
        margin-left: -25px;
    }
    #save{
        width: 50px;
        height: 50px;
        background: #fff;
        box-shadow: 0 0  5px #fff;
        border-radius: 50px;
        position: fixed;
        bottom: 40px;
        left: 70%;
        margin-left: -25px;
    }
    #show{
        height: 100px;
        width: 200px;

    }
    #showView{
        margin: 30px;
    }

</style>
 
<div align=center>
<%--    autoplay loop playsinline 参数支持苹果手机浏览器--%>
    <video id="video"  autoplay loop playsinline ></video>
    <canvas style="background-color: black;" id="canvas"  ></canvas>
    <button id="snap">拍</button>
    <button id="back" onclick="goback()"><i class="layui-icon layui-icon-return layui-font-12"></i></button>
    <button id="save" onclick="upload([[${zaufnr}]],[['${no}']],[[${noChild}]])"><i class="layui-icon layui-icon-ok layui-font-12"></i></button>
    <button id="zoomIn">大</button>
    <button id="zoomOut">小</button>
    <button id="backEnd">后</button>
    <div id="showView">
        <img id="show" alt=""/>
    </div>
</div>
</body>
<script type="text/javascript">

    let video = document.getElementById('video');
    let canvas = document.getElementById('canvas');
    let context = canvas.getContext('2d');
    let img;
    // var vConsole = new VConsole();
    // polyfill 提供了这个方法用来获取设备的 pixel ratio
    var getPixelRatio = function(context) {
        var backingStore = context.backingStorePixelRatio ||
            context.webkitBackingStorePixelRatio ||
            context.mozBackingStorePixelRatio ||
            context.msBackingStorePixelRatio ||
            context.oBackingStorePixelRatio ||
            context.backingStorePixelRatio || 1;

        return (window.devicePixelRatio || 1) / backingStore;
    };
    //屏幕 大小
    //设置屏幕为横屏
    var screen_height = document.documentElement.clientHeight;
    var screen_width=document.documentElement.clientWidth;

    //视频横屏

    // var Vwidth=screen_width;//480;//视频显示宽
    var Vwidth=screen_width;//视频显示宽
    if(screen_height>screen_width)screen_height=screen_width;
    var VHeight=screen_height;//视频显示高
    // var VHeight=screen_height;//480;//视频显示高

    var Qwidth = ${width};//取景框 宽
    var Qheight = ${height};//取景框 高
    var QlocalX=screen_width/2 -Qwidth/2;//140;
    var QlocalY=screen_height/2-Qheight/2;// 140;

    var isFinish = '${isFinish}';
    var path = '${path}';
    // 监听 orientation changes
    window.addEventListener("orientationchange", function(event) {
         location.reload();
    }, false);



    function getUserMedia(constraints, success, error) {

        if (navigator.mediaDevices.getUserMedia) {
            //最新的标准API            navigator.mediaDevices.getUserMedia(constraints).then(success).catch(error);
        } else if (navigator.webkitGetUserMedia) {
            //webkit核心浏览器
            navigator.webkitGetUserMedia(constraints,success, error)
        } else if (navigator.mozGetUserMedia) {
//firfox浏览器
            navigator.mozGetUserMedia(constraints, success, error);
        } else if (navigator.getUserMedia) {
            //旧版API
            navigator.getUserMedia(constraints, success, error);
        }
    }
    var videos  = new Array();
    function success(stream) {
        // let CompatibleURL = window.URL || window.webkitURL;
        // video.src = window.webkitURL.createObjectURL(stream);
        // video.mediaStreamTrack  = stream;
        // video.srcObject = stream;
        // video.play();
        //旧的浏览器可能没有srcObject

        if("srcObject" in video) {
            video.srcObject=stream;

        }else{//防止在新的浏览器里使用它,应为它已经不再支持了
            video.src=window.URL.createObjectURL(stream);
        }

        video.onloadedmetadata= function(e) {
            video.play();
        };
    }
    function error(error) {
        alert("error")
    }
    //打开摄像头
    var useFrontCamera=false;//true前置摄像头, false后置摄像头
    if (navigator.mediaDevices.getUserMedia || navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia) {
        openVideo();
    } else {
    }
    //打开摄像头
    function openVideo(){
        var videoMode=useFrontCamera ? 'user' : { exact: 'environment' };
        getUserMedia({video : {
                facingMode: videoMode,
                width: Vwidth,
                height: VHeight
            }}, success, error);
    }
    window.onload = function(){
        init();
    }
    // 初始化屏幕大小
    canvas.width=canvas.width=video.width=Vwidth;
    canvas.height=video.height=VHeight;
    var dpr= getPixelRatio(context);
    // 获取css的宽高
    let { width: cssWidth, height: cssHeight } = canvas.getBoundingClientRect();
    // 不停的截图
    function init()
    {
         video.style.display="none";
        var i = window.setInterval(function () {
            //对video进行截图
            context.drawImage(video, 0, 0, Vwidth, VHeight);
            context.strokeStyle=  "#ff0000";
            context.strokeRect(QlocalX,QlocalY,Qwidth,Qheight);//x,y,w,h
        }, 10)//10ms
        QlocalX=Vwidth/2 -Qwidth/2;//140;
        QlocalY=VHeight/2-Qheight/2;// 140;
    }



    // 拍照按钮
    var snap = document.querySelector('#snap');
    if(snap !== null){
        snap.onclick=function(){
            context.drawImage(video, QlocalX,QlocalY,Qwidth,Qheight,0,0,Vwidth, VHeight);//图形大小
            // document.querySelector('img').src = canvas.toDataURL('image/webp',1);
            document.querySelector('img').src = canvas.toDataURL('image/jpeg',1);//1标识高清
            document.getElementById('showView').style.display="block";
        }
    }
    //切换到后端摄像头
    var backEnd = document.querySelector('#backEnd');
    if(backEnd !== null){
        backEnd.onclick=function(){
            if (useFrontCamera)
            useFrontCamera=false;//true前置摄像头, false后置摄像头
            else
            useFrontCamera=true;//true前置摄像头, false后置摄像头
            openVideo();
        }
    }
    //取景框放大
    var zoomIN = document.querySelector('#zoomIn');
    if(zoomIN !== null){
        zoomIN.onclick=function(){
            //按比例放大缩小
            Qwidth=Qwidth*0.1+Qwidth;
            Qheight=Qheight*0.1+Qheight;
            QlocalX=Vwidth/2 -Qwidth/2;//140;
            QlocalY=VHeight/2-Qheight/2;// 140;
        }
    }
    //取景框缩小
    var zoomOut = document.querySelector('#zoomOut');
    if(zoomOut !== null){
        zoomOut.onclick=function(){
            //按比例放大缩小
            Qwidth=Qwidth-Qwidth*0.1;
            Qheight=Qheight-Qheight*0.1;
            QlocalX=Vwidth/2 -Qwidth/2;//140;
            QlocalY=VHeight/2-Qheight/2;// 140;
        }
    }
    //显示框
    var show = document.querySelector('#show');
    var showView = document.querySelector('#showView');
    show.style.width = Qwidth;
    show.style.height = Qheight;
    if (isFinish == 'Y'){
        document.querySelector('img').src ='/static'+path+'?time='+new Date();
        document.getElementById('showView').style.display="block";
    }else{
        document.getElementById('showView').style.display="none";
    }
    ///放大缩小
    function upload(zaufnr,no,noChild){
        var fileName = zaufnr+'_'+no+'_'+noChild;
        console.log(fileName)
        var file = dataURLtoFile(document.querySelector('img').src,fileName);
        var fd = new FormData();
        fd.append("file",file);
        fd.append("zaufnr",zaufnr);
        fd.append("no",no);
        fd.append("isFinish",isFinish);
        fd.append("noChild",noChild);
        $.ajax({
            type: "post",
            url: "../../api/img/upload",
            contentType: false,
            cache: false,
            processData: false,
            data: fd, //fromData,
            success: function (res) {
                if (res != null && res.code == 200) {
                    layer.msg("上传成功", {
                        icon: 6,
                        time: 2000
                    }, function () {
                        //返回上一页
                        window.location.href = '../../detail/'+zaufnr;
                    });
                } else {
                    layer.open({
                        title: '上传失败',
                        content: res.message,
                        icon: 5,
                        anim: 6
                    });
                }
            },
        });
    }
    //dataURLtoFile
    function dataURLtoFile(dataUrl, fileName){
        var arr = dataUrl.split(','), mime = arr[0].match(/:(.*?);/)[1],
            bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
        while(n--){
            u8arr[n] = bstr.charCodeAt(n);
        }
        return new File([u8arr], fileName, {type:mime});
    }
    //返回
    function goback(){
        window.location.href = '../../detail/${zaufnr}';
    }
</script>
</html>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值