Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to control what Camera sees #38

Open
ThunZEN opened this issue Nov 7, 2020 · 5 comments
Open

How to control what Camera sees #38

ThunZEN opened this issue Nov 7, 2020 · 5 comments

Comments

@ThunZEN
Copy link

ThunZEN commented Nov 7, 2020

How to make the camera see only those objects that are in the frame

WhatsApp Image 2020-11-07 at 21 35 58

@heart
Copy link
Owner

heart commented Nov 7, 2020

Hi @ThunZEN
thankyou to let me know the problem I will fix it in the next version

@ThunZEN ThunZEN closed this as completed Nov 7, 2020
@heart heart reopened this Nov 7, 2020
@heart
Copy link
Owner

heart commented Nov 7, 2020

Hi @ThunZEN Keep it open that will remind me for this

@ButasButora
Copy link

hello, i have the same question. I cropped the screen scan preview, but the scanner reads the codes outside this area. How to fix this?

@tomasf71
Copy link

The same problem, is there any way how to detect codes only from the frame? It would be very useful

@ButasButora
Copy link

Try this setupCamera function:

func setupCamera() {
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: cameraPosition)

    if let selectedCamera = deviceDiscoverySession.devices.first {
        if let input = try? AVCaptureDeviceInput(device: selectedCamera) {
           
            let session = AVCaptureSession()
            session.sessionPreset = .hd1920x1080//.vga640x480
            if session.canAddInput(input) {
                session.addInput(input)
                cameraInput = input
            }
            
            if session.canAddOutput(metadataOutput) {
                session.addOutput(metadataOutput)
        
                metadataOutput.metadataObjectTypes = supportBarcode
                metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
            }
            
            previewLayer?.removeFromSuperlayer()
            self.session = session
          
            self.selectedCamera = selectedCamera
           
            self.backgroundColor = UIColor.gray
            
            // calculate a centered square rectangle with red border
            //let size = 200
            let screenWidth = CGFloat(1080) //bounds.size.width
            let screenHeight = CGFloat(1920)
            
           // let xPos = (CGFloat(screenWidth) / CGFloat(2)) - (CGFloat(size) / CGFloat(2))
            //let scanRect = CGRect(x: Int(xPos), y: 150, width: size, height: size)
            //width is height :-)
            /// windows height 
            let w = screenWidth / 2.5

            /// window width 
            let h = screenHeight

            /// Middle on window height 
            let ws = screenWidth / 2 - w / 2
            
            let scanRect = CGRect(x: ws , y: 0, width: w, height: h)
            
            let x = scanRect.origin.x/screenWidth
            let y = scanRect.origin.y/screenHeight
            let width = scanRect.width/screenWidth
            let height = scanRect.height/screenHeight
            let scanRectTransformed = CGRect(x: x, y: y, width: width, height: height)
            
            //create area for scanning 
            let scanAreaView = UIView()
            scanAreaView.layer.borderColor = UIColor.red.cgColor
            scanAreaView.layer.borderWidth = 4
            scanAreaView.frame = scanRect
            //   self.addSubview(scanAreaView)
   
                DispatchQueue.global().async {
                    Thread.sleep(forTimeInterval: 0.2)
                    session.startRunning()
                    
                    DispatchQueue.main.async {
                        let previewLayer = AVCaptureVideoPreviewLayer(session: session)
                        //Preview layer je 0x0 = nelze použít metadataOutputRectConverted
                        //let rectOfInterest = self.previewLayer?.metadataOutputRectConverted(fromLayerRect: CGRect(x: 0, y: 0, width: 300, height: 300)) //  videoPreviewLayer is AVCaptureVideoPreviewLayer
                        //print ("width: \(rectOfInterest?.width); heght: \(rectOfInterest?.height)")
                        
                        // print ("rect of int :\(self.metadataOutput.rectOfInterest)")
                        self.metadataOutput.rectOfInterest = scanRectTransformed
                        
                        try? selectedCamera.lockForConfiguration()
                    
                            do {
                                self.selectedCamera!.focusMode = .continuousAutoFocus
                            }
                        
                        selectedCamera.unlockForConfiguration()
                      
                        //rectOfInterest ?? CGRect(x: 0, y: 0, width: 1, height: 1)
                        //   print ("rect of int 2:\(self.metadataOutput.rectOfInterest)")
                        previewLayer.videoGravity = .resizeAspectFill
                        //  print ("previewLayer: width: \(previewLayer.frame.width); heght: \(previewLayer.frame.height)")
                        self.layer.addSublayer(previewLayer)
                        
                        self.previewLayer = previewLayer
                        self.updateCameraView()
                    }
                }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants