编程语言
首页 > 编程语言> > Java-旋转180度android无法正常工作

Java-旋转180度android无法正常工作

作者:互联网

这个问题已经在这里有了答案:            >            Rotating phone quickly 180 degrees, camera preview turns upside down                                    2个
如果我将“左侧基准”更改为“右侧基准”(均为用户的横向视图模式),则不会重新创建应用程序(活动).诚然,相机显示为水平翻转图像.当我尝试为两种纵向模式(正常和反向)更改设备位置时,结果相同.设备正常检测到90度旋转.即,左侧=>画像=>正确大小=>反像=>左侧正常工作!

有可能解决这个问题吗?

我正在使用Android 4.4.4.

码:

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

    if(display.getRotation() == Surface.ROTATION_0){//0

        mCamera.setDisplayOrientation(90);
    }else if(display.getRotation() == Surface.ROTATION_90){//1
        mCamera.setDisplayOrientation(0);
    }else if(display.getRotation() == Surface.ROTATION_180){//2
        mCamera.setDisplayOrientation(270);
    }else if(display.getRotation() == Surface.ROTATION_270){//3
        mCamera.setDisplayOrientation(180);
    }
    Log.d(TAG, "ROTATION:" + display.getRotation() + " ");

编辑
我的意思是,当我将设备旋转180度时,永远不会调用onResume()和SurfaceCreate().

编辑功能代码

SurfaceView sfv;
FrameLayout fl0;
 @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
//some code to initialize
fl0 = (FrameLayout) findViewById(R.id.fl0);
 sfv = new SurfaceView(this);
            fl0.addView(sfv,0);
addSurfaceHolder();
}
private void addSurfaceHolder(){
        SurfaceHolder holder = sfv.getHolder();

        holder.addCallback(new SurfaceHolder.Callback() {


        @Override
          public void surfaceCreated(SurfaceHolder holder) {
              Log.d(TAG,TAG + " surfaceCreated");
//this function never calls for 180 deg rotation.
                  cameraStart(MainActivity.this,false);
                  //some code for camera holder connection
          }

          @Override
          public void surfaceChanged(SurfaceHolder holder, int format,
              int width, int height) {
              Log.d(TAG,TAG + " surfaceChanged");
          }

          @Override
          public void surfaceDestroyed(SurfaceHolder holder) {
             ///Some code to destroy camera
              Log.d(TAG,TAG + " surfaceDestroyed");
          }
        });
    }
 @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG,TAG + " onResume");//call for 90 deg rotation. not for 180 deg rotation.
    }

public static void cameraStart(Context context, boolean startStopPreview){

        if (mCamera == null)
        {
            mCamera = Camera.open(CAMERA_ID);//0
        }

         Camera.Parameters cameraParam = mCamera.getParameters();

         Camera.Size previewSize = cameraParam.getPreviewSize();
         previewFormat = cameraParam.getPreviewFormat();
         frameWidth = previewSize.width;
         frameHeight = previewSize.height;
         frameRect = new Rect(0, 0, frameWidth, frameHeight);

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

            if(display.getRotation() == Surface.ROTATION_0){//0

                mCamera.setDisplayOrientation(90);
            }else if(display.getRotation() == Surface.ROTATION_90){//1

                mCamera.setDisplayOrientation(0);
            }else if(display.getRotation() == Surface.ROTATION_180){//2

                mCamera.setDisplayOrientation(270);
            }else if(display.getRotation() == Surface.ROTATION_270){//3

                mCamera.setDisplayOrientation(180);
            }
            Log.d(TAG, "ROTATION:" + display.getRotation() + " ");//just calles for 90 deg rotation. not for 180 deg.
}

解决方法:

您需要获取相机的支持参数,如下所示:

Camera.Parameters parameters = camera.getParameters();

并在mCamera.setDisplayOrientation(int);之前添加此行;

parameters.setRotation(int);

例如:

if(display.getRotation() == Surface.ROTATION_0){//0
    parameters.setRotation(90);
    mCamera.setDisplayOrientation(90);
}else if(display.getRotation() == Surface.ROTATION_90){//1
    parameters.setRotation(0);
    mCamera.setDisplayOrientation(0);
}else if(display.getRotation() == Surface.ROTATION_180){//2
    parameters.setRotation(270);
    mCamera.setDisplayOrientation(270);
}else if(display.getRotation() == Surface.ROTATION_270){//3
    parameters.setRotation(180);
    mCamera.setDisplayOrientation(180);
}

希望这能回答您的问题

编辑

上面的代码也需要出现在onResume()方法中,因为每次更改方向时都会调用onDestroy(),然后调用onCreate(),因此onResume()需要具有这段代码以及onCreate().

编辑2

看一下OrientationEventListener

更多内容请看this问题,以获取有关OrientationEventListener的提示

希望您会发现此答案有用.

标签:android-camera,java,android
来源: https://codeday.me/bug/20191120/2047542.html