2
  1. Setup -> Phone is fixed on a stand , can't rotate . Camera is facing the notebook

  2. MyCameraActivity -> Fixed orientation -> Portrait

  3. User should be able to rotate the preview on click of a button i.e. My preview should be rotated ( For eg . if I am seeing up arrow at the beginning , then on click user should be able to Right side arrow )

I was to able to do this in camera2 but couldn't find any useful info regarding cameraX .

ArpitA
  • 721
  • 7
  • 17

2 Answers2

9

The feature is now supported by CameraX' PreviewView. With PreviewView, you can rotate the preview by simply putting it in COMPATIBLE mode, then set the target rotation of the Preview.

Code sample:

previewView.setImplementationMode(COMPATIBLE)
preview.setTargetRotation(ROTATION_0)
Xi 张熹
  • 10,492
  • 18
  • 58
  • 86
0

Work around for camerax version 1.0.0-alpha2

  1. Set the matrix in update transform .
  2. Rotate textview to rotate the preview

Similar Logic can be applied for Camera2 api also (Working for me)

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);
                }
            }
        });
    }
ArpitA
  • 721
  • 7
  • 17
  • this is also valid for camera2 api . Let me know if someone can implement the same for camerax beta versions . – ArpitA Jul 09 '20 at 19:21