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

❓ Trying to taking photos in landscape mode #213

Closed
Enzo-Amorim opened this issue Jun 18, 2021 · 8 comments · Fixed by #301
Closed

❓ Trying to taking photos in landscape mode #213

Enzo-Amorim opened this issue Jun 18, 2021 · 8 comments · Fixed by #301
Labels
💭 question Further information is requested

Comments

@Enzo-Amorim
Copy link

Hello, i'm trying to take photos in landscape but always the photo is on portrait mode, have a way to change this?

@Enzo-Amorim Enzo-Amorim added the 💭 question Further information is requested label Jun 18, 2021
@mrousavy
Copy link
Owner

Hi! Thanks for the report, I'll start working on landscape mode soon, but it doesn't have very high priority since our app only supports portrait orientation. Related to #210

@Enzo-Amorim
Copy link
Author

oh, ok man, thanks, you know some kind of workaround that i can do to this?

@LupulescuAlexandru
Copy link

LupulescuAlexandru commented Jun 18, 2021

You'd need to bind an orientation listener when you setup the camera and update the camera rotation accordingly. It'd look something like this (snippet taken from react-native-camera-kit):

//Android
orientationListener = object : OrientationEventListener(context, SensorManager.SENSOR_DELAY_UI) {
                override fun onOrientationChanged(orientation: Int) {
                    val imageCapture = imageCapture ?: return
                    var newOrientation: Int = imageCapture.targetRotation
                    if (orientation >= 315 || orientation < 45) {
                        newOrientation = Surface.ROTATION_0
                    } else if (orientation in 225..314) {
                        newOrientation = Surface.ROTATION_90
                    } else if (orientation in 135..224) {
                        newOrientation = Surface.ROTATION_180
                    } else if (orientation in 45..134) {
                        newOrientation = Surface.ROTATION_270
                    }
                    if (newOrientation != imageCapture.targetRotation) {
                        imageCapture.targetRotation = newOrientation
                        onOrientationChange(newOrientation)
                    }
                }
            }

I had the same problem as you, I wanted to solve it, but unfortunately Vision Camera relies on coroutines and I'm not too familiar with them.
If it's urgent and you don't need the extra features Vision Camera gives you, you could use the react-native-camera-kit package instead of react-native-vision-camera, orientation changes are supported out of the box.

FYI: In case you need to fix orientations (e.g. set the camera only in landscape) as well, you could use my fork of react-native-camera-kit: https://github.com/LupulescuAlexandru/react-native-camera-kit.

@dmarkow
Copy link

dmarkow commented Jul 9, 2021

This library is nearly perfect for us, especially considering none of the other popular libraries seem to support the iPhone triple camera setup. Orientation support has been the only sticking point for us so far, since 95% of our photos are taken in landscape.

To get this working, I was able to patch react-native-vision-camera@2.4.1 in our project, and it seems to be working out. This new code runs right before photoOutput.capturePhoto to set the video capture orientation. I haven't done anything for Android (we're not using it), and it would need an option like autoOrientation to decide when to use this or not. Note that the landscapeRight/Left opposites are not typos. Apparently device orientations and interface orientations have opposite meanings of landscapeRight and landscapeLeft. This Swift code might also be hot garbage since I have very limited experience with it...

diff --git a/node_modules/react-native-vision-camera/ios/CameraView+TakePhoto.swift b/node_modules/react-native-vision-camera/ios/CameraView+TakePhoto.swift
index be988bc..8c516ae 100644
--- a/node_modules/react-native-vision-camera/ios/CameraView+TakePhoto.swift
+++ b/node_modules/react-native-vision-camera/ios/CameraView+TakePhoto.swift
@@ -103,6 +103,20 @@ extension CameraView {
         photoSettings.isAutoContentAwareDistortionCorrectionEnabled = enableAutoDistortionCorrection
       }
 
+      // use actual device orientation
+      if let photoOutputConnection = photoOutput.connection(with: .video) {
+        switch UIDevice.current.orientation {
+        case .landscapeLeft:
+          photoOutputConnection.videoOrientation = .landscapeRight
+        case .landscapeRight:
+          photoOutputConnection.videoOrientation = .landscapeLeft
+        case .portraitUpsideDown:
+          photoOutputConnection.videoOrientation = .portraitUpsideDown
+        default:
+          photoOutputConnection.videoOrientation = .portrait
+        }
+      }
+      
       photoOutput.capturePhoto(with: photoSettings, delegate: PhotoCaptureDelegate(promise: promise))
 
       // Assume that `takePhoto` is always called with the same parameters, so prepare the next call too.

@mrousavy
Copy link
Owner

mrousavy commented Jan 4, 2022

Implemented in #715! Hope this works for you :) If you appreciate the work I do in my free time, please consider sponsoring me on GitHub, it would mean a lot to me. 🖤

@dmarkow
Copy link

dmarkow commented Jan 5, 2022

@mrousavy Thanks! As far as I can tell, this doesn't detect orientation though? In the native iOS camera app, the UI stays portrait, but the camera knows when you've rotated the device. Using this new prop, if we wanted to do the same, would we have to somehow track device orientation separately and pass that into the orientation prop? (whereas the patch I posted above detected orientation automatically and independently of the UI rotation restrictions).

@mrousavy
Copy link
Owner

mrousavy commented Jan 6, 2022

Exactly, you can override orientation yourself depending on the device orientation, so it's fully up to you how to handle it. With your patch, it only overrides the orientation for takePhoto and only on iOS, and there's no way to prevent this behaviour (e.g. for apps like Snapchat or Instagram)

@mrousavy
Copy link
Owner

Hey - I'm tracking Orientation in this feature request/issue now: #1891

Make sure to upvote or sponsor to support this feature, and leave a comment if you have any additional thoughts/ideas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💭 question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants