Search code examples
androidcameraandroid-camera

Why is the default android camera preview smoother than my own camera preview?


I just setup a very basic camera preview that displays the camera full screen. I compared the smoothness of both my app and the android camera and figured that the android camera seems a lot smoother.

Why is that the case? Are there any special tricks to improve the camera preview performance?


Solution

  • I faced the same issue a time ago, and it turned out to be related with the camera resolution. Chances are that your Camera is initializing with the maximum available resolution, which may slow down performance during preview. Try and set a lower picture size with something like this.-

    Camera.Parameters params = camera.getParameters();
    params.setPictureSize(1280, 960);
    camera.setParameters(params);
    

    Notice that you'll need to set an available picture size. You can check available sizes with

    camera.getParameters().getSupportedPictureSizes();
    

    Hope it helps.

    EDIT

    It seems using a picture size with different aspect ratio than the default one, slows down performance as well. This is how I choose my pictureSize.-

    First off, I get the aspect ratio of the camera default pictureSize

    Camera.Parameters params = camera.getParameters();
    defaultCameraRatio = (float) params.getPictureSize().width / (float) params.getPictureSize().height;
    

    And then I get a lower pictureSize that fits the same ratio.-

    private Size getPreferredPictureSize() {
        Size res = null;
        List<Size> sizes = camera.getParameters().getSupportedPictureSizes();
    
        for (Size s : sizes) {
            float ratio = (float) s.width / (float) s.height;
            if (ratio == defaultCameraRatio && s.height <= PHOTO_HEIGHT_THRESHOLD) {
                res = s;
        break;
            }
        }
        return res;
    }
    

    Where PHOTO_HEIGHT_THRESHOLD is the max height you want to allow.