天天看點

Web調用安卓,蘋果手機攝像頭,本地圖檔和檔案

由于要給一個客戶做一個記賬WAP,裡面有調用手機拍照功能,這裡記錄一下,以供需要的朋友,下面是完整的一個HTML頁面内容,放在伺服器上然後浏覽就可以了,隻支援Chrome和Safari核的浏覽器,我測試過QQ浏覽器,Chrome,Safari浏覽器都可以。在不同的手機和浏覽器上面展現的方式不一樣。

<!DOCTYPE HTML>  

<html>  

<head>  

    <title>上傳圖檔</title>  

    <meta charset="utf-8">  

</head>  

<body>  

    <iframe name="uploadfrm" id="uploadfrm" style="display: none;"></iframe>  

    <form name="formHead" method="post" action="" id="formHead" enctype="multipart/form-data" target="uploadfrm">  

        <div>  

            <div>  

                <input type="file" name="file_head" id="file_head" onchange="javascript:setImagePreview();" />  

            </div>  

                <div id="DivUp" style="display: none">  

                    <input type="submit" data-inline="true" id="BtnUp" value="确認上傳" data-mini="true" />  

                </div>  

        </div>  

    </form>  

    <div data-role="fieldcontain">  

        <div id="localImag">  

            <img id="preview" width="-1" height="-1" style="display: none" />  

    </div>  

    <script type="text/javascript">  

        function setImagePreview() {  

            var preview, img_txt, localImag, file_head = document.getElementById("file_head"),  

            picture = file_head.value;  

            if (!picture.match(/.jpg|.gif|.png|.bmp/i)) return alert("您上傳的圖檔格式不正确,請重新選擇!"),  

            !1;  

            if (preview = document.getElementById("preview"), file_head.files && file_head.files[0]) preview.style.display = "block",  

                preview.style.width = "63px",  

                preview.style.height = "63px",  

                preview.src = window.navigator.userAgent.indexOf("Chrome") >= 1 || window.navigator.userAgent.indexOf("Safari") >= 1 ? window.webkitURL.createObjectURL(file_head.files[0]) : window.URL.createObjectURL(file_head.files[0]);  

            else {  

                file_head.select(),  

                file_head.blur(),  

                img_txt = document.selection.createRange().text,  

                localImag = document.getElementById("localImag"),  

                localImag.style.width = "63px",  

                localImag.style.height = "63px";  

                try {  

                    localImag.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)",  

                    localImag.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = img_txt  

                } catch(f) {  

                    return alert("您上傳的圖檔格式不正确,請重新選擇!"),  

                    !1  

                }  

                preview.style.display = "none",  

                document.selection.empty()  

            }  

            return document.getElementById("DivUp").style.display = "block",  

            !0  

        }  

    </script>  

</body>  

</html>  

伺服器端程式自己加,如果自己沒有伺服器也可以調用http://jwzhangjie.com/preview.html來做測試

下面是調用幾個浏覽器的測試效果:

Chrome浏覽器效果:

Web調用安卓,蘋果手機攝像頭,本地圖檔和檔案

QQ浏覽器效果:

Web調用安卓,蘋果手機攝像頭,本地圖檔和檔案

拍照效果:

Web調用安卓,蘋果手機攝像頭,本地圖檔和檔案

本文轉自ljianbing51CTO部落格,原文連結:http://blog.51cto.com/ljianbing/1607033 ,如需轉載請自行聯系原作者

繼續閱讀