Android,我的应用程序中的相机旋转

时间:2012-01-31 14:08:52

标签: android

我在我的应用程序中打开相机,即使我手中的设备旋转,凸轮显示也会旋转90度。

代码:

import java.io.IOException;
import java.util.List;    
import android.content.Context;
import android.hardware.Camera;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback
{
        private SurfaceHolder holder;
        private Camera camera;
        private Context context;
        public CameraSurfaceView(Context context) 
        {
                super(context);

                //Initiate the Surface Holder properly
                this.holder = this.getHolder();
                this.holder.addCallback(this);
                this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
                this.context = context;
          }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {               

        // Now that the size is known, set up the camera parameters and  begin
            // the preview.
            Camera.Parameters parameters = camera.getParameters();
            List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
            Camera.Size size = previewSizes.get(0);
            parameters.setPreviewSize(size.width, size.height);           

            Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

            if(display.getOrientation() == Surface.ROTATION_0)
            {
                //parameters.setPreviewSize(height, width);                           
                parameters.setRotation(90);
            }

            if(display.getOrientation() == Surface.ROTATION_90)
            {
                //parameters.setPreviewSize(width, height);                           
            }

            if(display.getOrientation() == Surface.ROTATION_180)
            {
                //parameters.setPreviewSize(height, width);               
            }

            if(display.getOrientation() == Surface.ROTATION_270)
            {
                //parameters.setPreviewSize(width, height);
                parameters.setRotation(180);
            }                
            camera.setParameters(parameters);                
            camera.startPreview();              
        }    
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
             try
             {
                     //Open the Camera in preview mode
                     this.camera = Camera.open();
                     this.camera.setPreviewDisplay(this.holder);
             }
             catch(IOException ioe)
             {
                     ioe.printStackTrace(System.out);
             }              
        }    
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // Surface will be destroyed when replaced with a new screen
            //Always make sure to release the Camera instance
            camera.stopPreview();
            camera.release();
            camera = null;              
        }
  }

有什么想法吗?感谢

p.s我怎么知道

中我的设备的正确选择是什么
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();

2 个答案:

答案 0 :(得分:1)

当我想正确显示相机预览时,我使用了这段代码

 Camera.Parameters parameters = camera.getParameters();
            parameters.set("jpeg-quality", 100);
            parameters.set("orientation", "landscape");
            parameters.set("rotation", 90);
            parameters.setPictureFormat(PixelFormat.JPEG);

            parameters.setPreviewSize(w, h);
            camera.setParameters(parameters);

答案 1 :(得分:0)

查看Camera.setDisplayOrientation(int degrees)方法参考中给出的示例代码。请注意,您应该在调用setPreviewDisplay (SurfaceHolder)

之前执行这些操作