Correct aspect ratio in Android - android

I'm developing an application that is painting the camera on a SurfaceView. At the beginning I found trouble with the views. They were not correctly shown. So I used the following method to correct the aspect ratio problem:
NOTE: This method is found in several places as the one to correct aspect ratio for Android camera.
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio;
// Check wheter is portrait or landscape
if (orientation == 1)
targetRatio = (double) h / w;
else
targetRatio = (double) w / h;
if (sizes == null)
return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
This worked perfect, and my camera was shown correctly. But I found problems in newer devices, such as the LG G3. It had a resolution that this method considered that was the most appropiate one, but it was showing the image with pillar boxes on portrait mode, like the image show below:
Why is this happening? How can I solve this pillar box on portrait mode?

Unless there is a bug in that code, you are getting the 'best' result possible.
The aspect ratio of the phone's camera is not guaranteed to match that of the phone's screen.
It is just a matter of deciding what the criteria are for the best result. That code is Google's suggestion for deciding what is the best camera resolution for displaying on the phone's screen.
You might not agree with Google. In that case you will need to write your own algorithm for deciding what is the best camera resolution. You might optimize for aspect-ratio, or for the resolution along the width or height. You may try to fit the screen, or you could clip bits of the image to fill the entire screen. It's up to you.
Note: the code you posted tries to find a camera resolution that is within 10% of the screen's aspect-ratio and has a height that matches the screen's the closest.
If there is no camera resolution within 10% from the screen aspect-ratio, then it picks the camera resolution that has a height that matches the screen's the closest. In this case you would have significant black bars around the pillar box.

In my case I had other views in the same activity, so I wanted to fix the dimensions of the camera preview layout on the first place. In the layout you need to give the max desired dimensions (weights).
I created another method which determines the optimal preview size for any camera given the target view current width and height and also the activity orientation:
public static Size getOptimalPreviewSize(List<Camera.Size> cameraPreviewSizes, int targetWidth, int targetHeight, boolean isActivityPortrait) {
if (CommonUtils.isEmpty(cameraPreviewSizes)) {
return null;
}
int optimalHeight = Integer.MIN_VALUE;
int optimalWidth = Integer.MIN_VALUE;
for (Camera.Size cameraPreviewSize : cameraPreviewSizes) {
boolean isCameraPreviewHeightBigger = cameraPreviewSize.height > cameraPreviewSize.width;
int actualCameraWidth = cameraPreviewSize.width;
int actualCameraHeight = cameraPreviewSize.height;
if (isActivityPortrait) {
if (!isCameraPreviewHeightBigger) {
int temp = cameraPreviewSize.width;
actualCameraWidth = cameraPreviewSize.height;
actualCameraHeight = temp;
}
} else {
if (isCameraPreviewHeightBigger) {
int temp = cameraPreviewSize.width;
actualCameraWidth = cameraPreviewSize.height;
actualCameraHeight = temp;
}
}
if (actualCameraWidth > targetWidth || actualCameraHeight > targetHeight) {
// finds only smaller preview sizes than target size
continue;
}
if (actualCameraWidth > optimalWidth && actualCameraHeight > optimalHeight) {
// finds only better sizes
optimalWidth = actualCameraWidth;
optimalHeight = actualCameraHeight;
}
}
Size optimalSize = null;
if (optimalHeight != Integer.MIN_VALUE && optimalWidth != Integer.MIN_VALUE) {
optimalSize = new Size(optimalWidth, optimalHeight);
}
return optimalSize;
}
This uses a custom Size object, because Android's Size is available after API 21.
public class Size {
private int width;
private int height;
public Size(int width, int height) {
this.width = width;
this.height = height;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
}
You can determine the width and height of a view by listening to its global layout changes and then you can set the new dimensions. This also shows how to programmatically determine activity's orientation:
cameraPreviewLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// gets called after layout has been done but before display.
cameraPreviewLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
boolean isActivityPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
Size optimalCameraPreviewSize = CustomUtils.getOptimalPreviewSize(cameraPreview.getCameraSizes(), cameraPreviewLayout.getWidth(), cameraPreviewLayout.getHeight(), isActivityPortrait);
if (optimalCameraPreviewSize != null) {
LinearLayout.LayoutParams cameraPreviewLayoutParams = new LinearLayout.LayoutParams(optimalCameraPreviewSize.getWidth(), optimalCameraPreviewSize.getHeight());
cameraPreviewLayout.setLayoutParams(cameraPreviewLayoutParams);
}
}
});
You need to also rotate the camera orientation which is another issue.

