1+ /*
2+ * Copyright (C) 2023 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.example.android.camera2.extensions
18+
19+ import android.graphics.Rect
20+ import android.hardware.camera2.CameraCharacteristics
21+
22+ object ZoomUtil {
23+
24+ fun minZoom () = 1.0f
25+
26+ fun maxZoom (characteristics : CameraCharacteristics ): Float {
27+ val maxZoom = characteristics.get(CameraCharacteristics .SCALER_AVAILABLE_MAX_DIGITAL_ZOOM )
28+ ? : return minZoom()
29+
30+ if (maxZoom < minZoom()) return minZoom()
31+
32+ return maxZoom
33+ }
34+
35+ fun getZoomCropRect (zoomRatio : Float , characteristics : CameraCharacteristics ): Rect {
36+ val sensorRect = sensorRect(characteristics)
37+ return cropRectByRatio(zoomRatio, sensorRect)
38+ }
39+
40+ private fun cropRectByRatio (zoomRatio : Float , sensorRect : Rect ): Rect {
41+ val cropWidth = sensorRect.width() / zoomRatio
42+ val cropHeight = sensorRect.height() / zoomRatio
43+
44+ val left = (sensorRect.width() - cropWidth) / 2.0f
45+ val top = (sensorRect.height() - cropHeight) / 2.0f
46+
47+ return Rect (
48+ left.toInt(),
49+ top.toInt(),
50+ (left + cropWidth).toInt(),
51+ (top + cropHeight).toInt()
52+ )
53+ }
54+
55+ private fun sensorRect (characteristics : CameraCharacteristics ): Rect =
56+ characteristics.get(CameraCharacteristics .SENSOR_INFO_ACTIVE_ARRAY_SIZE )!!
57+ }
0 commit comments