Home | History | Annotate | Download | only in focus
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.camera.ui.focus;
     18 
     19 import android.annotation.TargetApi;
     20 import android.hardware.camera2.CameraCharacteristics;
     21 import android.os.Build.VERSION_CODES;
     22 
     23 import com.android.camera.ui.motion.LinearScale;
     24 
     25 /**
     26  * Compute diopter range scale to convert lens focus distances into
     27  * a ratio value.
     28  */
     29 @TargetApi(VERSION_CODES.LOLLIPOP)
     30 public class LensRangeCalculator {
     31 
     32     /**
     33      * A NoOp linear scale for computing diopter values will always return 0
     34      */
     35     public static LinearScale getNoOp() {
     36         return new LinearScale(0, 0, 0, 0);
     37     }
     38 
     39     /**
     40      * Compute the focus range from the camera characteristics and build
     41      * a linear scale model that maps a focus distance to a ratio between
     42      * the min and max range.
     43      */
     44     public static LinearScale getDiopterToRatioCalculator(CameraCharacteristics characteristics) {
     45         // From the android documentation:
     46         //
     47         // 0.0f represents farthest focus, and LENS_INFO_MINIMUM_FOCUS_DISTANCE
     48         // represents the nearest focus the device can achieve.
     49         //
     50         // Example:
     51         //
     52         // Infinity    Hyperfocal                 Minimum   Camera
     53         //  <----------|-----------------------------|         |
     54         // [0.0]     [0.31]                       [14.29]
     55         Float nearest = characteristics.get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE);
     56         Float hyperfocal = characteristics.get(CameraCharacteristics.LENS_INFO_HYPERFOCAL_DISTANCE);
     57 
     58         if (nearest == null && hyperfocal == null) {
     59             return getNoOp();
     60         }
     61 
     62         nearest = (nearest == null) ? 0.0f : nearest;
     63         hyperfocal = (hyperfocal == null) ? 0.0f : hyperfocal;
     64 
     65         if (nearest > hyperfocal) {
     66             return new LinearScale(hyperfocal, nearest, 0, 1);
     67         }
     68 
     69         return new LinearScale(nearest, hyperfocal, 0, 1);
     70     }
     71 }
     72