首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在手机浏览器中使用input[type='file']抓取照片后,如何在画布上绘制正确方向的照片?

在手机浏览器中使用input[type='file']抓取照片后,如何在画布上绘制正确方向的照片?
EN

Stack Overflow用户
提问于 2013-10-19 14:28:00
回答 5查看 53.6K关注 0票数 68

我正在制作一个简单的移动网络应用程序,允许访问者通过使用html5 inputtype=file element捕捉照片。然后我会在网上展示它预览,然后访问者可以选择上传到我的服务器上的其他目的(即:上传到FB)

当我用我的iPhone拍照时,我发现照片的方向有问题,并且在tag中持有vertically.The照片的方向是正确的。但是,当我尝试使用drawImage()方法将其绘制到画布中时,它被绘制为旋转了90度。

我试着在四个方向上拍照,只有一个方向可以在画布上画出正确的图像,其他的都是旋转的,甚至是颠倒的。

嗯,我很困惑于如何正确地解决这个问题……谢谢你的帮助..。

这是我的代码,大部分是从MDN复制的

代码语言:javascript
复制
<div class="container">
            <h1>Camera API</h1>

            <section class="main-content">
                <p>A demo of the Camera API, currently implemented in Firefox and Google Chrome on Android. Choose to take a picture with your device's camera and a preview will be shown through createObjectURL or a FileReader object (choosing local files supported too).</p>

                <p>
                    <form method="post" enctype="multipart/form-data" action="index.php">
                        <input type="file" id="take-picture" name="image" accept="image/*">
                        <input type="hidden" name="action" value="submit">
                        <input type="submit" >
                    </form>
                </p>

                <h2>Preview:</h2>
                <div style="width:100%;max-width:320px;">
                    <img src="about:blank" alt="" id="show-picture" width="100%">
                </div>

                <p id="error"></p>
                <canvas id="c" width="640" height="480"></canvas>
            </section>

        </div>


        <script>
            (function () {
                var takePicture = document.querySelector("#take-picture"),
                    showPicture = document.querySelector("#show-picture");

                if (takePicture && showPicture) {
                    // Set events
                    takePicture.onchange = function (event) {
                        showPicture.onload = function(){
                            var canvas = document.querySelector("#c");
                            var ctx = canvas.getContext("2d");
                            ctx.drawImage(showPicture,0,0,showPicture.width,showPicture.height);
                        }
                        // Get a reference to the taken picture or chosen file
                        var files = event.target.files,
                            file;
                        if (files && files.length > 0) {
                            file = files[0];
                            try {
                                // Get window.URL object
                                var URL = window.URL || window.webkitURL;

                                // Create ObjectURL
                                var imgURL = URL.createObjectURL(file);

                                // Set img src to ObjectURL
                                showPicture.src = imgURL;

                                // Revoke ObjectURL
                                URL.revokeObjectURL(imgURL);
                            }
                            catch (e) {
                                try {
                                    // Fallback if createObjectURL is not supported
                                    var fileReader = new FileReader();
                                    fileReader.onload = function (event) {
                                        showPicture.src = event.target.result;

                                    };
                                    fileReader.readAsDataURL(file);
                                }
                                catch (e) {
                                    // Display error message
                                    var error = document.querySelector("#error");
                                    if (error) {
                                        error.innerHTML = "Neither createObjectURL or FileReader are supported";
                                    }
                                }
                            }
                        }
                    };
                }
            })();
        </script>
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19463126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档