Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating custom camera with square view on iOS

I am trying to create a custom camera experience on iOS and the following code snippet is as far as I got. Basically I want the usual camera view (i.e. with the following buttons: capture, flash, grid, front/back, cancel). But the only difference between the normal camera and mine is that I want a square for the preview surface; not a rectangle. And then, what you see is what you get (WYSIWYG) such that there is no cropping necessary; as the user would have taken a square picture in the first place.

I have also been looking at the library https://github.com/danielebogo/DBCamera but I am not seeing how to customize it to my end. Any help? Thanks.

MY CODE SO FAR:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //Capture Session
    AVCaptureSession *session = [[AVCaptureSession alloc]init];
    session.sessionPreset = AVCaptureSessionPresetPhoto;

    //Add device
    AVCaptureDevice *device =
    [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    //Input
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];

    if (!input)
    {
        NSLog(@"No Input");
    }

    [session addInput:input];

    //Output
    AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
    [session addOutput:output];
    output.videoSettings =
    @{ (NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA) };

    //Preview Layer
    AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    UIView *myView = self.view;
    previewLayer.frame = myView.bounds;
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer:previewLayer];

    //Start capture session
    [session startRunning];
}

This is the only custom code in a single view project on Xcode

like image 242
Katedral Pillon Avatar asked Jan 07 '15 16:01

Katedral Pillon


People also ask

Do I need to create a custom camera view?

However, when you need style and functionality that goes beyond the stock Image Picker Controller you will need to create a Custom Camera View. Add the following view elements to the ViewController in Storyboard:

What is camera manager for iOS?

With Camera Manager, the developer can create beautiful custom UIs and achieve awesome results with photos, without reinventing the wheel. Camera Manager provides a simple custom iOS camera view to capture photos and record videos easily, with the following features: front/rear camera selection;

How do I Capture the current frame from the camera?

When the TakePhotoButtonTapped event is raised, we can capture the current frame shown from the camera by tapping into the AVCaptureStillImageOutput object we configured earlier in SetupLiveCameraStream. Once we have a connection to the camera, we can capture an image and convert the resulting buffer to type NSData, and finally to a byte array:

Can I build my own camera control using AVFoundation?

The remainder of this blog post, though, will detail exactly how to build your own camera control using AVFoundation. AVFoundation is a namespace that contains classes for high-level recording and playback capabilities for audio and video on iOS.


1 Answers

You have two options for doing what you want, either stick with and customize a UIImagePickerController, or create your own by using the AVFoundation.

The UIImagePickerController does provide a fair bit of customization options, and this similar thread has some good information on that: link.

If you still want to make your own, I suggest heading over to the Apple Documentation and checking out this demo project called AVCam: link. However, it's way more in-depth than you'll probably need so I can recommend this video tutorial as well: link.

If going for the last option, I would like to mention that to make the "actual camera" fit the frame of your previewLayer, you can set the videoGravity on the AVCaptureVideoPreviewLayer to AVLayerVideoGravityResizeAspectFill.

like image 178
Aleksander Avatar answered Oct 09 '22 22:10

Aleksander