Captured image size and Preview Size are not same. (Capture image has bigger size)

Skip to first unread message

Rajat Patel

May 3, 2021, 3:02:33 AMMay 3
to Android CameraX Discussion Group
Here's what i am doing to bind Camera.

    private fun bindCameraUseCases() {

// Get screen metrics used to setup camera for full screen resolution
val metrics = DisplayMetrics().also { previewView.display.getRealMetrics(it) }

/* start preview */
/* start preview */
val aspRatioW : Int = previewView.getWidth() //get width of screen

val aspRatioH : Int = previewView.getHeight() //get height

val asp = Rational(aspRatioW, aspRatioH) //aspect ratio

val screen = Size(aspRatioW, aspRatioH) //size of the screen

val screenAspectRatio = aspectRatio(metrics.widthPixels, metrics.heightPixels)

val rotation = previewView.display.rotation

// Bind the CameraProvider to the LifeCycleOwner
val cameraSelector = CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build()

val cameraProviderFuture = ProcessCameraProvider.getInstance(this@CameraActivity)

cameraProviderFuture.addListener(Runnable {

// CameraProvider
cameraProvider = cameraProviderFuture.get()

// Preview
val previewBuilder = Preview.Builder()
// We request aspect ratio but no resolution
// .setTargetAspectRatio(screenAspectRatio)
// Set initial target rotation

val preview =

// Attach the previewView's surface provider to preview use case

// ImageCapture
val imageCaptureBuilder = ImageCapture.Builder().setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
// We request aspect ratio but no resolution to match preview config, but letting
// CameraX optimize for whatever specific resolution best fits our use cases
// Set initial target rotation, we will have to call this again if rotation changes
// during the lifecycle of this use case

imageCapture =

// ImageAnalysis
imageAnalyzer = ImageAnalysis.Builder()
// We request aspect ratio but no resolution
// .setTargetAspectRatio()
// Set initial target rotation, we will have to call this again if rotation changes
// during the lifecycle of this use case
// The analyzer can then be assigned to the instance
.also {
it.setAnalyzer(executor, LuminosityAnalyzer { luma ->
// Values returned from our analyzer are passed to the attached listener
// We log image analysis results here - you should do something useful
// instead!

// Must unbind the use-cases before rebinding them

try {

// A variable number of use-cases can be passed here -
// camera provides access to CameraControl & CameraInfo
camera = /*if (BuildConfig.DEBUG) {
cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture, imageAnalyzer)
} else*/ cameraProvider?.bindToLifecycle(this, cameraSelector, preview, imageAnalyzer,imageCapture)


catch (exception : Exception) {
}, ContextCompat.getMainExecutor(this@CameraActivity))

Wenhung Teng

May 3, 2021, 5:03:57 AMMay 3
to Android CameraX Discussion Group,
Are you looking for the same FOV on Preview & ImageCapture?
You have a try on the ViewPort feature to produce the same crop rect in a WYSIWYG.
While using the CameraXBasic sample code, you can replace the bindToLifeCycle with
val viewPort: ViewPort = ViewPort.Builder(

val useCaseGroupBuilder: UseCaseGroup.Builder = UseCaseGroup.Builder().setViewPort(


camera = cameraProvider.bindToLifecycle(
this, cameraSelector,

If you are focusing on the output resolution, you can try to use setTargetResolution() on use cases to limit the resolution of the output size. 在 2021年5月3日 星期一下午3:02:33 [UTC+8] 的信中寫道:
Reply all
Reply to author
0 new messages