I would like to prevent my camera X preview from rotation when my device orientation changes, but the rest of my UI should be rotated (landscape or portrait).
I have actually built my app in Flutter but i have implemented the camera X in Kotlin.
I can't just "prevent" the screen orientation changes as i actually want my UI to be rotated.
The only alternative i have found is building 2 UIs myself (portrait / landscape) and rotating when listening to a gyroscopic event...
I have tried to do the following without success :
<androidx.camera.view.PreviewView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:screenOrientation="portrait"
android:id="#+id/camera_view"/>
I am looking for a method such as :
Preview preview = new Preview.Builder().setTargetResolution(size).setTargetRotation(Surface.ROTATION_90).build();
preview.lockOrientation(); // This line
preview.setSurfaceProvider(cameraPreview.getSurfaceProvider());
Thanks in advance
Related
The precondition is we have to let Activity rotate follow the system orientation, so i add following code to the AndroidManifest.xml:
android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenLayout|screenSize|uiMode"
android:screenOrientation="sensor"
And set the rotationAnimation to ROTATION_ANIMATION_SEAMLESS.
WindowManager.LayoutParams windowAttributes = getWindow().getAttributes(); windowAttributes.rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_SEAMLESS; getWindow().setAttributes(windowAttributes);
I reset the correct glViewport, projection matrix and model matrix in surfaceChanged(),the proplem is that serveral frames looks weired while change the device orientation, but after rotation is complete, all frames look good.
This image shows what's going on:
It tried to reset viewport and matrix while onConfigurationChanged() is invoked, but it doesn't work.
I want the frames look seamless while change the device orientation, just as the Pixels' camera APP did.
It's simple: it's not possibile to avoid this completly.
You could let the SurfaceView invisible while rotation is not completed, but the Surface cannot renders not-yet-existent pixels for fill the new space that is created when the "landscape mode" is just started: Surface/View should adapts itself and it takes few milliseconds which are few frames as you reported (more the Device is slow/cheap and more frames are affected by this behavious/issue) expecially for the Camera to get new pixels from a different orientation.
I am creating an app where I need to manually set screen's orientation to landscape and I've encountered a problem when I rotate device clockwise.
Case which works: Imagine holding a phone in portrait mode and then start rotating it counterclockwise. When you reach approximately half a way, phone would normally switch to landscape.
Case which doesn't work: The same situation but phone will be rotated clockwise. When you reach almost half a way, phone would rotate to landscape but it would look like the landscape where I rotated phone counterclockwise and not clockwise. Everything would be opposite. If I rotate a little bit more, then it would switch to correct landscape.
My question is why is that happening in only one direction? I am setting my landscape orientation like this:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
This should allow activity to know in which orientation it is. What do you think?
Is there a way to get the device orientation in Android even if the interface orientation remains the same? I turn the device 180 degrees, the UI is upside down. But I still want to be notified.
You should set up an OrientationEventListener in the Activity you want to be notified of changes in.
Then you will have to specify the desired ScreenOrientation in the manifest for that Activity to prevent the screen from rotating.
In my camera app, the UI orientation is fixed to portrait.
the preview displayed correctly on both landscape and portrait by setting camera.setdisplayorientation(90).
I can take picture and save it to file.
when I display it by ImageView,
the picture that was taken in landscape orientation could display correctly.
(the top of phone in my left side)
But... the one that was taken in portrait orientation is not so lucky...
It looks like turn 90 degrees to left.
I try to detect the device orientation through sensor so that I can set the EXIF header,
but... so far...that too hard to achieve for me...does any one can help me to solve this problem?
Use the getRotation method:
Display display = ((WindowManager)
context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();
From the documentation:
Returns the rotation of the screen from its "natural" orientation.
The returned value may be Surface.ROTATION_0 (no rotation), Surface.ROTATION_90, Surface.
ROTATION_180, or Surface.ROTATION_270.
For example, if a device has a naturally tall screen, and the user has turned it on its side to go into a landscape orientation, the value returned here may be either Surface.ROTATION_90 or Surface.ROTATION_270 depending on the direction it was turned.
The angle is the rotation of the drawn graphics on the screen, which is the opposite direction of the physical rotation of the device.
For example, if the device is rotated 90 degrees counter-clockwise, to compensate rendering will be rotated by 90 degrees clockwise and thus the returned value here will be Surface.ROTATION_90.
getRotation was introduced from Android 2.2. Use getOrientation if your target are older devices.
Got answer form here:
how to detect orientation of android device?
If you just want to detect orientation of the device then you can use an OrientationEventListener.
here's the official docs:
http://developer.android.com/reference/android/view/OrientationEventListener.html#onOrientationChanged(int)
But before you use anything, check out this excellent blog post about orientation handling in android:
http://android-developers.blogspot.in/2010/09/one-screen-turn-deserves-another.html
I have developed one application and configured its orientation is Landscape so it will always display on landscape view on the device.
Now i want to rotate it 180 degree upside down when user rotate the device 180 degree upside down so it will adjust accordingly and displays to the user properly.At present if I am rotating device 180 degree upside down, My application doesn't adjust accordingly as per the rotation so it will display in reverse (from bottom to top) for example: suppose my activity is having 2 text fields on the top and 2 buttons on the bottom so if you rotate device 180 degree upside down, Activity is not adjusting accordingly, so user can see buttons on the top and text fields are on bottom which is wrong it should adjust/rotate as per the device rotation and must display text fields on the top and buttons on bottom.
Please provide your valuable solutions to resolve this problem.
Regards,
Piks
specify:
android:screenOrientation="sensorLandscape"
in your AndroidManifest.xml. This will transform application between landscape and reverseLandscape.
In your AndroidManifest.xml file, you need to configure the <activity> to use the orientation from the sensor. This should be the default, but you can force it to the sensor's orientations, for all 4 possible orientations, with android:screenOrientation="fullSensor". See http://developer.android.com/guide/topics/manifest/activity-element.html#screen
EDIT: If you want to enable all but one orientation, you could disable that orientation by intercepting the orientation change event and quashing it in your Activity:
public void onConfigurationChanged(Configuration config) {
if (config.orientation != Activity.ORIENTATION_PORTRAIT) {
setRequestedOrientation(config.orientation);
{
}
(This is off the top of my head but think it works, or something nearly like it.)
You need to tell Android to let the app handle orientation changes too in your <activity> with android:configChanges="orientation".