scanner overlay

📹 AVCaptureVideoPreviewLayer with a masked overlay

23
1
Swift

ScannerOverlay

ScannerOverlay extends AVCaptureVideoPreviewLayer with an overlay. You can define a masked area and setup the corners.

Installation

You can either install it via Swift Package Manager or copy the class from the Sources folder into your project.

Usage

At first setup your AVCaptureSession, then initiate the ScannerOverlay with it, and after that add it as a sublayer to your view.

  let scannerOverlayPreviewLayer              = ScannerOverlayPreviewLayer(session: captureSession)
  scannerOverlayPreviewLayer.frame            = self.view.bounds
  scannerOverlayPreviewLayer.maskSize         = CGSize(width: 200, height: 200)
  scannerOverlayPreviewLayer.videoGravity     = .resizeAspectFill
  self.view.layer.addSublayer(scannerOverlayPreviewLayer)

If you want to limit the video capturing to the masked area you have to set the rectOfInterest of your AVCaptureMetadataOutput.

  metadataOutput.rectOfInterest = scannerOverlayPreviewLayer.rectOfInterest

Keep in mind that the overlay won’t show in the simulator - for testing run the code on your device.

For an example implementation refer to the demo project in the Sources folder.

Settings

Parameter Description
maskSize Specifies the size of the masked area
cornerLength Specifies the length of the corners
lineWidth Specifies the line width of the corners
lineColor Specifies the line color of the corners
lineCap Specifies the line cap of the corner endpoints
backgroundColor Specifies the backgroundColor of the outer masked area
cornerRadius Specifies the corner radius of the masked area

License

ScannerOverlay is available under the MIT license. See the LICENSE file for more info.