Java – when my application is in portrait, use the camera in landscape
I have tried various ways to achieve the behavior I want, but there seems to be no work
My application is locked in portrait mode (I don't want landscape UI), but I want to be able to switch the camera display to alternately display the camera preview in portrait and landscape modes
I'm not interested in taking pictures
To display the preview vertically, I just need to:
camera.setDisplayOrientation(90);
My preview works normally, but when I try to display the camera preview in landscape mode, I will:
camera.setDisplayOrientation(0);
And the preview is actually rotated, but the image from the camera does not rotate, and the final result is only the portrait image rotated to the horizontal
I've tried various ways:
1) Use parameters Setrotation() rotates the camera, but this only affects the saved final image, not the preview;
2) Set parameters Set ("orientation", "landscape") but it seems to have no effect (it may be an old command that has not been supported yet?);
3) Set each combination of the following methods, but no, the only way to effectively rotate the image from the camera seems to be to physically rotate my device
This is a practical demonstration of what I mean:
As you can see, parameters Setrotation() has no effect on the preview, which is correct in portrait, but only in landscape, because the camera does not rotate itself, but only rotates the portrait image
So, when the application is in the portrait state, is there a way to display the camera preview horizontally?
thank you.
Solution
View this method to set the orientation of the camera The parameter direction is display Getrotation(), you can change the settings according to your requirements:
public void setCameraOrientation(int orientation) { int orientationDegree = 0; switch (orientation) { case Surface.ROTATION_0: orientationDegree = 0; break; case Surface.ROTATION_90: orientationDegree = 90; break; case Surface.ROTATION_180: orientationDegree = 180; break; case Surface.ROTATION_270: orientationDegree = 270; break; } int displayOrientation = (cameraInfo.orientation - orientationDegree + 360) % 360; camera.setDisplayOrientation(displayOrientation); Camera.Parameters parameters = camera.getParameters(); parameters.setRotation(displayOrientation); camera.setParameters(parameters); }
For the entire context, see my camera demo application here Specifically, the camerapreview class