Related

Android Camera - Preview zooms in when recording video

I have been trying to figure this out for a while, but for some reason, when I start recording a video with the camera, the preview zooms in. I have the following code from some examples:
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters myParameters = mCamera.getParameters();
List<Camera.Size> sizes = myParameters.getSupportedPreviewSizes();
Camera.Size myBestSize = getBestPreviewSize(sizes, width, height);
if (myBestSize != null) {
myParameters.setPreviewSize(myBestSize.width, myBestSize.height);
myParameters.setVideoStabilization(false);
mCamera.setParameters(myParameters);
mCamera.startPreview();
mCamera.unlock();
}
}
private Camera.Size getBestPreviewSize(List<Camera.Size> sizes, int width, int height) {
final double ASPECT_TOLERANCE = 0.05;
double targetRatio = (double) width / height;
if (sizes == null) return null;
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = height;
// Find size
for (Camera.Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
But the preview still zooms in when recording a video. I tried manually setting the preview size to 1280x720, however, it doesn't work on all devices that I tested, with some showing a black SurfaceView.
Is there any way to prevent the preview from zooming in when recording a video? None of the solutions here at StackOverflow seem to work.
EDIT: I tried created a custom SurfaceView with its own onMesaure method, but now I get a -19 error when I start my MediaRecorder. (disregard the -16 in the bounty)
I am confused about the kind of zoom you're talking about, could be a software zoom, or the autofocus maybe, so I'll suggest different potential tips leading to the final solution :
Implement mCamera.setZoomChangeListener, eg:
mCamera.setZoomChangeListener(new Camera.OnZoomChangeListener() {
#Override public void onZoomChange(int i, boolean b, Camera camera) {
Log.e("StackOverflow","Zoom just changed !");
// Inspect things from here
}
});
Disable the smooth zoom :
mCamera.stopSmoothZoom(); // My device doesn't support it, but maybe your device does
Inspect all of yours Camera.Parameters:
Log.e("StackOverflow", "All Camera parameters :");
for(String s : mCamera.getParameters().flatten().split(";"))
Log.e("StackOverflow", s + "\n");
Then switch anything that could be zoom related, eg :
Camera.Parameters parameters = mCamera.getParameters();
parameters.setZoom(100); // My device support some values in the range 100 | 400
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
parameters.setVideoStabilization(false);
// Don't forget to add the new parameters to the camera :
mCamera.setParameters(parameters);
Regards
You probably have setVideoStabilization(true). In high res e.g. 4K, if you enable this, it will create this jump zoom.

Android: Taken photo is bigger than the preview

I'm trying to capture photos directly using the camera api, but this is the preview I got:
& this is the image taken after calling takePicture() which is bigger than the preview itself:
(note: I cropped the height of the previous 2 photos to enhance question readability, & kept the width as is)
I'm using this utility method to choose best optimal preview size before starting the camera preview:
public static Camera.Size getBestAspectPreviewSize(int displayOrientation,
int width,
int height,
Camera.Parameters parameters) {
double targetRatio = (double) width / height;
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
if (displayOrientation == 90 || displayOrientation == 270) {
targetRatio = (double) height / width;
}
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
Collections.sort(sizes,
Collections.reverseOrder(new SizeComparator()));
for (Camera.Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) < minDiff) {
optimalSize = size;
minDiff = Math.abs(ratio - targetRatio);
}
if (minDiff < 0.0d) {
break;
}
}
return (optimalSize);
}
& this method to choose a suitable picture size:
public static Camera.Size getBiggestSafePictureSize(Camera.Parameters parameters) {
Camera.Size result = null;
long used = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
long availableMemory = Runtime.getRuntime().maxMemory() - used;
for (Camera.Size size : parameters.getSupportedPictureSizes()) {
int newArea = size.width * size.height;
long neededMemory = newArea * 4 * 4; // newArea * 4 Bytes/pixel * 4 needed copies of the bitmap (for safety :) )
if (neededMemory > availableMemory)
continue;
if (result == null) {
result = size;
} else {
int resultArea = result.width * result.height;
if (newArea > resultArea) {
result = size;
}
}
}
return (result);
}
& this is the camera preview element in the layout:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/cameraPreview"></FrameLayout>
& I'm following the official documentation for creating the camera preview itself
So, how to force the camera preview to show the exact photo that will be taken?
Finally I found it :)
according to this answer & I quote:
While the typical camera is a 4:3 aspect ratio, the preview may also be available in 5:3 and 16:9 ratios and this seems to be accomplished by actually extending the horizontal field of view...
So we need to find a preview size & a picture size, both with 4:3 aspect ratio to be able to utilize the full angle of the camera, so I changed my code like this:
public static Camera.Size determineBestPreviewSize(Camera.Parameters parameters) {
List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
return determineBestSize(sizes);
}
public static Camera.Size determineBestPictureSize(Camera.Parameters parameters) {
List<Camera.Size> sizes = parameters.getSupportedPictureSizes();
return determineBestSize(sizes);
}
protected static Camera.Size determineBestSize(List<Camera.Size> sizes) {
Camera.Size bestSize = null;
long used = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
long availableMemory = Runtime.getRuntime().maxMemory() - used;
for (Camera.Size currentSize : sizes) {
int newArea = currentSize.width * currentSize.height;
long neededMemory = newArea * 4 * 4; // newArea * 4 Bytes/pixel * 4 needed copies of the bitmap (for safety :) )
boolean isDesiredRatio = (currentSize.width / 4) == (currentSize.height / 3);
boolean isBetterSize = (bestSize == null || currentSize.width > bestSize.width);
boolean isSafe = neededMemory < availableMemory;
if (isDesiredRatio && isBetterSize && isSafe) {
bestSize = currentSize;
}
}
if (bestSize == null) {
return sizes.get(0);
}
return bestSize;
}
You should run the same loop over the sizes returned by parameters.getSupportedPictureSizes(), and not rely on the default picture size. Furthermore, I would rather look for the best corresponding pair of preview/picture sizes, and let the picture be cropped on the screen if this aspect ratio does not match the aspect ratio of cameraPreview layout.

