我正在我的应用程序中打开前摄像头。在应用程序第一次运行正常,但当我再次运行第二次、第三次或第四次时,它会给我异常java.lang.RuntimeException : set display orientation failed
请帮忙。

public class MirrorView extends SurfaceView implements
SurfaceHolder.Callback
{
 private static SurfaceHolder mHolder;
  private static Camera mCamera;
  private int zoom;
  private boolean freeze, hide = false;
  private final static String DEBUG_TAG = "MirrorView";
 public static final int MEDIA_TYPE_IMAGE = 1;
public MirrorView(Context context, Camera camera)
{
    super(context);
    mCamera = camera;
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder)
{
    try
    {
            mCamera.stopPreview();
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
            Log.v(DEBUG_TAG, "Surface Created");

    }
    catch (Exception error)
    {
        Log.d("Debug",
                "Error starting mPreviewLayout: " + error.getMessage());
    }
}


public void surfaceChanged(SurfaceHolder holder, int format, int width,
    int height)
{
    Log.v(DEBUG_TAG, "Surface changed");

     if (mHolder.getSurface() == null)
    {
         Log.v(DEBUG_TAG, "Holder is null");
        return;
    }
    // can't make changes while mPreviewLayout is active
     try
    {
        mCamera.stopPreview();
        Log.v(DEBUG_TAG, "Preview Stopped");
    }

     catch (Exception e)
    {
         e.printStackTrace();
    }

     Display display = Main.getDisplay();
        Camera.Parameters parameters = mCamera.getParameters();

     if(display.getRotation() == Surface.ROTATION_0)
        {
           Log.v(DEBUG_TAG, "orientation set to 90");
            parameters.setPreviewSize(height, width);
            mCamera.setDisplayOrientation(90);
        }

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

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

        if(display.getRotation() == Surface.ROTATION_270)
        {
            Log.v(DEBUG_TAG, "orientation set to 180");
            parameters.setPreviewSize(width, height);
            mCamera.setDisplayOrientation(180);
        }

     try
    {
        // start up the mPreviewLayout
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();
    }
      catch (Exception error)
    {
        Log.d("Debug",
                "Error starting mPreviewLayout: " + error.getMessage());
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
     if(event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN)
     {
         if(hide)
         {
             Main.ShowViews();
             hide = false;
         }
         else
         {
             Main.HideShow();
             hide = true;
         }
     }

      return true;
}


/*private PictureCallback mPicture = new PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = null; //getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null)
        {
            Log.d(DEBUG_TAG, "Error creating media file, check storage permissions: " +
                "");
            return;
        }

        try {
           // FileOutputStream fos = new FileOutputStream(pictureFile);
           // fos.write(data);
           // fos.close();
        } catch (FileNotFoundException e) {
            Log.d(DEBUG_TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(DEBUG_TAG, "Error accessing file: " + e.getMessage());
        }
    }
};*/


public void ZoomIn()
{
     Camera.Parameters p = mCamera.getParameters();
        int maxZoom = p.getMaxZoom();

        if (p.isZoomSupported())
        {

           if (zoom < maxZoom)
           {
               zoom += 10;
           }
           p.setZoom(zoom);
        }

        mCamera.setParameters(p);

        try
        {
            mCamera.setPreviewDisplay(mHolder);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

         mCamera.startPreview();
}

public void ZoomOut()
{
    Camera.Parameters p = mCamera.getParameters();

    if (p.isZoomSupported())
    {

       if (zoom > 0)
       {
           zoom -= 10;
       }

       p.setZoom(zoom);
    }

    mCamera.setParameters(p);

    try
    {
        mCamera.setPreviewDisplay(mHolder);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

     mCamera.startPreview();
}

public void freezeMirror()
{
    if(freeze)
    {
        try
        {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        freeze = false;
    }
    else
    {
    //  mCamera.takePicture(null, null, mPicture);

        mCamera.stopPreview();
        mCamera.startPreview();
        freeze = true;
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    // TODO Auto-generated method stub

     if(mCamera != null)
     {
         //mCamera.stopPreview();
         mCamera.release();
     }
}


}

我有主课,这是我的劳彻活动
public class Main extends Activity
{

private Camera mCam;
private MirrorView mCamPreview;
private int mCameraId = 0;
private FrameLayout mPreviewLayout;
private static ImageView zoomin, zoomout, freeze;
private static Display display;
private static File picfile;

private static final String DEBUG_TAG = "Main";
public static final int MEDIA_TYPE_IMAGE = 1;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mCameraId = findFirstFrontFacingCamera();

    mPreviewLayout = (FrameLayout) findViewById(R.id.camPreview);
    zoomin = (ImageView) findViewById(R.id.zoomin);
    zoomout = (ImageView) findViewById(R.id.zoomout);
  //  freeze = (ImageView) findViewById(R.id.freeze);

    mPreviewLayout.removeAllViews();


    display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
   // picfile = getOutputMediaFile(MEDIA_TYPE_IMAGE);

    setListeners();

    startCameraInLayout(mPreviewLayout, mCameraId);
}


public static void ShowViews()
{
    zoomin.setVisibility(View.VISIBLE);
    zoomout.setVisibility(View.VISIBLE);
    //freeze.setVisibility(View.VISIBLE);
}

public static void HideShow()
{
    zoomin.setVisibility(View.INVISIBLE);
    zoomout.setVisibility(View.INVISIBLE);
    //freeze.setVisibility(View.INVISIBLE);
}

public  static Display getDisplay()
{
     return display;

}

public void setListeners()
{
    zoomin.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View arg0)
        {
            // TODO Auto-generated method stub
            mCamPreview.ZoomIn();
        }

    });

    zoomout.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View arg0)
        {
            // TODO Auto-generated method stub
            mCamPreview.ZoomOut();
        }

    });

