Camera Preview is stretched in codelab

833 views
Skip to first unread message

Nelson Glauber

unread,
May 23, 2019, 7:23:44 AM5/23/19
to Android CameraX Discussion Group
hey folks,

First of all, thanks for the CameraX initiative! I've been begging for this since 2015! 

Could you take a look into this question?

I'm not sure where is the best place to ask this (and give a suggestion), so feel free to delete this topic if this is not the right place.
Basically I'd love to have a view which adjust the camera preview inside a given area. Similar what we have for the scaleType property in ImageView. As you can see in my question on stackoverflow, I'm following the CameraX codelab and the square texture is displaying the camera preview stretched.

Best regards,
Nelson

Oscar Wahltinez‎

unread,
May 27, 2019, 1:32:13 AM5/27/19
to Android CameraX Discussion Group
Nelson, have you taken a look at AutoFitPreviewBuilder from the official sample? The codelab takes a few shortcuts to simplify the code, the goal is to teach developers about the core functionality of CameraX.

Keep in mind too that using the preview use case directly is still considered advanced. The team is working on a module named CameraView that should provide a simplified way for developers to display a viewfinder (of any desired aspect ratio) without having to do all the work shown in AutoFitPreviewBuilder.

Thank you,
Oscar

Nelson Glauber

unread,
May 27, 2019, 8:22:21 AM5/27/19
to Android CameraX Discussion Group
hi Oscar,

Good to know that we could have a easy preview camera preview in the future :)

Yeah, I've tried the AutoFitPreviewBuilder without success. But once you're the second person which recommend this class, could you help me to use it on the codelab code?

Thanks in advance.

[]'s

Nelson Glauber

unread,
May 27, 2019, 8:43:14 AM5/27/19
to Android CameraX Discussion Group
Just for reference, I added this code  inside startCamera function.

I replace this part
val previewConfig = PreviewConfig.Builder().apply {
setTargetAspectRatio(Rational(1, 1))
setTargetResolution(Size(640, 640))
}.build()

// Build the viewfinder use case
val preview = Preview(previewConfig)

by this one
--------------------------

val screenSize = Size(640, 640)
val screenAspectRatio = Rational(4, 3)

// Set up the view finder use case to display camera preview
val viewFinderConfig = PreviewConfig.Builder().apply {
setLensFacing(CameraX.LensFacing.BACK)
// We request a specific resolution matching screen size
setTargetResolution(screenSize)
// We also provide an aspect ratio in case the exact resolution is not available
setTargetAspectRatio(screenAspectRatio)
setTargetRotation(viewFinder.display.rotation)
}.build()

// Use the auto-fit preview builder to automatically handle size and orientation changes
val preview = AutoFitPreviewBuilder.build(viewFinderConfig, viewFinder)

[]'s
Reply all
Reply to author
Forward
0 new messages