1 /* <lambda>null2 * Copyright 2024 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.testing.impl 18 19 import android.graphics.ImageFormat 20 import android.hardware.camera2.CameraCharacteristics 21 import android.util.Pair 22 import android.util.Size 23 import androidx.camera.core.CameraFilter 24 import androidx.camera.core.CameraInfo 25 import androidx.camera.core.CameraProvider 26 import androidx.camera.core.CameraSelector 27 import androidx.camera.core.ImageCapture 28 import androidx.camera.core.impl.CameraConfig 29 import androidx.camera.core.impl.CameraConfig.REQUIRED_RULE_COEXISTING_PREVIEW_AND_IMAGE_CAPTURE 30 import androidx.camera.core.impl.CameraInfoInternal 31 import androidx.camera.core.impl.Config 32 import androidx.camera.core.impl.ExtendedCameraConfigProviderStore 33 import androidx.camera.core.impl.Identifier 34 import androidx.camera.core.impl.MutableOptionsBundle 35 import androidx.camera.core.impl.SessionProcessor 36 import androidx.camera.core.impl.UseCaseConfigFactory 37 38 /** Utilities for Extensions related tests. */ 39 public object ExtensionsUtil { 40 private fun getOutputSizes( 41 cameraProvider: CameraProvider, 42 cameraSelector: CameraSelector, 43 format: Int 44 ): Array<Size> { 45 val cameraCharacteristics = 46 (cameraProvider.getCameraInfo(cameraSelector) as CameraInfoInternal) 47 .cameraCharacteristics as CameraCharacteristics 48 return cameraCharacteristics 49 .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)!! 50 .getOutputSizes(format) 51 } 52 53 /** Returns a {@link CameraSelector} with the given {@link SessionProcessor}. */ 54 @JvmStatic 55 public fun getCameraSelectorWithSessionProcessor( 56 cameraProvider: CameraProvider, 57 cameraSelector: CameraSelector, 58 sessionProcessor: SessionProcessor, 59 outputYuvformatInCapture: Boolean = false 60 ): CameraSelector { 61 val identifier = Identifier.create("idStr") 62 ExtendedCameraConfigProviderStore.addConfig(identifier) { _, _ -> 63 object : CameraConfig { 64 override fun getConfig(): Config { 65 return MutableOptionsBundle.create() 66 } 67 68 override fun getCompatibilityId(): Identifier { 69 return Identifier.create(0) 70 } 71 72 override fun getSessionProcessor( 73 valueIfMissing: SessionProcessor? 74 ): SessionProcessor { 75 return sessionProcessor 76 } 77 78 override fun getSessionProcessor(): SessionProcessor { 79 return sessionProcessor 80 } 81 82 override fun getUseCaseCombinationRequiredRule(): Int { 83 return REQUIRED_RULE_COEXISTING_PREVIEW_AND_IMAGE_CAPTURE 84 } 85 86 override fun getUseCaseConfigFactory(): UseCaseConfigFactory = 87 object : UseCaseConfigFactory { 88 override fun getConfig( 89 captureType: UseCaseConfigFactory.CaptureType, 90 captureMode: Int 91 ): Config? { 92 if (captureType == UseCaseConfigFactory.CaptureType.IMAGE_CAPTURE) { 93 val builder = ImageCapture.Builder() 94 builder.setHighResolutionDisabled(true) 95 val supportedResolutions = 96 mutableListOf<android.util.Pair<Int, Array<Size>>>() 97 if (outputYuvformatInCapture) { 98 supportedResolutions.add( 99 Pair( 100 ImageFormat.YUV_420_888, 101 getOutputSizes( 102 cameraProvider, 103 cameraSelector, 104 ImageFormat.YUV_420_888 105 ) 106 ) 107 ) 108 } else { 109 supportedResolutions.add( 110 Pair( 111 ImageFormat.JPEG, 112 getOutputSizes( 113 cameraProvider, 114 cameraSelector, 115 ImageFormat.JPEG 116 ) 117 ) 118 ) 119 } 120 builder.setSupportedResolutions(supportedResolutions) 121 return builder.useCaseConfig 122 } 123 return null 124 } 125 } 126 } 127 } 128 129 val builder = CameraSelector.Builder.fromSelector(cameraSelector) 130 builder.addCameraFilter( 131 object : CameraFilter { 132 override fun filter(cameraInfos: MutableList<CameraInfo>): MutableList<CameraInfo> { 133 val newCameraInfos = mutableListOf<CameraInfo>() 134 newCameraInfos.addAll(cameraInfos) 135 return newCameraInfos 136 } 137 138 override fun getIdentifier(): Identifier { 139 return identifier 140 } 141 } 142 ) 143 144 return builder.build() 145 } 146 } 147