Skip to content

Commit f284e1d

Browse files
authored
Merge pull request android#530 from jsaund/camera2extensions_add_zoom_and_focus_support
Camera2extensions add zoom and focus support
2 parents 845b72b + 1ba284c commit f284e1d

File tree

3 files changed

+372
-48
lines changed

3 files changed

+372
-48
lines changed

Camera2Extensions/app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ apply plugin: 'kotlin-kapt'
2020
apply plugin: "androidx.navigation.safeargs"
2121

2222
android {
23-
compileSdkVersion 'android-31'
23+
compileSdkVersion 'android-33'
2424
defaultConfig {
2525
testInstrumentationRunner kotlin_version
2626
applicationId "com.android.example.camera2.extensions"
2727
minSdkVersion 31
28-
targetSdkVersion 31
28+
targetSdkVersion 33
2929
versionCode 1
3030
versionName "1.0.0"
3131
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)