Camera preview quality in Android is poor

I am making a Camera app in Android and have used the following function to get the preview size:
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) w / h;
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
And I set the size like this:
Size s = getOptimalPreviewSize(parameters.getSupportedPreviewSizes(), w, h);
parameters.setPreviewSize(s.width, s.height);
But the problem is that when I run my app, the camera preview quality is really poor. How do i get the same preview quality as I get when I run my default camera app on my phone, in other words, I want a high resolution Camera preview in my app.
I am even not sure whether the preview size is the cause of this problem.
NOTE: The function to get the preview size is taken from sample programs from Android Docs.
That algorithm is not the greatest.
The default algorithm in my CWAC-Camera library is now:
public static Camera.Size getBestAspectPreviewSize(int displayOrientation,
int width,
int height,
Camera.Parameters parameters,
double closeEnough) {
double targetRatio=(double)width / height;
Camera.Size optimalSize=null;
double minDiff=Double.MAX_VALUE;
if (displayOrientation == 90 || displayOrientation == 270) {
targetRatio=(double)height / width;
}
List<Size> sizes=parameters.getSupportedPreviewSizes();
Collections.sort(sizes,
Collections.reverseOrder(new SizeComparator()));
for (Size size : sizes) {
double ratio=(double)size.width / size.height;
if (Math.abs(ratio - targetRatio) < minDiff) {
optimalSize=size;
minDiff=Math.abs(ratio - targetRatio);
}
if (minDiff < closeEnough) {
break;
}
}
return(optimalSize);
}
This:
Takes into account portrait versus landscape
Starts with the highest resolution previews and works its way down
Can be tailored via closeEnough to opt for higher resolution as opposed to best matching the aspect ratio of the preview area

Where is Video resolution stored in android?

