Android Camera Set setZoom(Camera.Parameters parameters, double targetZoomRatio)

Here you can find the source of setZoom(Camera.Parameters parameters, double targetZoomRatio)

Description

set Zoom

License

Apache License

Declaration

public static void setZoom(Camera.Parameters parameters,
            double targetZoomRatio) 

Method Source Code

//package com.java2s;
/*/*w w  w. ja v  a 2s  . com*/
 * Copyright (C) 2014 ZXing authors
 *
 * 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.
 */

import android.hardware.Camera;

import android.util.Log;

import java.util.List;

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

    public static void setZoom(Camera.Parameters parameters,
            double targetZoomRatio) {
        if (parameters.isZoomSupported()) {
            Integer zoom = indexOfClosestZoom(parameters, targetZoomRatio);
            if (zoom == null) {
                return;
            }
            if (parameters.getZoom() == zoom) {
                Log.i(TAG, "Zoom is already set to " + zoom);
            } else {
                Log.i(TAG, "Setting zoom to " + zoom);
                parameters.setZoom(zoom);
            }
        } else {
            Log.i(TAG, "Zoom is not supported");
        }
    }

    private static Integer indexOfClosestZoom(Camera.Parameters parameters,
            double targetZoomRatio) {
        List<Integer> ratios = parameters.getZoomRatios();
        Log.i(TAG, "Zoom ratios: " + ratios);
        int maxZoom = parameters.getMaxZoom();
        if (ratios == null || ratios.isEmpty()
                || ratios.size() != maxZoom + 1) {
            Log.w(TAG, "Invalid zoom ratios!");
            return null;
        }
        double target100 = 100.0 * targetZoomRatio;
        double smallestDiff = Double.POSITIVE_INFINITY;
        int closestIndex = 0;
        for (int i = 0; i < ratios.size(); i++) {
            double diff = Math.abs(ratios.get(i) - target100);
            if (diff < smallestDiff) {
                smallestDiff = diff;
                closestIndex = i;
            }
        }
        Log.i(TAG, "Chose zoom ratio of "
                + (ratios.get(closestIndex) / 100.0));
        return closestIndex;
    }
}

Related

  1. setFocusArea(Camera.Parameters parameters)
  2. setInvertColor(Camera.Parameters parameters)
  3. setMetering(Camera.Parameters parameters)
  4. setTorch(Camera.Parameters parameters, boolean on)
  5. setVideoStabilization(Camera.Parameters parameters)
  6. getBackId()
  7. adjustCamera(Context context, Camera camera)