Attempts to find a fixed preview frame rate that matches the desired frame rate. - Android Camera

Android examples for Camera:Camera Preview

Description

Attempts to find a fixed preview frame rate that matches the desired frame rate.

Demo Code

/*//from   ww  w.j  a  v a  2  s .  c  om
 * Copyright 2014 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;
import android.hardware.Camera;
import android.util.Log;
import java.util.List;

public class Main {
    private static final String TAG = "CameraUtils";

    /**
     * Attempts to find a fixed preview frame rate that matches the desired frame rate.
     * <p/>
     * It doesn't seem like there's a great deal of flexibility here.
     * <p/>
     * TODO: follow the recipe from http://stackoverflow.com/questions/22639336/#22645327
     *
     * @return The expected frame rate, in thousands of frames per second.
     */
    public static int chooseFixedPreviewFps(Camera.Parameters parms,
            int desiredThousandFps) {
        List<int[]> supported = parms.getSupportedPreviewFpsRange();

        for (int[] entry : supported) {
            Log.d(TAG, "entry: " + entry[0] + " - " + entry[1]);
            if ((entry[0] == entry[1]) && (entry[0] == desiredThousandFps)) {
                parms.setPreviewFpsRange(entry[0], entry[1]);
                return entry[0];
            }
        }

        int[] tmp = new int[2];
        parms.getPreviewFpsRange(tmp);
        int guess;
        if (tmp[0] == tmp[1]) {
            guess = tmp[0];
        } else {
            guess = tmp[1] / 2; // shrug
        }

        Log.d(TAG, "Couldn't find match for " + desiredThousandFps
                + ", using " + guess);
        return guess;
    }
}

Related Tutorials