Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Camera in Portrait on SurfaceView

Tags:

android

I tried several things to try to get the camera preview to show up in portrait on a SurfaceView. Nothing worked. I am testing on a Droid that has 2.0.1. I tried:

1) forcing the layout to be portrait by: this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

2) using

Camera.Parameters parameters = camera.getParameters(); parameters.set("orientation", "portrait"); parameters.setRotation(90); camera.setParameters(parameters); 

Is there something else I can try? If this a bug in Android or the phone how can I make sure that this is the case so that I have proof to inform the client?

Thanks, Prasanna

like image 748
Prasanna Avatar asked Mar 30 '10 05:03

Prasanna


People also ask

How do I fix the camera orientation on my Android?

Find and turn on the "Auto-rotate" tile in the quick-setting panel. You can also go to Settings > Display > Auto-rotate screen to turn it on. Your phone screen should rotate automatically now if nothing is wrong with the sensors.

What is SurfaceView?

Provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface and, if you like, its size; the SurfaceView takes care of placing the surface at the correct location on the screen.


2 Answers

As of API lvl 8, this is available:

public final void setDisplayOrientation (int degrees)

i.e. with portait in the manifest:

public void surfaceCreated(SurfaceHolder holder) {     mCamera = Camera.open();     mCamera.setDisplayOrientation(90); 
like image 96
Ed Jellard Avatar answered Sep 30 '22 16:09

Ed Jellard


i have a working solution for portrait mode working in 2.1 (tested on Desire) maybe less.

Activity screen orientation is set to portrait. (android:screenOrientation="portrait")

the camera parameters:

Camera.Parameters p = mCamera.getParameters();

 p.set("jpeg-quality", 100);  p.set("orientation", "landscape");  p.set("rotation", 90);  p.setPictureFormat(PixelFormat.JPEG);  p.setPreviewSize(h, w);// here w h are reversed  mCamera.setParameters(p); 

and the image will be portrait.

SurfaceHolder you use for camera must be at a size compatible with phone preview size usualy screen resolution.

Funny on Desire 2.2 is not working... Here is the fix:

   At surfaceCreated(..) or when you have this line camera = Camera.open();        add camera.setDisplayOrientation(90);//only 2.2>  Camera.Parameters p = camera.getParameters();     p.set("jpeg-quality", 100); p.setRotation(90); p.setPictureFormat(PixelFormat.JPEG); p.setPreviewSize(h, w); camera.setParameters(p); 
like image 26
valientinx Avatar answered Sep 30 '22 15:09

valientinx