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

Landscape orientation on Android App. #568

Closed
tagy opened this issue Mar 31, 2020 · 5 comments
Closed

Landscape orientation on Android App. #568

tagy opened this issue Mar 31, 2020 · 5 comments
Assignees
Labels
platform:android Issues with Android as Platform

Comments

@tagy
Copy link

tagy commented Mar 31, 2020

I have built some of the demo android apps and they work correctly.

But they do have android:screenOrientation="portrait" in the manifest so so not support landscape orientation.

I have also built an aar library and built a similar app myself without the orientation restriction in the manifest. But when I rotate the device the camera preview is distorted and rotated 90 degrees.

Can anyone point me in the right direction of how I could fix landscape mode? Thanks.

@eknight7
Copy link

Yes, all the Android example apps use Portrait mode.
To change the app to work in landscape mode, you will have to change the code for handling the preview to appropriately handle size when camera rotation changes in CameraXPreviewHelper.java and MainActivity.java of your app.

@mzwtjp
Copy link

mzwtjp commented Jul 27, 2020

I did something like this.
In my app code,

wm : WindowManager
converter: ExternalTextureConverter

rotation = WindowManager.getDefaultDisplay().getRotation()
converter.setRotation((360 - rotation) % 360)

Then in ExternalTextureRenderer.java

//ADD
  private int rotation = 0;
  public void setRotation(int rotation) {
    this.rotation = rotation;
  }
//ADD

  public void render(SurfaceTexture surfaceTexture) {
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    ShaderUtil.checkGlError("glActiveTexture");
    surfaceTexture.updateTexImage(); // This implicitly binds the texture.
    surfaceTexture.getTransformMatrix(textureTransformMatrix);
//ADD
    switch (rotation) {
      case 0:
        break;
      case 90:
        Matrix.rotateM(textureTransformMatrix, 0, 90, 0, 0, 1);
        Matrix.translateM(textureTransformMatrix, 0, 0, -1, 0);
        break;
      case 180:
        Matrix.rotateM(textureTransformMatrix, 0, 180, 0, 0, 1);
        Matrix.translateM(textureTransformMatrix, 0, -1, -1, 0);
        break;
      case 270:
        Matrix.rotateM(textureTransformMatrix, 0, 270, 0, 0, 1);
        Matrix.translateM(textureTransformMatrix, 0, -1, 0, 0);
        break;
      default:
        //unknown
    }
//ADD

@yeon1216
Copy link

yeon1216 commented Aug 13, 2020

@mzwtjp thanks 👍
but WindowManager.getDefaultDisplay() was deprecated.
How can I get rotation?

image

@Christy-V
Copy link

Christy-V commented Sep 29, 2021

I am facing the same issue, what should i do in android studio?
@tagy @eknight7

@vinayak19th
Copy link

vinayak19th commented Nov 22, 2021

@Christy-V I found the fix to this. It requires 3 changes for the input of mediapipe to be handled:

  1. Modify the CameraXPreviewHelper.java
    In line 222:
-     Size rotatedSize =
-        new Size(/* width= */ targetSize.getHeight(), /* height= */ targetSize.getWidth()); 
+        Size rotatedSize = targetSize;
  1. Modify the ExternalTextureRenderer.java
    In line 69
- private int rotation = Surface.ROTATION_0;
+ private int rotation = Surface.ROTATION_270;

Surface.ROTATION_270 might need to be replaced by Surface.ROTATION_90 for certain camera configurations based on vertical flipping

  1. Modify the AndroidManifest.xml for the Basic or any additional you create
    Add the following to Every Activity in the manifest
android:screenOrientation="landscape"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform:android Issues with Android as Platform
Projects
None yet
Development

No branches or pull requests

7 participants