Ios: Capture Image from Front Facing Camera

How to Fix capturing images with front facing camera mirrors image, AVFoundation,Swift

i figured this out myself, Here is the solution:

if captureDevice.position == AVCaptureDevicePosition.back {
if let image = context.createCGImage(ciImage, from: imageRect) {
return UIImage(cgImage: image, scale: UIScreen.main.scale, orientation: .right)
}
}

if captureDevice.position == AVCaptureDevicePosition.front {
if let image = context.createCGImage(ciImage, from: imageRect) {
return UIImage(cgImage: image, scale: UIScreen.main.scale, orientation: .leftMirrored)
}
}

Fix orientation of images taken with front camera ios

It turned out that i was changing the videoOrientation when rotation did change, which was wrong. I had to move that logic before capturing the photo. Now it works fine. Also I've fixed the mirroring problem, by setting isVideoMirrored to true if using the front camera.

LandscapeRight front camera photos capturing upside down

Start the configuration of the camera ...

    if captureSession.canAddOutput(videoOutput) {
captureSession.addOutput(videoOutput)
let connection = videoOutput.connectionWithMediaType(AVMediaTypeVideo)
if connection.supportsVideoOrientation {
connection.videoOrientation = isFrontCamera ? AVCaptureVideoOrientation.LandscapeLeft : AVCaptureVideoOrientation.LandscapeRight
}
print("adding output")

} else {
print("Could not add front video output")
return
}

captureSession.commitConfiguration()
captureSession.startRunning()

... set up the preview layer for the camera.

func setupPreviewLayer() {        
var previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
var bounds = cameraView.bounds
previewLayer.backgroundColor = UIColor.blackColor().CGColor
previewLayer.bounds = bounds
previewLayer.videoGravity = AVLayerVideoGravityResize
previewLayer.position = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds))
previewLayer.connection.videoOrientation = recordingOrientation;
self.cameraView.layer.addSublayer(previewLayer)

}

You could try a few work arounds one option being:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.currentDevice().orientation.isLandscape.boolValue {
print("Landscape")
} else {
print("Portrait")
}
}

Another work around would be to rotate and/or flip the image after the picture is taken.

IOS Swift 3: Flip Front Camera Image Horizontally after taking Camera Picture

You are flipping your image correctly but why are you assigning back your flipped image to picture variable you can use flippedImage variable and pass it where it is required.

func didTakePicture(_ picture: UIImage) {
var flippedImage = UIImage(CGImage: picture.CGImage!, scale: picture.scale, orientation: .leftMirrored)
// Here you have got flipped image you can pass it wherever you are using image
}

What I am trying to do is prevent the front camera image only from
being flipped

If you are using Default Camera then it is not possible to prevent the camera to prevent flipping the image. To do so either you need to create your own camera using AVFoundation and need to apply your logics there.

If you are ready to use any thirds party library then you can check LEMirroredImagePicker



Related Topics



Leave a reply



Submit