CameraView icon indicating copy to clipboard operation
CameraView copied to clipboard

[FREQ] Cropped preview?

Open feelingsonice opened this issue 1 year ago • 5 comments

Is there a way to crop the preview and capture photos exactly like the cropped preview?

feelingsonice avatar Jun 27 '24 20:06 feelingsonice

@feelingsonice, I think you should implement Custom Preview for that

MCameraController()
    .outputType(.video)
    .gridVisible(false)
    .cameraPosition(.back) 
    .mediaPreviewScreen(CroppedPreview.init)
                
struct CroppedPreview: MCameraPreview {
    let capturedMedia: MijickCameraView.MCameraMedia
    let namespace: Namespace.ID
    let retakeAction: () -> ()
    let acceptMediaAction: () -> ()
// crop implementation
}

deden avatar Jun 28 '24 02:06 deden

@deden any tip on how to actually do that? I ran into a wall trying to do that with my own AVCaptureVideoPreviewLayer and couldn’t figure it out. Hence I’m now trying to see if there’s any third party libraries that could support this :(

feelingsonice avatar Jun 28 '24 03:06 feelingsonice

@feelingsonice, were you able to solve your problem on your own?

If not please let me know, I'll try to create a demo project for you this week.

FulcrumOne avatar Jul 01 '24 11:07 FulcrumOne

I ended up not actually "cropping" the preview but fitFill a smaller rectangle on screen. When the user captures a photo, I crop it to the preview frame with something like this.

I think this works fine for my use case but would love to know how to actually crop out of curiosity. If it's a lot of work for you don't worry about it though :)

Thanks for the help!

feelingsonice avatar Jul 01 '24 23:07 feelingsonice

My guess is that because CameraBridgeView goes offscreen because it doesn't maintain a proper aspect ratio.

Here is how I fixed it:


import SwiftUI
import AVFoundation

public struct DefaultCameraScreen: MCameraScreen {
    @ObservedObject public var cameraManager: CameraManager
    public let namespace: Namespace.ID
    public let closeMCameraAction: () -> ()
    var config: Config = .init()


    public var body: some View {
        VStack {
            createTopBar()
            Spacer()
            createContentView()
                // hardcode aspect ratio is good enough for my case, but I am not sure about you
                .aspectRatio(3.0 / 4.0, contentMode: .fit)
            Spacer()
            createBottomBar()
        }
        .ignoresSafeArea()
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color(.mijickBackgroundPrimary).ignoresSafeArea())
        .statusBarHidden()
        .animation(.mSpring)
    }
}
private extension DefaultCameraScreen {
    func createTopBar() -> some View {
        DefaultCameraScreen.TopBar(parent: self)
            .frame(alignment: .top)
    }
    func createContentView() -> some View {
        createCameraOutputView()
            .ignoresSafeArea()
    }
    func createBottomBar() -> some View {
        DefaultCameraScreen.BottomBar(parent: self)
            .frame(alignment: .bottom)
    }
}

bogdan avatar Apr 04 '25 14:04 bogdan