Android Vision - Reduce bar code tracking window

前端 未结 2 1518
逝去的感伤
逝去的感伤 2021-01-02 02:12

I\'m trying to implement Google Visions scanner into an app im working on. By default its a full screen activity and barcodes are tracked over the entire screen.

How

2条回答
  •  遥遥无期
    2021-01-02 02:51

    Based on @pm0733464's answer with an example of how to get the barcode that's nearest to the center of the preview.

    public class CentralBarcodeFocusingProcessor extends FocusingProcessor {
    
        public CentralBarcodeFocusingProcessor(Detector detector, Tracker tracker) {
            super(detector, tracker);
        }
    
        @Override
        public int selectFocus(Detector.Detections detections) {
    
            SparseArray barcodes = detections.getDetectedItems();
            Frame.Metadata meta = detections.getFrameMetadata();
            double nearestDistance = Double.MAX_VALUE;
            int id = -1;
    
            for (int i = 0; i < barcodes.size(); ++i) {
                int tempId = barcodes.keyAt(i);
                Barcode barcode = barcodes.get(tempId);
                float dx = Math.abs((meta.getWidth() / 2) - barcode.getBoundingBox().centerX());
                float dy = Math.abs((meta.getHeight() / 2) - barcode.getBoundingBox().centerY());
    
                double distanceFromCenter =  Math.sqrt((dx * dx) + (dy * dy));
    
                if (distanceFromCenter < nearestDistance) {
                    id = tempId;
                    nearestDistance = distanceFromCenter;
                }
            }
            return id;
        }
    }
    

提交回复
热议问题