1

I'm using camera to show preview. The app is always in portrait mode (landscape mode is disabled). The camera preview is always rotated 90 degrees ccw and I can't change it (neither with setDisplayOrientation nor with p.set("orientation", "portrait" ) and p.set("rotation", 90) ).

Is the preview ALWAYS rotated like this or is it device dependent? If it was always like this in portrait mode, I could just rotate the image afterwards.

I hold my phone in "portrait mode" but the cameraPreview is in mode landscape (i.e. I see something rotated by 90 degrees).

Works fine on many devices LG Optimus Samsung Galaxy S2

Issues for some samsung devices (SAMSUNG GALAXY ACE DUOS)

                             /*
            Copyright Sergi Martínez (@sergiandreplace)

            Licensed under the Apache License, Version 2.0 (the "License");
            you may not use this file except in compliance with the License.
            You may obtain a copy of the License at

                http://www.apache.org/licenses/LICENSE-2.0

            Unless required by applicable law or agreed to in writing, software
            distributed under the License is distributed on an "AS IS" BASIS,
            WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
            See the License for the specific language governing permissions and
            limitations under the License.

            */

            package com.appunta.android.ui;

            import java.io.IOException;
            import java.lang.reflect.Method;

            import android.content.Context;
            import android.hardware.Camera;
            import android.util.AttributeSet;
            import android.util.Log;
            import android.view.Display;
            import android.view.Surface;
            import android.view.SurfaceHolder;
            import android.view.View;
            import android.view.SurfaceHolder.Callback;
            import android.view.SurfaceView;
            import android.view.WindowManager;

            public class CameraView extends SurfaceView implements Callback {

                Camera camera;
                SurfaceHolder previewHolder;
                private boolean isPreviewRunning;

                public CameraView(Context ctx) {
                    super(ctx);

                    init();
                }

                private void init() {
                    previewHolder = this.getHolder();
                    previewHolder.addCallback(this);
                    previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
                }

                public CameraView(Context context, AttributeSet attrs) {
                    super(context, attrs);
                    init();
                }

                public CameraView(Context context, AttributeSet attrs, int defStyle) {
                    super(context, attrs, defStyle);
                    init();
                }

                public void surfaceCreated(SurfaceHolder holder) {
                    camera = Camera.open();
                    setCameraDisplayOrientation(camera);
                    try {
                        camera.setPreviewDisplay(holder);
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    camera.startPreview();
                }

                public void surfaceChanged(SurfaceHolder holder, int format, int width,
                        int height) {
                    if (isPreviewRunning) {
                        camera.stopPreview();
                    }
                    setCameraDisplayOrientation(camera);
                    previewCamera();
                }

                public void previewCamera() {
                    try {
                        camera.setPreviewDisplay(previewHolder);
                        camera.startPreview();
                        isPreviewRunning = true;
                    } catch (Exception e) {
                    }
                }

                public void surfaceDestroyed(SurfaceHolder arg0) {
                    camera.stopPreview();
                    camera.release();
                }

                public void startCamera() {
                    previewCamera();
                }

                public void stopCamera() {
                    camera.stopPreview();
                    camera.release();
                }

                protected static void setDisplayOrientation(Camera camera, int angle) {
                    Method downPolymorphic;

                    try {
                        downPolymorphic = camera.getClass().getMethod(
                                "setDisplayOrientation", new Class[] { int.class });
                        if (downPolymorphic != null)
                            downPolymorphic.invoke(camera, new Object[] { angle });
                    } catch (Exception e1) {
                    }

                }

                private void setCameraDisplayOrientation(Camera camera) {
                    // android.hardware.Camera.CameraInfo info = new
                    // android.hardware.Camera.CameraInfo();
                    // android.hardware.Camera.getCameraInfo(0, info);
                    // Display display = ((WindowManager) getContext().getSystemService(
                    // Context.WINDOW_SERVICE)).getDefaultDisplay();
                    // int rotation = display.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;
                    // }
                    // setDisplayOrientation(camera, result);

                    setDisplayOrientation(camera, 90);
                }

            }
Shivam Singh
  • 287
  • 4
  • 12

2 Answers2

0

This is my implementation of surfaceCreated method of SurfaceHolder.Callback, it works fine, Please check if you are missing anything in your implementation

public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
        try {
            Log.d(TAG, "surfaceCreated(), holder"+holder.toString());
            mCamera = null;
            mCamera = Camera.open();
            Log.d(TAG, "surfaceCreated(), mCamera="+mCamera);
            mCamera.setDisplayOrientation(90);
            mCamera.setPreviewDisplay(holder);
            Camera.Parameters params= mCamera.getParameters();
            params.set("jpeg-quality", 72);
            params.set("rotation", 90);
            params.set("orientation", "portrait");
            params.setPictureFormat(PixelFormat.JPEG);
                    mCamera.setParameters(params);
            createZoomlayout();

        } catch (Exception e) {
            Toast.makeText(CameraInterface.this,
                    " surfaceCreated " + e.getMessage(), Toast.LENGTH_LONG)
                    .show();
            e.printStackTrace();
        }
    }
AAnkit
  • 27,299
  • 12
  • 60
  • 71
0

Use - setDisplayOrientation(90); if it dosn't work, reply..

Karan Nagpal
  • 521
  • 1
  • 8
  • 19
  • I have used setDisplayOrientation(90); and this works perfectly for all devices exept some samsung devices like SAMSUNG GALAXY ACE DUOS – Shivam Singh Aug 02 '13 at 10:37
  • setDisplayOrientation() method is introduced with api level 8, so if your device is working on android 2.1 or lower version then setDisplayorientation() will not work. – Karan Nagpal Aug 02 '13 at 10:50
  • i'm using minsdk API 8 only – Shivam Singh Aug 02 '13 at 10:58
  • setDisplayOrientation function is not working for Samsung Galaxy ACE with Android 2.3.6 It's a bug. There is a similar question http://stackoverflow.com/questions/19176038/camera-setdisplayorientation-function-is-not-working-for-samsung-galaxy-ace-wi There's no way to work around it, only landscape =/ But maybe someone will find way how to fix this... – DeniSHow Jul 16 '14 at 12:07