1 /*
2  * Copyright 2022 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 androidx.camera.camera2.internal;
18 
19 import android.hardware.camera2.CameraMetadata;
20 
21 import androidx.annotation.OptIn;
22 import androidx.camera.core.CameraSelector;
23 import androidx.camera.core.ExperimentalLensFacing;
24 
25 /**
26  * Contains utility methods related to lens facing.
27  */
28 public class LensFacingUtil {
29 
30     // Do not allow instantiation.
LensFacingUtil()31     private LensFacingUtil() {
32 
33     }
34 
35     /**
36      * Converts a lens facing direction from a {@link CameraMetadata} integer to a camera
37      * selector lens facing.
38      *
39      * @param lensFacingInteger the lens facing integer, as defined in {@link CameraMetadata}.
40      * @return the camera selector lens facing.
41      * @throws IllegalArgumentException if the specified lens facing integer can not be recognized.
42      */
43     @OptIn(markerClass = ExperimentalLensFacing.class)
44     @CameraSelector.LensFacing
getCameraSelectorLensFacing(int lensFacingInteger)45     public static int getCameraSelectorLensFacing(int lensFacingInteger) {
46         switch (lensFacingInteger) {
47             case CameraMetadata.LENS_FACING_BACK:
48                 return CameraSelector.LENS_FACING_BACK;
49             case CameraMetadata.LENS_FACING_FRONT:
50                 return CameraSelector.LENS_FACING_FRONT;
51             case CameraMetadata.LENS_FACING_EXTERNAL:
52                 return CameraSelector.LENS_FACING_EXTERNAL;
53             default:
54                 throw new IllegalArgumentException(
55                         "The given lens facing integer: " + lensFacingInteger
56                                 + " can not be recognized.");
57         }
58     }
59 
60     /**
61      * Converts a lens facing direction from a camera selector lens facing to a
62      * {@link CameraMetadata} integer.
63      *
64      * @param lensFacing the camera selector lens facing.
65      * @return The lens facing integer.
66      * @throws IllegalArgumentException if the given lens facing can not be recognized.
67      */
68     @OptIn(markerClass = ExperimentalLensFacing.class)
getLensFacingInt(@ameraSelector.LensFacing int lensFacing)69     public static int getLensFacingInt(@CameraSelector.LensFacing int lensFacing) {
70         switch (lensFacing) {
71             case CameraSelector.LENS_FACING_BACK:
72                 return CameraMetadata.LENS_FACING_BACK;
73             case CameraSelector.LENS_FACING_FRONT:
74                 return CameraMetadata.LENS_FACING_FRONT;
75             case CameraSelector.LENS_FACING_EXTERNAL:
76                 return CameraMetadata.LENS_FACING_EXTERNAL;
77             default:
78                 throw new IllegalArgumentException(
79                         "The given lens facing: " + lensFacing + " can not be recognized.");
80         }
81     }
82 }
83