首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WinJS低内存问题与ZXing

WinJS低内存问题与ZXing
EN

Stack Overflow用户
提问于 2014-02-04 07:26:54
回答 1查看 277关注 0票数 1

我正在为Windows 8 Metro App创建一个条形码扫描模块。

我的逻辑有多成功,但突然间,我看到我的应用程序由于内存不足而崩溃。

代码语言:javascript
复制
<script>
        var canvas = null;
        var ctx = null;
        var livePreview = null;
        var count = 0,rescount=0;
        function takepicture() {
            var Capture = Windows.Media.Capture;
             livePreview = document.getElementById("live-preview");
            var mediaCapture = new Capture.MediaCapture();
            canvas = document.getElementById("Vcanvas");
           ctx=canvas.getContext('2d');



           livePreview.addEventListener('play', function () { var i = window.setInterval(function () { ctx.drawImage(livePreview, 0, 0, canvas.width, canvas.height); scanCanvasEasy(); }, 20); }, false);
            livePreview.addEventListener('pause', function () { window.clearInterval(i); }, false);
            livePreview.addEventListener('ended', function () { clearInterval(i); }, false);

            /*
            var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
            openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
            openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.videosLibrary;
            openPicker.fileTypeFilter.replaceAll([".mp4", ".avi", ".ogg"]);
            openPicker.pickSingleFileAsync()
            .then(function (file) {
                if (file) {
                    // draw the image
                    var img = new Image;
                    //img.onload = function () {
                    //    canvas.width = img.width;
                    //    canvas.height = img.height;
                    //    ctx.drawImage(img, 0, 0, img.width, img.height);
                    //    scanCanvasEasy();
                    //}
                    //img.src = URL.createObjectURL(file);
                    // open a stream from the image
                    livePreview.src = URL.createObjectURL(file);
                    livePreview.play();

                }
            })*/
            mediaCapture.initializeAsync().then(function () {

                livePreview.src = URL.createObjectURL(mediaCapture);
                livePreview.play();
            });
        }



        function scanCanvasEasy() {
            var imgd = ctx.getImageData(0, 0,canvas.width,canvas.height);
            var pix = imgd.data;
            var reader = new ZXing.BarcodeReader();
            reader.onresultpointfound = function (resultPoint) {
                // do something with the resultpoint location
                console.log(resultPoint.toString());
            }
            // try to decode the raw pixel data
            var result = reader.decode(pix, canvas.width, canvas.height, ZXing.BitmapFormat.rgba32);
            /*

            The above line cause that memory issue, without that line there is no change in memory level.
            */


            // show the result
            if (result) {
                document.getElementById("result").innerText ="Result(+"+rescount++ +")==>"+ result.text;
            }
            else {
                document.getElementById("error").innerText = "no barcode found" + count++;
            }
      }
      </script>

我发布了这里使用的全部代码,我刚刚从按钮单击事件中调用了takepicture()方法。

代码语言:javascript
复制
 var result = reader.decode(pix, canvas.width, canvas.height, ZXing.BitmapFormat.rgba32);

这一行会导致内存问题。

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-04 12:22:34

代码语言:javascript
复制
 var reader = new ZXing.BarcodeReader();

多个读取器实例导致此问题。只要创建一个阅读器实例并将其用于所有后续扫描,就会修复该问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21546398

复制
相关文章

相似问题

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