简体   繁体   中英

Determining Camera Resolution (i.e. Megapixels) Programmatically in Android

I am developing an Android application in 2.2, which uses Camera. Now Can anyone tell me that "Is it possible to programmatically determine the Camera Resolution in Megapixels in Android"

if you've got the camera object, try:

android.hardware.Camera.Parameters parameters = camera.getParameters();
android.hardware.Camera.Size size = parameters.getPictureSize();


int height = size.height;
int width = size.width;

What does image resolution mean?

Resolution refers to the number of pixels in an image. Resolution is sometimes identified by the width and height of the image as well as the total number of pixels in the image. For example, an image that is 2048 pixels wide and 1536 pixels high (2048X1536) contains (multiply) 3,145,728 pixels (or 3.1 Megapixels). You could call it a 2048X1536 or a 3.1 Megapixel image. As the megapixels in the pickup device in your camera increase so does the possible maximum size image you can produce. This means that a 5 megapixel camera is capable of capturing a larger image than a 3 megapixel camera.

Example: 1936 x 1552 / 1024000 = 3 Mega Pixels

Try this

public float getBackCameraResolutionInMp()
{
    int noOfCameras = Camera.getNumberOfCameras();
    float maxResolution = -1;
    long pixelCount = -1;
    for (int i = 0;i < noOfCameras;i++)
    {
        Camera.CameraInfo cameraInfo = new CameraInfo();
        Camera.getCameraInfo(i, cameraInfo);

        if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK)
        {
            Camera camera = Camera.open(i);;
            Camera.Parameters cameraParams = camera.getParameters();
            for (int j = 0;j < cameraParams.getSupportedPictureSizes().size();j++)
            {
                long pixelCountTemp = cameraParams.getSupportedPictureSizes().get(j).width * cameraParams.getSupportedPictureSizes().get(j).height; // Just changed i to j in this loop
                if (pixelCountTemp > pixelCount)
                {
                    pixelCount = pixelCountTemp;
                    maxResolution = ((float)pixelCountTemp) / (1024000.0f);
                }
            }

            camera.release();
        }
    }

    return maxResolution;
}

Add this permission in android manifest

<uses-permission android:name="android.permission.CAMERA" />

Yes, that way works to me. One more note here, getPictureSize() will return a list of supported size with different heights and widths. To calculate the resolution in pixel of device's camera, please get the biggest height & width from the size list.

You can you this to get the list of supported sizes. getSupportedSizes()

The highest size would give you the camera resoultion in pixels.

EDIT: Just in case you do not know.

Resolution in pixel = width X height

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM