Reputation: 85
(I have already read the other answers to similar questions, but they didn't helped me). I'm new with the Android Camera 2 API, and I'm trying to create a custom camera. Everything works fine but the preview is stretched when the phone is in portrait mode and it is squashed when th ephone is in landscape mode. So, how could i solve my problem?
Here there is the code that could contain the error.
private void setUpCameraOutputs(int width, int height) {
try {
CameraCharacteristics characteristics = this.cameraManager.getCameraCharacteristics(this.cameraId);
StreamConfigurationMap map = characteristics.get(
CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
if (map != null) {
this.imageReader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 2);
this.imageReader.setOnImageAvailableListener(
this.imageReaderListener,
this.backgroundHandler);
Point displaySize = new Point();
this.getWindowManager().getDefaultDisplay().getSize(displaySize);
int maxPreviewWidth = displaySize.x;
int maxPreviewHeight = displaySize.y;
if (MAX_PREVIEW_WIDTH < maxPreviewWidth) {
maxPreviewWidth = MAX_PREVIEW_WIDTH;
}
if (MAX_PREVIEW_HEIGHT < maxPreviewHeight) {
maxPreviewHeight = MAX_PREVIEW_HEIGHT;
}
//Viene selezionata la risoluzione ideale per l'anteprima
this.previewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
width, height, maxPreviewWidth, maxPreviewHeight);
}
} catch (CameraAccessException e) {
e.printStackTrace();
} catch (NullPointerException e) {
//L'eccezione è lanciata quando le Camera2API sono usate,
//ma non sono supportate dal dispositivo
this.showToast("Camera2 API not supported on this device");
}
}
In this method I find out the best resolution for the preview
private static Size chooseOptimalSize(Size[] choices, int
textureViewWidth, int textureViewHeight,
int maxWidth, int maxHeight) {
//Raccoglie tutte le risoluzioni grandi almeno quanto la superficie di anteprima
List<Size> bigEnough = new ArrayList<>();
//Raccoglie tutte le risoluzioni più piccole della superficie di anteprima
List<Size> notBigEnough = new ArrayList<>();
//int w = aspectRatio.getWidth();
int w = maxWidth;
//int h = aspectRatio.getHeight();
int h = maxHeight;
for (Size option : choices) {
if (option.getWidth() <= maxWidth && option.getHeight() <= maxHeight)
if (option.getHeight() == option.getWidth() * h / w) {
if (option.getWidth() >= textureViewWidth && option.getHeight() >= textureViewHeight) {
bigEnough.add(option);
} else {
notBigEnough.add(option);
}
}
}
//Pick the smallest of those big enoughPrende la risoluzione minima tra quelle grandi abbastanza.
//Se non ce ne sono di grandi abbastanza prende quella più grande tra quelle non
//abbastanza grandi
if (bigEnough.size() > 0) {
return Collections.min(bigEnough, new CompareSizesByArea());
} else if (notBigEnough.size() > 0) {
return Collections.max(notBigEnough, new CompareSizesByArea());
} else {
Log.e("Camera2", "Couldn't find any suitable preview size");
return choices[0];
}
}
Upvotes: 2
Views: 891
Reputation: 1195
You should take a look at the official Android Camera2 sample from Google: https://github.com/googlesamples/android-Camera2Basic
Specifically, what you are trying to achieve is mostly accomplished by the AutoFitTextureView class and the chooseOptimalSize method in the Camera2BasicFragment file, here's a snippet:
private static Size chooseOptimalSize(Size[] choices, int textureViewWidth,
int textureViewHeight, int maxWidth, int maxHeight, Size aspectRatio) {
// Collect the supported resolutions that are at least as big as the preview Surface
List<Size> bigEnough = new ArrayList<>();
// Collect the supported resolutions that are smaller than the preview Surface
List<Size> notBigEnough = new ArrayList<>();
int w = aspectRatio.getWidth();
int h = aspectRatio.getHeight();
for (Size option : choices) {
if (option.getWidth() <= maxWidth && option.getHeight() <= maxHeight &&
option.getHeight() == option.getWidth() * h / w) {
if (option.getWidth() >= textureViewWidth &&
option.getHeight() >= textureViewHeight) {
bigEnough.add(option);
} else {
notBigEnough.add(option);
}
}
}
// Pick the smallest of those big enough. If there is no one big enough, pick the
// largest of those not big enough.
if (bigEnough.size() > 0) {
return Collections.min(bigEnough, new CompareSizesByArea());
} else if (notBigEnough.size() > 0) {
return Collections.max(notBigEnough, new CompareSizesByArea());
} else {
Log.e(TAG, "Couldn't find any suitable preview size");
return choices[0];
}
}
Getting started with Android Camera2 API can be pretty daunting. Besides the official documentation and the aforementioned official sample, there are also a few (hopefully) helpful blog posts like:
Upvotes: 1
Reputation: 57173
The preview size should match the aspect ratio of the surface you use to display it. The Camera2 sample uses an AutoFitTextureView wrapper class that helps to reshape the view so that it fits the preview size, but copying it to your project may easily go wrong, because it depends on the delicate nuances of the inflator.
To keep it simple, set the size of your TextureView to 1600px by 900px, and this.previewSize
to 1920 by 1080.
Upvotes: 0