ZXing的WinJS低内存问题

时间:2014-02-04 07:26:55

标签: javascript canvas windows-8 zxing winjs

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

我对我的逻辑有多成功,但突然之间我看到我的应用程序因内存不足问题而崩溃。

<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()方法。

 var result = reader.decode(pix, canvas.width, canvas.height, ZXing.BitmapFormat.rgba32);

此行会导致内存问题。

提前致谢。

1 个答案:

答案 0 :(得分:1)

 var reader = new ZXing.BarcodeReader();

多个阅读器实例导致此问题。刚刚创建了一个阅读器实例并将其用于所有后续扫描将解决该问题。