【问题标题】:CameraX - Rotate only preview while activity is locked in Portrait modeCameraX - 在纵向模式下锁定活动时仅旋转预览
【发布时间】:2020-12-03 03:09:30
【问题描述】:
  1. 设置 -> 手机固定在支架上,不能旋转。相机正对着笔记本

  2. MyCameraActivity -> 固定方向 -> 纵向

  3. 用户应该能够在点击按钮时旋转预览,即我的预览应该被旋转(例如,如果我在开始时看到向上箭头,那么点击用户应该能够向右箭头)

我可以在 camera2 中执行此操作,但找不到任何有关 cameraX 的有用信息。

【问题讨论】:

    标签: android android-camera2 android-camerax


    【解决方案1】:

    CameraX' PreviewView 现在支持该功能。使用 PreviewView,您只需将预览置于COMPATIBLE 模式即可旋转预览,然后设置预览的目标旋转。

    代码示例:

    previewView.setImplementationMode(COMPATIBLE)
    preview.setTargetRotation(ROTATION_0)
    

    【讨论】:

      【解决方案2】:

      解决 camerax 版本 1.0.0-alpha2

      1. 在更新变换中设置矩阵。
      2. 旋转 textview 以旋转预览

      Camera2 api 也可以应用类似的逻辑(为我工作)

      private Preview setPreview() {
      
              PreviewConfig pConfig = new PreviewConfig.Builder()
                      .setLensFacing(CameraX.LensFacing.BACK)
                      .build();
              Preview preview = new Preview(pConfig);
      
              preview.setOnPreviewOutputUpdateListener(
                      output -> {
                          ViewGroup parent = (ViewGroup) binding.viewFinder.getParent();
                          parent.removeView(binding.viewFinder);
                          parent.addView(binding.viewFinder, 0);
                          binding.viewFinder.setSurfaceTexture(output.getSurfaceTexture());
                          updateTransform();
                      });
      
              return preview;
          }
      
      
          private void updateTransform() {
              float viewWidth = binding.viewFinder.getMeasuredWidth();
              float viewHeight = binding.viewFinder.getMeasuredHeight();
               Size mPreviewSize = new Size(1080,720);
      
              Activity activity = getActivity();
              if (null == binding.viewFinder || null == mPreviewSize || null == activity) {
                  return;
              }
              int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
              Matrix matrix = new Matrix();
              RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
              RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
              float centerX = viewRect.centerX();
              float centerY = viewRect.centerY();
              if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
                  bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
                  matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
                  float scale = Math.max(
                          (float) viewHeight / mPreviewSize.getHeight(),
                          (float) viewWidth / mPreviewSize.getWidth());
                  matrix.postScale(scale, scale, centerX, centerY);
                  matrix.postRotate(90 * (rotation - 2), centerX, centerY);
              } else if (Surface.ROTATION_180 == rotation) {
                  matrix.postRotate(180, centerX, centerY);
              }
              binding.viewFinder.setTransform(matrix);
          }
          int rotatiion = 0;
          private void setRotationClickListener() {
      
              binding.captureButton.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      if (rotatiion == 0) {
                          rotatiion = 90;
                          binding.viewFinder.setRotation(90);
                      }else if(rotatiion == 90) {
                          rotatiion = 180;
                          binding.viewFinder.setRotation(180);
                      }else if(rotatiion == 180) {
                          rotatiion = 270;
                          binding.viewFinder.setRotation(270);
                      }else if(rotatiion == 270) {
                          rotatiion = 0;
                          binding.viewFinder.setRotation(0);
                      }
                  }
              });
          }
      

      【讨论】:

      • 这对 camera2 api 也有效。让我知道是否有人可以为 camerax beta 版本实现相同的功能。
      猜你喜欢
      • 2019-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-17
      相关资源
      最近更新 更多