I'm developing application, where activity starts after phone's rotation. When you rotate phone backward that activity have to finish. Is it possible to ignore rotation around other axes?
You can check which way the phone is held with this:
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();
Then you can check the orientation like this:
switch (rotation)
{
case Surface.ROTATION_90:
...
break;
case Surface.ROTATION_180:
...
break;
case Surface.ROTATION_270:
...
break;
default:
...
break;
}
(Note that for pre-API 8 versions you'd need to use display.getOrientation() instead of display.getRotation())
Related
I am working with some camera-related features. For some of the devices (like a smart glass called Vuzix) the camera is flipped upside down, so when passing some attributes I need to do ROTATE.180 while for the others it is not flipped so I just need to pass ROTATE.NONE; I want to know if there is any way I could get the running device name/camera flipped or not in an if statement like (if device.name==Vuzix) or something like (if camera.orientation==reversed). Right now I have to manually change before running on each device.
Yeah Sure, you can easily detect on what device your code is running on. See this link : How to detect a mobile device manufacturer and model programmatically in Android?
You can get Device name and Manufacturer as follows :
String deviceName = android.os.Build.MODEL;
String deviceMan = android.os.Build.MANUFACTURER;
You can certainly put in a check for the manufacturer, but it is not the most robust solution. Vuzix glasses have a sensor orientation is upside-down compared to many other vendors, but there are others that use this same mounting besides Vuzix.
Luckily on Vuzix products, the camera characteristics reflect this, so you can use the sample code published by Android Developer Documentation. You query the Window Manager for the display orientation, and the camera Characteristics to find the sensor mounting. Then you can use the same code no matter what device you are using.
public static void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
I know there are already questions like this, but none solved it. I took some help and figured out basic code that I need to change the camera with respect to the display.
cameraInfo=new Camera.CameraInfo();
camera.getCameraInfo(camId,cameraInfo);
int angle=0;
int rotation = getActivity().getWindowManager().getDefaultDisplay()
.getRotation();
switch(rotation)
{
case Surface.ROTATION_0:
parameters.setPreviewSize(s.width, s.height);
Log.i(TAGR, "THE ROATTION IS 0");
camera.setDisplayOrientation(90);
break;
case Surface.ROTATION_90:
parameters.setPreviewSize(s.width, s.height);
Log.i(TAGR, "THE ROATTION IS 90");
camera.setDisplayOrientation(0);
break;
case Surface.ROTATION_180:
parameters.setPreviewSize(s.width, s.height);
Log.i(TAGR, "THE ROATTION IS 180");
camera.setDisplayOrientation(0);
break;
case Surface.ROTATION_270:
parameters.setPreviewSize(s.width, s.height);
Log.i(TAGR, "THE ROATTION IS 270");
camera.setDisplayOrientation(180);
break;
default:
camera.setDisplayOrientation(0);
break;
}
}
However, I am unable to get the correct display preview with respect to the rotation of the device. I have tried several different combination of angles. However, none gave me the correct output. I am attaching the images of my preview. Please explain the solution to the problem. Moreover, I want to know what position of my device corresponds to angles 0,90,180, and 270. In other words, is 0 corresponds to portrait or landscape orientation?.
Images :
The landscape is inverted in this one. I tried setting the rotation by 180 when ROTATION_180. Doing so did not work out, as the other orientation (in landscape) became inverted
The upside down orientation does not do anything. The desired result is to bring the click button down with correct preview. I don't have any idea why this is not working. I have tried several values in each rotation, but none solved this issue.
I am a beginner, so please explain in detail. Thanks.
You should check CameraInfo.orientation for the device. Note that this property gives rotation from the natural display orientation, given for the device by Display.getRotation().
If your custom camera app uses android.hardware.camera2 API, this compensation will happen automatically.
If your custom camera activity does not lock the screen orientation, then it will be subject to "reverse landscape" glitch, and needs special treatment which involves OrientationEventListener.
You can rotate the image like this before preview:
public static Bitmap rotateImage(Bitmap bitmapSrc) {
Matrix matrix = new Matrix();
matrix.postRotate(previewRotation);
return Bitmap.createBitmap(bitmapSrc, 0, 0,
bitmapSrc.getWidth(), bitmapSrc.getHeight(), matrix, true);
}
"previewRotation" can be got from:
private static void setCameraDisplayOrientation(Context mContext, android.hardware.Camera.CameraInfo info) {
int rotation = ((CameraActivity) mContext).getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
previewRotation = (info.orientation + degrees) % 360;
previewRotation = (360 - previewRotation) % 360; // compensate the mirror
} else { // back-facing
previewRotation = (info.orientation - degrees + 360) % 360;
}
mCameraInstance.setDisplayOrientation(previewRotation);
}
When we will trying to record the video using MediaRecorder video is recorded properly in android and in device it will display as like recording, but when we can play video in VLC or other player in Desktop that time it will rotate the video and it will not display properly.
and i can set the MediaRecorder setOrientationHint to 90 degree.
what's the problem for changing the orientation and Why?
we Can not directly apply fix orientation while capturing video. I mean to say that you used fix 90 degree orientation in MediaRecorder setOrientationHint. you need to set setOrientationHint(dynamic degree);
First of all you need to get display rotation and get angle using display rotation. after then set That Degree to setOrientationHint method. That will work for all. Here is code.
Display display = getWindowManager().getDefaultDisplay();
int mDisplayRotation = display.getRotation();
public int getDisplayOrientationAngle() {
Log.e("", "setDisplayOrientationAngle is call");
int angle;
// switch (MeasurementNativeActivity.DisplayRotation) {
switch (mDisplayRotation) {
case Surface.ROTATION_0: // This is display orientation
angle = 90; // This is camera orientation
break;
case Surface.ROTATION_90:
angle = 0;
break;
case Surface.ROTATION_180:
angle = 270;
break;
case Surface.ROTATION_270:
angle = 180;
break;
default:
angle = 90;
break;
}
Log.v("", "media recorder displayRotation: " + mDisplayRotation);
Log.v("", "media recorder angle: " + angle);
return angle;
}
mMediaRecorder.setOrientationHint(getDisplayOrientationAngle());
Extracted from the Android Documentation for MediaRecorder's setOrientationHint(int degrees) function:
This method will not trigger the source video frame to rotate during video recording, but to add a composition matrix containing the rotation angle in the output video if the output format is OutputFormat.THREE_GPP or OutputFormat.MPEG_4 so that a video player can choose the proper orientation for playback.
To sum it up, setOrientationHint just adds some sort of header to the video file that "tells" video players that they should rotate the video when playing it. In my experience, VLC player ignores this header and plays the video as it was recorded.
The only workaround I can think of would be to post-process the video by rotating it to your needs, although it seems quite a bad decision resource-wise.
I wanted to lock orientation of screen to its default orinetation. I have having problem on achieving this. Initially i locked screen to portrait from manifest. It works fine for portrait defaultdevices. But many tablets have landscape as default so in these devices locking to portrait is not suitable, i want to detect this default orientation and lock it. I mean if landscape is default orientation i want to lock the orientation to landscape and if its portrait then lock it to port. How to do this. I am stuck in this part. I don't want to support both orientation(auto). Please help
Thanks.
There is a default orientation for different devices for example the default orientation on a galaxy 10 tablet is different from a nexus 7 tablet. when you get the orientation of the display you get the following values :
said so, what you have to do in your locking method is the following:
public void mLockScreenRotation(FA_Padre actividad){
int buildVersionSDK = Build.VERSION.SDK_INT;
Display display = ((WindowManager) actividad.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int orientation=0;
if(buildVersionSDK >= 8){
orientation=display.getRotation();
}
/****************Phone*************************************/
if(buildVersionSDK < 8){// older Android versions with only two orientations
switch (actividad.getResources().getConfiguration().orientation){
case Configuration.ORIENTATION_PORTRAIT:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Configuration.ORIENTATION_LANDSCAPE:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
}
}else if((buildVersionSDK > 7 ) && (!GlobalInfo.isTablet())){// Newer Android phones with more than two orientations
switch(orientation){
case Surface.ROTATION_0:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_90:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_180:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_270:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
}
/****************TABLET*************************************/
}else{
int width = 0;
int height = 0;
switch(orientation){
/*If the default orientation of the device is landscape Rotation_0 and rotation_180 will be the case if the device is being held in landscape. if the default orientation of the device is portrait rotation_0 or 180 will only be the case if the device is in portrait mode*/
case Surface.ROTATION_0:
case Surface.ROTATION_180:
width = display.getWidth();
height = display.getHeight();
break;
/*the opposite in here*/
case Surface.ROTATION_90: //
case Surface.ROTATION_270:
width = display.getHeight();
height = display.getWidth();
break;
default:
break;
}
if(width > height){//Default ORIENTATION = LANDSCAPE, lock the screen in the current orientation
switch(orientation){
case Surface.ROTATION_0:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_90:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_180:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
case Surface.ROTATION_270:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
}
} else {//Default ORIENTATION = PORTRAIT, lock the screen in the current orientation
switch(orientation){
case Surface.ROTATION_0:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_90:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_180:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_270:
actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
}
}
}
}
try this one lock screen to default
Lockorientationactivity :
public class Lockorientationactivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int getConf=this.getResources().getConfiguration().orientation;
if(getConf==Configuration.ORIENTATION_PORTRAIT)
{
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Toast.makeText(getBaseContext(), "ORIENTATION_PORTRAIT", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(), "ORIENTATION_LANDSCAPE", Toast.LENGTH_SHORT).show();
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
setContentView(R.layout.main);
}
}
But you must set android:configChanges="orientation" for activity.
To lock the screen by code you have to use the actual rotation of the screen (0, 90, 180, 270) and you have to know the natural position of it, in a smartphone the natural position will be portrait and in a tablet, it will be landscape.
Here's the code (lock and unlock methods), it has been tested in some devices (smartphones and tablets) and it works great.
public static void lockScreenOrientation(Activity activity)
{
WindowManager windowManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
Configuration configuration = activity.getResources().getConfiguration();
int rotation = windowManager.getDefaultDisplay().getRotation();
// Search for the natural position of the device
if(configuration.orientation == Configuration.ORIENTATION_LANDSCAPE &&
(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) ||
configuration.orientation == Configuration.ORIENTATION_PORTRAIT &&
(rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270))
{
// Natural position is Landscape
switch (rotation)
{
case Surface.ROTATION_0:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_90:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_180:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
case Surface.ROTATION_270:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
}
}
else
{
// Natural position is Portrait
switch (rotation)
{
case Surface.ROTATION_0:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_90:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_180:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_270:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
}
}
}
public static void unlockScreenOrientation(Activity activity)
{
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
How can I detect which one of 4 sides of the phone is up.
I can detect portrait/landscape mode, but how do I tell landscape-turned-on-left-side from landscape-turned-on-right-side?
Basically I want to make a nice transition animation when user turns phone. You know, like in iPhone's Safari: a swift 400ms rotation from the previous layout to the new.
Use an OrientationEventListener:
mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL)
{
#Override
public void onOrientationChanged(int orientation)
{
mDeviceOrientation = orientation;
}
};
if(mOrientationEventListener.canDetectOrientation())
{
mOrientationEventListener.enable();
}
mDeviceOrientation should then be an integer telling you the angle your device is rotated to, if you do some clever rounding you should be able to see which of the four orientations it is in:
// Divide by 90 into an int to round, then multiply out to one of 5 positions, either 0,90,180,270,360.
int orientation = 90*Math.round(mDeviceOrientation / 90);
// Convert 360 to 0
if(orientation == 360)
{
orientation = 0;
}
Enjoy!
Just came across :) 2.2+ put xml code to ur res/values(false) and res/values-xlarge(true)
<resources>
<bool name="isTablet">false</bool>
private void getScreenRotationOnPhone() {
final Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
switch (display.getRotation()) {
case Surface.ROTATION_0:
System.out.println("SCREEN_ORIENTATION_PORTRAIT");
break;
case Surface.ROTATION_90:
System.out.println("SCREEN_ORIENTATION_LANDSCAPE");
break;
case Surface.ROTATION_180:
System.out.println("SCREEN_ORIENTATION_REVERSE_PORTRAIT");
break;
case Surface.ROTATION_270:
System.out.println("SCREEN_ORIENTATION_REVERSE_LANDSCAPE");
break;
}
}
private void getScreenRotationOnTablet() {
final Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
switch (display.getRotation()) {
case Surface.ROTATION_0:
System.out.println("SCREEN_ORIENTATION_LANDSCAPE");
break;
case Surface.ROTATION_90:
System.out.println("SCREEN_ORIENTATION_REVERSE_PORTRAIT");
break;
case Surface.ROTATION_180:
System.out.println("SCREEN_ORIENTATION_REVERSE_LANDSCAPE");
break;
case Surface.ROTATION_270:
System.out.println("SCREEN_ORIENTATION_PORTRAIT");
break;
}
}
private boolean isTabletDevice(){
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize) {
return true;
} else {
return false;
}
}
It seems like you should be able to tell from the screenOrientation, gotten via getRequestedOrientation
I was able to get landscape left vs right events using this code:
object : OrientationEventListener(this, SENSOR_DELAY_NORMAL) {
override fun onOrientationChanged(orientation: Int) {
afterGlobalLayout {
logInfoToast(if (screenRotation == LandscapeLeft)
"LandscapeLeft" else "LandscapeRight")
}
}
}.enable()
afterGlobalLayout uses viewTreeObserver.addOnGlobalLayoutListener
and screenRotation is calculated by Piotr Ĺšlesarew code.