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

54 views
Skip to first unread message

Rajat Patel

unread,
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
.setTargetRotation(rotation)

val preview = previewBuilder.build()

// Attach the previewView's surface provider to preview use case
preview.setSurfaceProvider(previewView.createSurfaceProvider())

// 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
.setTargetAspectRatio(asp.toInt())
// Set initial target rotation, we will have to call this again if rotation changes
// during the lifecycle of this use case
.setTargetRotation(rotation)

imageCapture = imageCaptureBuilder.build()

// ImageAnalysis
imageAnalyzer = ImageAnalysis.Builder()
// We request aspect ratio but no resolution
.setTargetAspectRatio(screenAspectRatio)
// .setTargetAspectRatio()
// Set initial target rotation, we will have to call this again if rotation changes
// during the lifecycle of this use case
.setTargetRotation(rotation).build()
// 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
mStopCamera()

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)

checkForFlashAvailability()
enableZoomFeature()
setupZoomAndTapToFocus(cameraSelector,camera?.cameraInfo!!,camera?.cameraControl!!)


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

Wenhung Teng

unread,
May 3, 2021, 5:03:57 AMMay 3
to Android CameraX Discussion Group, rajat...@technostacks.in
Hi, 
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(
Rational(
viewFinder.width,
viewFinder.height
),
viewFinder.display.rotation
).setScaleType(ViewPort.FILL_CENTER).build()

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

useCaseGroupBuilder.addUseCase(preview!!)
useCaseGroupBuilder.addUseCase(imageCapture!!)
useCaseGroupBuilder.addUseCase(imageAnalyzer!!)

camera = cameraProvider.bindToLifecycle(
this, cameraSelector,
useCaseGroupBuilder.build()
)
/////////////////////////////////////

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.


rajat...@technostacks.in 在 2021年5月3日 星期一下午3:02:33 [UTC+8] 的信中寫道:
Reply all
Reply to author
Forward
0 new messages