4

How do you crop (not scale) a PreviewView so that it always fits the given parent ViewGroup? See a demo I created here.

In the below example, there are two equal size PreviewViews. Both PreviewViews extend their parent bounds because the selected resolution's height is greater than the container.

The scale types defined in PreviewView.ScaleType will just resize the frame, adding black borders on both sides, maintaining the aspect ratio.

My device (Pixel 4XL) doesn't have a supported resolution that fits nicely into these frames, so I'd like to use a resolution that's larger than the given frame, and crop the excess.

I know that setTargetResolution is supposed to return a resolution that is a minimum of the bounds given, so I could pass the parent width/height, but this still doesn't guarantee that PreviewView stays within bounds.

Shows PreviewView that extends bounds

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
Adrian Elder
  • 1,993
  • 3
  • 19
  • 38
  • 1
    Hey Adrian I am too looking for something similar from past few days so please have a look at my issue maybe you can resolve it: https://stackoverflow.com/questions/61728321/how-to-change-preview-of-camera-at-runtime – Cosmic Dev May 13 '20 at 01:45
  • or if there any alternative to what i am trying to do? – Cosmic Dev May 13 '20 at 01:59
  • @Adrian I'm not sure I understand what you're trying to achieve. Let's assume the resolution the camera chooses is 1000x1000, and your view is 200x200, are you saying you only want the rectangle (0, 0, 200, 200) from the preview to be displayed in the view? – Husayn Hakeem May 14 '20 at 03:17
  • @Husayn yes that's correct. As long as I can display a PreviewView that fits the bounds perfectly with no scaling, it could be any rectangle. – Adrian Elder May 14 '20 at 11:42
  • I see. `PreviewView`'s scale types all usually involve some sort of scaling. You can either use your own `SurfaceTexture` to handle the preview surface, and not crop the preview. Or you can access `PreviewView`'s underlying TextureView, and scale it however you like. Make sure to set `PreviewView`'s implementation mode to `TEXTURE_VIEW`, and access the `TextureView` by calling `PreviewView.getChildAt(0)`. This approach won't work if `PreviewView` is using a `SurfaceView`, since scaling/translating a `SurfaceView` doesn't work well on Android 7 and below. – Husayn Hakeem May 14 '20 at 16:04

0 Answers0