I am automating one of the video app in android. In order to do it, I need to set the video resolution to highest.
I know that in regular camera, I can set the values in
/data/data/com.android.gallery3d/shared_prefs/com.android.gallery3d_preferences_0.xml
But the values I set there are just set for the camera not for the video. Any idea where the video resolution values are stored?
If there is any ADb command to store video encoding resolution then it would be even better.
Following is the adb command that I used but does nto seem to work:
adb shell am start -a android.media.action.VIDEO_CAPTURE --ei android.intent.extras.CAMERA_FACING 1 --ei android.intent.extras.EXTRA_VIDEO_QUALITY 1 -n com.android.gallery3d/com.android.camera.CameraActivity
I also found recently that
/data/data/com.android.gallery3d/shared_prefs/com.android.gallery3d_preferences_0.xml
file contains the value for the highest resolution and the key name is :
"pref_video_quality_key" but somehow, it only sets back camera value and does not do front camera value
You dont have to look for that, but ask the system.
Every device has a sort of supported resolutions. You can select the best available size for your requirements:
What to do?
Step 1.
you have to check for the supported sizes. You can do it with
Camera.Parameters p = myCamera.getParameters();
List<Size> previewsizes = p.getSupportedPreviewSizes();
List<Size> videosizes = p.getSupportedVideoSizes();
and then, you can choose one. If you want to automatize this, you can go further, and follow the
Step 2
write a function to select the best available size, which will receive the supported sizes, and the desired size. Yo can get the size whose ratio is closer to the desired, and if none is good enough, you get the one which height is closed to the desired, or you can get just the biggest something like:
public static final int BEST_RATIO=0;
public static final int IMPORTANT_HEIGHT=2;
public static final int IMPORTANT_WIDTH=1;
public static final int BIGGEST=3;
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h, int mode) {
final double ASPECT_TOLERANCE = 0.2;
double targetRatio = (double) w / h;
if (sizes == null)
return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
if (mode==BEST_RATIO)
{ for (Size size : sizes) {
Log.d("Camera", "Checking size " + size.width + "w " + size.height
+ "h");
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
if (mode= IMPORTANT_HEIGHT) { //you can do other for width
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
if (mode=IMPORTANT_WIDTH) { //you can do other for width
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.Width - targetWidth) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.Width - targetWidth);
}
}
}
else {
minDiff = 0;
for (Size size : sizes) {
if ( size.height * size.width > minDiff ) {
optimalSize = size;
minDiff = size.height * size.width ;
}
}
}
return optimalSize;
}
And the last step, set the parameters
Step 3
private int desiredwidth=640, desiredheight=360;
Size optimalPreviewSize = getOptimalPreviewSize(previewsizes, desiredwidth, desiredheight,BIGGEST);
Size optimalVideoSize = getOptimalPreviewSize(videosizes, desiredwidth, desiredheight,BIGGEST);
p.setPreviewSize(optimalPreviewSSize.width, optimalPreviewSSize.height);
CamcorderProfile profile = CamcorderProfile.get(cameraid, CamcorderProfile.QUALITY_LOW);
profile.videoFrameHeight= optimalVideoSize.height;
profile.videoFrameWidth=optimalVideoSize.with;
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setVideoSize(optimalVideoSize.width, optimalVideoSize.height);
myCamera.setParameters(p);

Android custom camera cannot follow phone default camera view

I read that you need to get the parameters.getSupportedPreviewSizes() and parameters.getSupportedPictureSizes() so I did this
Camera.Parameters pic = mCamera.getParameters();
List<Size> pic2 = pic.getSupportedPictureSizes();
Camera.Parameters p = mCamera.getParameters();
List<Size> previews = p.getSupportedPreviewSizes();
Size preview = getOptimalPreviewSize(previews, pic2.get(0).width,pic2.get(0).height);
with this to get optimal preview
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.05;
double targetRatio = (double) w / h;
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
then
mFrameLayout.getLayoutParams().height = preview.height;
mFrameLayout.getLayoutParams().width = preview.width;
mCamera.setParameters(parameters);
mCamera.setDisplayOrientation(90);
mFrameLayout.addView(mPreview);
I was hoping to get similar preview to as the Camera Intent with custom view similar to instagram, but this shows a rectangle that is flattened as a preview. Is there a way to get the preview and picture size of the default camera and set it to the custom camera?
For some reason when i take a picture there is more content in the saved pic on the left and right than what you see in the preview Layout it looked like it was zoomed but checked zoom and it is at 0. Any tips on how to fix this?
Just another question, can you use the Camera intent on a Layout instead of it opening the default camera?

Categories

Resources