【问题标题】:Scan the QR code picture directly from camera with ZXing使用ZXing直接从相机扫描二维码图片
【发布时间】:2016-04-07 13:03:36
【问题描述】:

我开发了一个 ASP 网站(目标是移动用户),其主要功能是解码 QR 码。我决定使用 ZXing 作为解码器,如果我输入好的二维码图像,它就可以完美运行。但是当我通过直接从相机拍照来更改二维码的来源时:

<input type="file" name="file" capture="camera" accept="image/*" id="cameraInput" name="cameraInput">

它不起作用。即使我尽可能稳定地拍摄照片,ZXing 仍然无法从相机中解码该照片。 问题是您知道解码从相机拍摄的 QR 码的最佳方法是什么吗?如果这需要很多努力,那么您是否有其他关于解码二维码的建议(使用 ASP.NET 的 Web 平台)。

【问题讨论】:

    标签: asp.net camera qr-code zxing mobile-website


    【解决方案1】:

    所以,我已经有了解决方案。事实上,问题不在于 ZXing,而在于拍摄图像的结果。因为当我尝试拍摄图像,并直接将其转换并显示到画布中时,图片的结果非常大。这就是ZXing解码失败的原因。但是当我首先尝试管理画布图像(在我的情况下为 400 x 400 像素)时,将 base64 图像发送到服务器,在服务器中再次将其转换为图像,然后对其进行解码。它像魅力一样完美。

    编辑: 这是我用于解决方案的代码(为了使其可读,我删除了一些不相关的代码。但希望我没有删除有用的部分)

    var JsIndexPage = new function (jsonData) {
        this.pageData = {
            maxWidth: 400,
            maxHeight: 400
        };
    
        this.pageUI = {
            canvas: null,
            blankCanvas: null,
            messageDiv: null,
            cameraInput: null,
            uploadInput: null
        };
    
        this.initUI = function () {
            ///<summary>
            ///Display ui on the page
            ///</summary>
    
            this.pageUI.canvas = document.getElementById('capturedPhoto');
            this.pageUI.blankCanvas = document.getElementById('blank');
            this.pageUI.cameraInput = $('#cameraInput');
            this.pageUI.messageDiv = $("#message");
            this.pageUI.uploadInput = $('#uploadInput');
    
            //add triggers to buttons
            this.pageUI.cameraInput.on('change', function (e) {
                JsIndexPage.onCameraInputChanged(e);
            });
    
            this.pageUI.uploadInput.on('click', function () {
                JsIndexPage.onUploadInputClick();
            });
        };
    
        this.onCameraInputChanged = function (e) {
            /// <summary>
            /// On camera input changed write image to canvas
            /// </summary>
    
            var reader = new FileReader();
            reader.onload = function (event) {
                JsIndexPage.onFileSelected(event);
            }
    
            //contains the data as a URL representing the file's data as a base64 encoded string
            reader.readAsDataURL(e.target.files[0]);
        };
    
        this.onFileSelected = function (event) {
            ///<summary>
            /// Html5 file readed onLoad event handler
            ///</summary>
    
            var context = this.pageUI.canvas.getContext('2d');
    
            //instantiates the Image() object
            var img = new Image();
            img.onload = function () {
    
                //converts the sizes of image into the proper size (400 x 400 at maximum)
                if (img.width > JsIndexPage.pageData.maxWidth && img.height <= JsIndexPage.pageData.maxHeight) {
                    JsIndexPage.pageUI.canvas.width = JsIndexPage.pageUI.maxWidth;
                    JsIndexPage.pageUI.canvas.height = (JsIndexPage.pageUI.canvas.width / img.width) * img.height;
                }
                else if (img.width <= JsIndexPage.pageData.maxWidth && img.height > JsIndexPage.pageData.maxHeight) {
                    JsIndexPage.pageUI.canvas.height = JsIndexPage.pageData.maxHeight;
                    JsIndexPage.pageUI.canvas.width = (JsIndexPage.pageUI.canvas.height / img.height) * img.width;
                }
                else if (img.width <= JsIndexPage.pageData.maxWidth && img.height <= JsIndexPage.pageData.maxHeight) {
                    JsIndexPage.pageUI.canvas.width = img.width;
                    JsIndexPage.pageUI.canvas.height = img.height;
                }
                else if (img.width > JsIndexPage.pageData.maxWidth && img.height > JsIndexPage.pageData.maxHeight) {
                    if (img.width > img.height) {
                        JsIndexPage.pageUI.canvas.width = JsIndexPage.pageData.maxWidth;
                        JsIndexPage.pageUI.canvas.height = (JsIndexPage.pageUI.canvas.width / img.width) * img.height;
                    }
                    else {
                        JsIndexPage.pageUI.canvas.height = JsIndexPage.pageData.maxHeight;
                        JsIndexPage.pageUI.canvas.width = (JsIndexPage.pageUI.canvas.height / img.height) * img.width;
                    }
                }
    
                //draws the context to the canvas
                context.drawImage(img, 0, 0, JsIndexPage.pageUI.canvas.width, JsIndexPage.pageUI.canvas.height);
            }
    
            //changes the image source into the new one
            img.src = event.target.result;
        };
    }
    

    【讨论】:

    • 我似乎遇到了同样的问题,我的服务器从文件中可以很好地解码 QR 码,但是当我拍照时,它总是失败。您是如何将图像转换为 400x400px 的,您有任何代码吗?另一个问题,我假设您将其转换为 base 64 以减少发送的数据量并消除将图像保存在服务器上的需要?谢谢。
    • 是的,没错。获取图像,将其转换为 base64,然后从那里管理大小。
    • 你介意给我看一些代码吗?我将图像发送到我的服务器,然后将其缩小到 400 x 400 像素,然后使用 ZXing 进行解码,但仍然没有运气。
    • @James 刚刚编辑了答案。最好在将图像发送到服务器之前对其进行编辑。
    • 太棒了,感谢您的帮助 - 我会试一试!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多