美文网首页代码片段分享
Android 拍照后方向旋转问题

Android 拍照后方向旋转问题

作者: 花艺荣 | 来源:发表于2019-11-15 15:57 被阅读0次
  1. 假定为 横屏应用 ,这时无需设定Camera预览时的 display orientation,当手持为横屏时(home键在右侧)无需旋转,其它手持方向都需要旋转数据。
    屏幕方向.jpg
  1. 获取屏幕方向:
  • 获取设备角度
mOrientationListener = new OrientationEventListener(getApplicationContext(), SensorManager.SENSOR_DELAY_NORMAL) {
     @Override
     public void onOrientationChanged(int orientation) {
               mOrientation = orientation;
          }
       };
       try {
              mOrientationListener.enable();
           } catch (Exception e) {
               log(e.toString());
           }
  • 根据设备角度计算出屏幕方向
public static  ScreenOrientation getCameraDeviceOritation(int orientation){

       ScreenOrientation newOrientation = ScreenOrientation.LANDSCAPE;

       if (orientation >= 45 && orientation < 135)

       {
           newOrientation = ScreenOrientation.REVERSED_LANDSCAPE;

       }

       else if (orientation >= 135 && orientation < 225)

       {
           newOrientation = ScreenOrientation.REVERSED_PORTRAIT;

       }

       else if (orientation >= 225 && orientation < 315)

       {
           newOrientation = ScreenOrientation.LANDSCAPE;

       }else if((orientation >= 315 && orientation<=360) || (orientation>=0&&orientation < 45))
       {
           newOrientation = ScreenOrientation.PORTRAIT;
       }

       return newOrientation;

   }
  1. 根据屏幕角度旋转
  • 旋转
               if (!frontCam)
                  {
                      if (ortation == ScreenOrientation.LANDSCAPE)
                          bitmapRotated = gBmp;
                      if (ortation == ScreenOrientation.PORTRAIT)
                          bitmapRotated = CameraUtils.rotaingImageView(90, gBmp);
                      if(ortation == ScreenOrientation.REVERSED_PORTRAIT)
                          bitmapRotated = CameraUtils.rotaingImageView(-90, gBmp);
                      if (ortation == ScreenOrientation.REVERSED_LANDSCAPE)
                          bitmapRotated = CameraUtils.rotaingImageView(180, gBmp);
                  }
                   else
                  {
                      if (ortation == ScreenOrientation.LANDSCAPE)
                          bitmapRotated = gBmp;
                      if (ortation == ScreenOrientation.PORTRAIT)
                          bitmapRotated = CameraUtils.rotaingImageView(270, gBmp);
                      if (ortation == ScreenOrientation.REVERSED_PORTRAIT)
                          bitmapRotated = CameraUtils.rotaingImageView(90, gBmp);
                      if (ortation == ScreenOrientation.REVERSED_LANDSCAPE)
                          bitmapRotated = CameraUtils.rotaingImageView(180, gBmp);
                  }
    
      public static Bitmap rotaingImageView(int angle, Bitmap bitmap)
      {
          //旋转图片 动作
          Matrix matrix = new Matrix(); ;
          matrix.postRotate(angle);
    
          // 创建新的图片
          Bitmap resizedBitmap = null;
          try{
              resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                      bitmap.getWidth(), bitmap.getHeight(), matrix, true);
          }catch (Exception e){
             e.printStackTrace();
          }
    
          return resizedBitmap;
      }
    
    
    

相关文章

网友评论

    本文标题:Android 拍照后方向旋转问题

    本文链接:https://www.haomeiwen.com/subject/oiqsictx.html