質問

I'm using for my app the RedLaser library for barcode scanning (which is built off Zxing, I believe). Everything is working fine, while in scanning mode, any barcode that comes within view is scanned, but I don't want that, I want only barcodes that are aligned in a scanning area to be considered and the rest to be ignored.

The redlaser sample app, after which I implemented the library in my own app, had a rectangle on the layout of the scanning activiy, however that was ignored, as barcodes from any part of the screen were scanned and taken into account by the sample app.

Bottom line: I want my scanning activity to compare the location of the currently detected barcode and see if it is within the bounds of the "scanning area", if it is not then it won't be read.

barcode scanning

Here is the code for the adjustment of the "scanning area":

     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);

The following code doesn't do anything, I just wrote it to exemplify the how everything works (since I don't have the source files for the Redlaser library), like the barcodelocation.

            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
            }

Sorry for the long post, but I've been on this issue for a while now and I'm really stuck.

Any help is appreciated!

役に立ちましたか?

解決

Managed to find a solution on the Redlaser site: turning the view into a rect then comparing the barcode location with that using .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
             }

        }

I'm still working on a few issues, but this question is answered now. Gonna go ahead and give myself a pat on the back. :)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top