Java tutorial
//package com.java2s; /* * 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 java.util.List; import android.hardware.Camera; import android.util.Log; 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; } }