Question

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

No correct solution

OTHER TIPS

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);

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);

You can try this (good for 2.2 or below). Here I rotate the image before saving it to sd card. But it is only for portrait mode. If you had to make it for both mode then you should check camera orientation and put some check before capturing image.

PictureCallback jpegCallback = new PictureCallback() {
   public void onPictureTaken(byte[] data, Camera camera) {
      FileOutputStream outStream = null;
      try {
        imageFilePath = getFilename();
        InputStream is = new ByteArrayInputStream(data);
        Bitmap bmp = BitmapFactory.decodeStream(is);
        // Getting width & height of the given image.
        if (bmp != null){
           int w = bmp.getWidth();
           int h = bmp.getHeight();
           // Setting post rotate to 90
           Matrix mtx = new Matrix();
           mtx.postRotate(90);
           // Rotating Bitmap
           Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
           ByteArrayOutputStream stream = new ByteArrayOutputStream();
           rotatedBMP.compress(Bitmap.CompressFormat.PNG, 100, stream);
           byte[] byteArray = stream.toByteArray(); 
           outStream = new FileOutputStream
                              (String.format(imageFilePath,System.currentTimeMillis()));
           outStream.write(byteArray);
           outStream.close();
        } else {
           outStream = new FileOutputStream
                              (String.format(imageFilePath,System.currentTimeMillis()));
           outStream.write(data);
           outStream.close();           
        }       

        preview.camera.startPreview();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
    }
  }
};

There's no way to do this on many current devices, including the G1 and Droid. Take a look at the relevant bug report here:

Also see a comment from one of the Android engineers (Dave) here:

The link Roman gave of the issue thread has a workable solution that I'm using now.

Find it here: http://code.google.com/p/android/issues/detail?id=1193#c26

There is no need you have to set any parameters for the orientation until you need to do that explicitly. By Default, it supports this facility. In my case, i have a Activity and above that activity i have a camera view, so i didn't set any orientation for the camera properties, instead for the activity i set the orientation as portrait in the Manifest file. now the app looks and works good. Might be helpful for some one..

Thanks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow