扫描时的条形码位置

时间:2013-05-13 08:33:25

标签: java android graphics barcode-scanner screen-capture

我正在为我的应用程序使用RedLaser库进行条形码扫描(我认为这是建立在Zxing之上)。一切工作正常,而在扫描模式下,扫描视图中的任何条形码,但我不希望这样,我只想要考虑在扫描区域中对齐的条形码,其余部分将被忽略。

redlaser示例应用程序,之后我在自己的应用程序中实现了库,在扫描活动的布局上有一个矩形,但是被忽略了,因为来自屏幕任何部分的条形码被扫描并考虑到示例应用。

底线:我希望我的扫描活动能够比较当前检测到的条形码的位置,看它是否在“扫描区域”的范围内,如果不是那么它就赢了不读。

barcode scanning

以下是调整“扫描区域”的代码:

     viewfinderView = findViewById(R.id.view_finder);
        LayoutParams params = new LayoutParams((int)(mDisplay.getWidth() * .75f), 
                (int)(mDisplay.getHeight() * .33f));
        params.gravity = Gravity.CENTER;
        viewfinderView.setLayoutParams(params);

以下代码没有做任何事情,我只是写它来举例说明一切是如何工作的(因为我没有Redlaser库的源文件),比如条形码位置。

            Set<BarcodeResult> allResults = (Set<BarcodeResult>) scanStatus.get(Status.STATUS_FOUND_BARCODES);
            for( BarcodeResult  s : allResults)
            {
                ArrayList<PointF> barcodelocation = s.barcodeLocation;
                float x = barcodelocation.get(0).x;
                float y = barcodelocation.get(0).y;
//there will be 4 points to each scanned barcode => 8 float's/barcode
            }

对不起这篇长篇文章感到抱歉,但我现在已经讨论了这个问题一段时间了,我真的被卡住了。

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

管理在Redlaser网站上找到解决方案:将视图转换为矩形,然后将条形码位置与使用.contain的条形码位置进行比较:

Rect scanningArea = new Rect(viewfinderView.getLeft(), viewfinderView.getTop(), viewfinderView.getRight(), viewfinderView.getBottom());
if(latestResult.iterator().hasNext())
        {

            boolean isInside = true;
            ArrayList<PointF> barcodelocation = latestResult.iterator().next().barcodeLocation;
            for (int i = 0; i < barcodelocation.size(); i++) 
            {
                int x = (int) barcodelocation.get(i).x;
                int y = (int) barcodelocation.get(i).y;
                if (!scanningArea.contains(x, y)) 
                {
                    isInside = false;
                }
            }
            if (isInside) 
            {
            //do stuff here
             }

        }

我还在处理一些问题,但现在回答这个问题了。要继续前进,让自己轻拍一下。 :)