The image captured by AVFoundation has a strange conversion.

Asked 2 years ago, Updated 2 years ago, 157 views

I would like to use AVFoundation to capture the in-camera capture image, but the capture image is strange.
I have attached an image.

Strange capture image

I captured my business card normally, but it looks like it's crushed like the attached image.
The affected codes are as follows:
If anyone knows, please let me know.

class BusinessCardReaderViewController{

    // MARK: - AVFoundation
    let session —AVCaptureSession=.init()
    let output —AVCapturePhotoOutput=.init()
    let settings —AVCapturePhotoSettings=.init()

    var previewLayer: AVCaptureVideoPreviewLayer=.init()

    override func viewDidLoad(){
        // omission
        // Start Camera Input
        let deviceDescoverySession=

        AVCaptureDevice.DiscoverySession.init(deviceType: AVCaptureDevice.DeviceType.buildInWideAngleCamera),
                                              mediaType —AVMediaType.video,
                                              position: AVCaptureDevice.Position.front)
        var captureDevice —AVCaptureDevice?=nil
        for device in deviceDescoverySession.devices{
            if device.position==.front{
                captureDevice=device
                break
            }
        }
        guard captureDevice != nil else {
            print("This device do not have front camera")
            return
        }
    
        let input = try ? AVCaptureDeviceInput (device:captureDevice!)
        session.canAddInput(input!)
        session.canAddOutput(output)

        session.addInput(input!)
        session.addOutput(output)
        session.startRunning()
            
        previewLayer=.init(session:session)
        previewLayer.frame = CGRect (x:0, y:0, width:600, height:400)
        previewLayer.videoGravity=.resizeAspectFill
        view.layer.addSublayer(previewLayer)
    
        settings.isAutoStillImageStabilizationEnabled=true
    }

    private func takeScreenshot(){
        self.output.capturePhoto (with:self.settings, delete:self)
    }

    funcphotoOutput(_output:AVCapturePhotoOutput, didFinishProcessingPhoto:AVCapturePhoto, error:Error?){
        guard let photoData=photo.fileDataRepresentation(),
            let image = UIImage (data:photoData) else {return}
        let imageView: UIImageView= UIImageView.init(image:image)
        imageView.frame = CGRect (x:0, y:0, width:600, height:400)
    
        // image processing
        view.addSubview (imageView)
    }
}

swift ios xcode camera avfoundation

2022-09-30 19:32

1 Answers

imageView.contentMode=.scaleAspectFit
What happens if I add ?


2022-09-30 19:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.