Pir Fahim Shah
Pir Fahim Shah

Reputation: 10623

How to find Camera resolution and aspect ratio in android

I want to get supported camera resolution preview size and aspected ratio in list view like i have shown in the pictures. Currently i am doing this which only show me a list of resolution width and height but i also want to show aspect ration.

public void find_camera_resolution()
  {
    Camera mCamera = Camera.open();
    Camera.Parameters params = mCamera.getParameters();

    List<Camera.Size> sizes = params.getSupportedPreviewSizes();
    int widths[] = new int[params.getSupportedPreviewSizes().size()];
    int heights[] = new int[params.getSupportedPictureSizes().size()];

    Camera.Size mSize;
    for (Camera.Size size : sizes) {
        Toast.makeText(this,"Available resolution: "+widths.length+" "+size.height,Toast.LENGTH_LONG).show();
        mSize = size;
    }
}

But i want to show like this in the picture enter image description here

Upvotes: 1

Views: 3572

Answers (1)

Elek
Elek

Reputation: 106

The aspect ratio is just the the ratio of the resolution. So you (just) need to find the Greatest Common Divisor of width and height, divide the both values by the divisior and then you got your ratio.

For example 1600x720 -> GCD: 80 -> 1600/80=20, 720/80=9 -> aspect ratio: 20:9

There are a lot of existing algorithms to solve the Createst Common Divisor Problem and there existist already some implementations which you can use.

Sample source code are here:

 int gcd=find_greatest_common_divisor_of_two_number(widths[i], heights[i]);
 String ratio =(widths[i]/gcd)+":"+(heights[i]/gcd);

 private static int find_greatest_common_divisor_of_two_number(int number1, int number2) {
    //base case
    if(number2 == 0){
        return number1;
    }
    return find_greatest_common_divisor_of_two_number(number2, number1%number2);
}

Upvotes: 2

Related Questions