mardi 8 avril 2014

Android : Orientation/Rotation de l'écran pour caméra Preview - débordement de pile


I have created a camera app and I want my app to be turned in all 4 possible orientations and the to update the camera preview accordingly. for that I have used the following method I have copied from: Android - Camera preview is sideways


 public void updateCameraDisplay(int w, int h) {
// set preview size and make any resize, rotate or
// reformatting changes here

Log.i("CameraPreviews", "Updating camera orientation with w=" + w
+ " and h=" + h);
Parameters parameters = camera.getParameters();
Display display = getActivity().getWindowManager()
.getDefaultDisplay();

int rotation = getActivity().getResources().getConfiguration().orientation;
Log.i("CameraPreviews", "rotation is " + display.getRotation());
if (display.getRotation() == Surface.ROTATION_0) {

parameters.setPreviewSize(h, w);
camera.setDisplayOrientation(0);
}

if (display.getRotation() == Surface.ROTATION_90) {
parameters.setPreviewSize(w, h);
camera.setDisplayOrientation(270);
}

if (display.getRotation() == Surface.ROTATION_180) {
parameters.setPreviewSize(h, w);
camera.setDisplayOrientation(180);
}

if (display.getRotation() == Surface.ROTATION_270) {
parameters.setPreviewSize(w, h);
camera.setDisplayOrientation(90);
}

try {
camera.setParameters(parameters);
} catch (Exception e) {
e.printStackTrace();
}
}
}

I have tweeked the values, testing them on the samsung galaxy tab2 to finally get the right orientations and it all works. When I tried it on htc one s phone it doesn't work at all!!!!! All e orientations are totally wrong! So I have arrived to the conclusion that thre must be 2 type of devices (or more... please no!) because the rotation represents how many degrees the screen has been rotated from its "default" position then some devices have one default position and others another. How could I find out about this default rotation and act accordingly in my code? ej: defaultOrientation=some code if(defaultOrientation==0) ... else ....


locking screen orientation is out of question. target api>=11 thanks a lot


EDIT: I have modified my code to:


public void updateCameraDisplay(int w, int h) {
// set preview size and make any resize, rotate or
// reformatting changes here

Log.i("CameraPreviews", "Updating camera orientation with w=" + w
+ " and h=" + h);
Parameters parameters = camera.getParameters();
Display display = getActivity().getWindowManager()
.getDefaultDisplay();

int rotation = getActivity().getResources().getConfiguration().orientation;
Log.i("CameraPreviews", "screen rotation is " + rotation);
Log.i("CameraPreviews", "display rotation is " + display.getRotation());
if (display.getRotation() == Surface.ROTATION_0) {

if (rotation == Configuration.ORIENTATION_LANDSCAPE) {
parameters.setPreviewSize(h, w);
camera.setDisplayOrientation(0);
} else {
parameters.setPreviewSize(h, w);
camera.setDisplayOrientation(90);
}
}

else if (display.getRotation() == Surface.ROTATION_90) {
if (rotation == Configuration.ORIENTATION_PORTRAIT) {
parameters.setPreviewSize(w, h);
camera.setDisplayOrientation(270);
} else {
parameters.setPreviewSize(w, h);
//camera.setDisplayOrientation(0);
}
}

else if (display.getRotation() == Surface.ROTATION_180) {
if (rotation == Configuration.ORIENTATION_LANDSCAPE) {
parameters.setPreviewSize(h, w);
camera.setDisplayOrientation(180);
}else {
parameters.setPreviewSize(h, w);
camera.setDisplayOrientation(270);
}
}

else if (display.getRotation() == Surface.ROTATION_270) {
if (rotation == Configuration.ORIENTATION_PORTRAIT) {
parameters.setPreviewSize(w, h);
camera.setDisplayOrientation(90);
} else {
parameters.setPreviewSize(w, h);
camera.setDisplayOrientation(180);
}
}

try {
camera.setParameters(parameters);
} catch (Exception e) {
e.printStackTrace();
}
}

works better on htc one s and samsung galaxy tab as long as we don't rotate the phone in the portrait mode upside down



