1

I am working with the Google sample project, but I cannot seem to get the preview to work without stretching it.

public void setAspectRatio(int width, int height) {
     if (width < 0 || height < 0) 
     {
        throw new IllegalArgumentException("Size cannot be negative.");
     }
     mRatioWidth =  width;
     mRatioHeight = height;
     requestLayout();
}

I have tried physically changing the aspect ration on the AutoFitTextureView class, this makes it full screen, but causes it to stretch.

Has anyone figured out a successful implementation of this ?

hcerim
  • 959
  • 1
  • 11
  • 27
Hydar M
  • 75
  • 1
  • 4

1 Answers1

1

you need to modify the setUpCameraOutputs method. Modify the following line

previously--->

   Size largest = Collections.max(
                        Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),
                        new CompareSizesByArea());

modified--->

largest =getFullScreenPreview(map.getOutputSizes(ImageFormat.JPEG),width,height);

previously--->

mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
                    rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth,
                    maxPreviewHeight, largest);

modified---->

  mPreviewSize = getFullScreenPreview(map.getOutputSizes(SurfaceTexture.class),
                    width, height);

and method for getting fullscreen preview is as follows-

 private Size getFullScreenPreview(Size[] outputSizes, int width, int height) {

        List<Size> outputSizeList = Arrays.asList(outputSizes);
        outputSizeList = sortListInDescendingOrder(outputSizeList); //because in some phones available list is in ascending order
        Size fullScreenSize = outputSizeList.get(0);
        for (int i = 0; i < outputSizeList.size(); i++) {
            int orginalWidth = outputSizeList.get(i).getWidth();
            int orginalHeight = outputSizeList.get(i).getHeight();
            float orginalRatio = (float) orginalWidth / (float) orginalHeight;
            float requiredRatio;
            if (width > height) {
                requiredRatio = ((float) width / height); //for landscape mode
                if ((outputSizeList.get(i).getWidth() > width && outputSizeList.get(i).getHeight() > height)) {
                    // because if we select preview size hire than device display resolution it may fail to create capture request
                    continue;
                }
            } else {
                requiredRatio = 1 / ((float) width / height); //for portrait mode
                if ((outputSizeList.get(i).getWidth() > height && outputSizeList.get(i).getHeight() > width)) {
                    // because if we select preview size hire than device display resolution it may fail to create capture request
                    continue;
                }
            }
            if (orginalRatio == requiredRatio) {
                fullScreenSize = outputSizeList.get(i);
                break;
            }
        }
        return fullScreenSize;
    }
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • but still there is some issue with camera2 api.. that might be android version issue. it stretches the preview on android lolipop version 5.0 i.e api 21, otherwise it works perfect on greater apis . i am also searching for the solution of it. – pankaj khedekar Jun 02 '17 at 08:20
  • have you found a solution? – Andrey Solera Jul 25 '17 at 21:11
  • I found workaround for this problem. I scaled up the textureview to work it same as the centerCrop scaleType of imageView by using setScaleX and setScaleY on textureView. – pankaj khedekar Jul 27 '17 at 15:13
  • Thanks a ton. after struggling alot, this is solution – Mohsin Oct 04 '17 at 14:31
  • not working for me!!Has anyone have solution? My preview is streched when showing in full screen in camera2 apis. – Akash Bisariya Oct 12 '17 at 07:41
  • Have you modified the code? If you use google sample code as it is then it is going to stretch anyhow. You need to modify the code as given in above snippet. – pankaj khedekar Oct 13 '17 at 08:25
  • @pankajkhedekar can u pls share sample code , after using chooseOptimalSize , still preview is getting stretched with camera2 API , any solution ? – Erum Jan 09 '20 at 11:30