Reputation: 3756
This is my code start camera and use Camerax
:
ImageCapture imageCapture;
ImageAnalysis imageAnalysis;
private void startCamera() {
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(new Runnable() {
@Override
public void run() {
try {
// Camera provider is now guaranteed to be available
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
// Set up the view finder use case to display camera preview
Preview preview = new Preview.Builder().build();
int DefaultPictureSizeWidth=1280;
int DefaultPictureSizeHeight=720;
imageAnalysis = new ImageAnalysis.Builder()
.setTargetResolution(new Size(DefaultPictureSizeWidth,DefaultPictureSizeHeight))
.build();
imageAnalysis.setAnalyzer(Executors.newFixedThreadPool(1), new ImageAnalysis.Analyzer() {
@Override
public void analyze(@NonNull ImageProxy image) {
image.close();
}
}
);
// Set up the capture use case to allow users to take photos
ImageCapture.Builder imageCaptureBulder = new ImageCapture.Builder();
imageCaptureBulder.setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY);
imageCapture=imageCaptureBulder.build();
// Choose the camera by requiring a lens facing
CameraSelector.Builder bulder = new CameraSelector.Builder();
bulder.requireLensFacing(CameraSelector.LENS_FACING_BACK);
CameraSelector cameraSelector= bulder.build();
// Attach use cases to the camera with the same lifecycle owner
try{
cameraProvider.unbindAll();
mCamera = cameraProvider.bindToLifecycle((LifecycleOwner)MainActivity.this,cameraSelector,imageCapture,preview,imageAnalysis);
preview.setSurfaceProvider(
mPreviewView.getSurfaceProvider());
} catch (Exception e) {
// Log.d(TAG, "Use case binding failed")
}
} catch (InterruptedException | ExecutionException e) {
// Currently no exceptions thrown. cameraProviderFuture.get()
// shouldn't block since the listener is being called, so no need to
// handle InterruptedException.
}
}
}, ContextCompat.getMainExecutor(this));
}
I had setting setTargetResolution
to Size(1280,720)
But Resolution of Result Image still is Maximum(4032x3024)
How can change resolution of Image?
Upvotes: 3
Views: 731
Reputation: 611
According to the docs:
CameraX will apply the best suitable resolution based on the requests. If the primary need is to satisfy aspect ratio, specify only
setTargetAspectRatio
, and CameraX will determine a specific resolution suitable based on the device. If the primary need of the app is to specify a resolution in order to make image processing more efficient (for example a small or mid-sized image based on device processing capability), usesetTargetResolution(Size resolution)
.
You could try
imageAnalysis = new ImageAnalysis.Builder()
.setTargetResolution(new Size(DefaultPictureSizeWidth,DefaultPictureSizeHeight))
.build();
.setTargetAspectRatio(Rational(3,4)) //Tweak this as desired
However, it generally appears that the target resolution is dependent on the device.
Upvotes: 2