I have created a camera app and I want my app to be turned in all 4 possible orientations and the to update the camera preview accordingly. for that I have used the following method I have copied from: Android - Camera preview is sideways


 public void updateCameraDisplay(int w, int h) {
// set preview size and make any resize, rotate or
// reformatting changes here

Log.i("CameraPreviews", "Updating camera orientation with w=" + w
+ " and h=" + h);
Parameters parameters = camera.getParameters();
Display display = getActivity().getWindowManager()
.getDefaultDisplay();

int rotation = getActivity().getResources().getConfiguration().orientation;
Log.i("CameraPreviews", "rotation is " + display.getRotation());
if (display.getRotation() == Surface.ROTATION_0) {

parameters.setPreviewSize(h, w);
camera.setDisplayOrientation(0);
}

if (display.getRotation() == Surface.ROTATION_90) {
parameters.setPreviewSize(w, h);
camera.setDisplayOrientation(270);
}

if (display.getRotation() == Surface.ROTATION_180) {
parameters.setPreviewSize(h, w);
camera.setDisplayOrientation(180);
}

if (display.getRotation() == Surface.ROTATION_270) {
parameters.setPreviewSize(w, h);
camera.setDisplayOrientation(90);
}

try {
camera.setParameters(parameters);
} catch (Exception e) {
e.printStackTrace();
}
}
}

I have tweeked the values, testing them on the samsung galaxy tab2 to finally get the right orientations and it all works. When I tried it on htc one s phone it doesn't work at all!!!!! All e orientations are totally wrong! So I have arrived to the conclusion that thre must be 2 type of devices (or more... please no!) because the rotation represents how many degrees the screen has been rotated from its "default" position then some devices have one default position and others another. How could I find out about this default rotation and act accordingly in my code? ej: defaultOrientation=some code if(defaultOrientation==0) ... else ....


locking screen orientation is out of question. target api>=11 thanks a lot


EDIT: I have modified my code to:


public void updateCameraDisplay(int w, int h) {
// set preview size and make any resize, rotate or
// reformatting changes here

Log.i("CameraPreviews", "Updating camera orientation with w=" + w
+ " and h=" + h);
Parameters parameters = camera.getParameters();
Display display = getActivity().getWindowManager()
.getDefaultDisplay();

int rotation = getActivity().getResources().getConfiguration().orientation;
Log.i("CameraPreviews", "screen rotation is " + rotation);
Log.i("CameraPreviews", "display rotation is " + display.getRotation());
if (display.getRotation() == Surface.ROTATION_0) {

if (rotation == Configuration.ORIENTATION_LANDSCAPE) {
parameters.setPreviewSize(h, w);
camera.setDisplayOrientation(0);
} else {
parameters.setPreviewSize(h, w);
camera.setDisplayOrientation(90);
}
}

else if (display.getRotation() == Surface.ROTATION_90) {
if (rotation == Configuration.ORIENTATION_PORTRAIT) {
parameters.setPreviewSize(w, h);
camera.setDisplayOrientation(270);
} else {
parameters.setPreviewSize(w, h);
//camera.setDisplayOrientation(0);
}
}

else if (display.getRotation() == Surface.ROTATION_180) {
if (rotation == Configuration.ORIENTATION_LANDSCAPE) {
parameters.setPreviewSize(h, w);
camera.setDisplayOrientation(180);
}else {
parameters.setPreviewSize(h, w);
camera.setDisplayOrientation(270);
}
}

else if (display.getRotation() == Surface.ROTATION_270) {
if (rotation == Configuration.ORIENTATION_PORTRAIT) {
parameters.setPreviewSize(w, h);
camera.setDisplayOrientation(90);
} else {
parameters.setPreviewSize(w, h);
camera.setDisplayOrientation(180);
}
}

try {
camera.setParameters(parameters);
} catch (Exception e) {
e.printStackTrace();
}
}

works better on htc one s and samsung galaxy tab as long as we don't rotate the phone in the portrait mode upside down


0 commentaires:

Enregistrer un commentaire