/*  freeze.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View arg0)
        {
            // TODO Auto-generated method stub
            mCamPreview.freezeMirror();
        }

    });*/

}



@Override
protected void onPause()
{
    super.onPause();

    Log.v(DEBUG_TAG, "Pause");
    freeCamera();
}


public void freeCamera()
{
    if (mCam != null)
    {
        mCam.release();
        mCam = null;
    }
}

private int findFirstFrontFacingCamera()
{
    int foundId = -1;
    int numCams = Camera.getNumberOfCameras();
    for (int camId = 0; camId < numCams; camId++)
    {
        CameraInfo info = new CameraInfo();
        Camera.getCameraInfo(camId, info);
        if (info.facing == CameraInfo.CAMERA_FACING_FRONT)
        {
            foundId = camId;
            break;
        }
    }
    return foundId;
}

private void startCameraInLayout(FrameLayout layout, int cameraId)
{
    mCam = Camera.open(cameraId);
    if (mCam != null)
    {
        mCamPreview = new MirrorView(this, mCam);
       // setCameraDisplayOrientationAndSize();
        layout.addView(mCamPreview);
    }
}


}

日志文件:
    11-28 11:05:23.729: E/AndroidRuntime(23827): FATAL EXCEPTION: main
11-28 11:05:23.729: E/AndroidRuntime(23827): java.lang.RuntimeException: set display orientation failed
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.hardware.Camera.setDisplayOrientation(Native Method)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at com.gsmappstabs.mirror.MirrorView.surfaceChanged(MirrorView.java:83)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.SurfaceView.updateWindow(SurfaceView.java:558)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.SurfaceView.dispatchDraw(SurfaceView.java:350)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.View.draw(View.java:6923)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewGroup.drawChild(ViewGroup.java:1651)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.View.draw(View.java:6923)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1869)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewRoot.draw(ViewRoot.java:1540)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1276)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1880)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.os.Looper.loop(Looper.java:130)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.app.ActivityThread.main(ActivityThread.java:3701)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at java.lang.reflect.Method.invokeNative(Native Method)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at java.lang.reflect.Method.invoke(Method.java:507)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at dalvik.system.NativeStart.main(Native Method)
11-28 11:05:23.729: E/AndroidRuntime(23827): [Blue Error Handler] Make Debugging Report file for main
11-28 11:05:23.729: E/AndroidRuntime(23827): java.lang.RuntimeException: set display orientation failed
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.hardware.Camera.setDisplayOrientation(Native Method)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at com.gsmappstabs.mirror.MirrorView.surfaceChanged(MirrorView.java:83)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.SurfaceView.updateWindow(SurfaceView.java:558)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.SurfaceView.dispatchDraw(SurfaceView.java:350)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.View.draw(View.java:6923)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewGroup.drawChild(ViewGroup.java:1651)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.View.draw(View.java:6923)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1869)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewRoot.draw(ViewRoot.java:1540)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1276)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1880)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.os.Looper.loop(Looper.java:130)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at android.app.ActivityThread.main(ActivityThread.java:3701)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at java.lang.reflect.Method.invokeNative(Native Method)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at java.lang.reflect.Method.invoke(Method.java:507)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
11-28 11:05:23.729: E/AndroidRuntime(23827):    at dalvik.system.NativeStart.main(Native Method)

最佳答案

在设置显示方向之前调用stopPreview(),然后在设置显示方向之后调用startPreview()

mCamera.stopPreview();
mCamera.setDisplayOrientation(90);
mCamera.startPreview();

关于android - 设置显示方向失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20259063/

10-09 12:59