Last active
August 17, 2018 13:03
-
-
Save lgyjg/07fdc8146f1d472a0941dcd0e470c93d to your computer and use it in GitHub Desktop.
get CustomStreamConfigurationMap with a custom CameraCharacteristics Key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.util; | |
import android.annotation.SuppressLint; | |
import android.hardware.camera2.CameraCharacteristics; | |
import android.hardware.camera2.params.StreamConfigurationMap; | |
import java.lang.reflect.Array; | |
import java.lang.reflect.Constructor; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.InvocationTargetException; | |
public class CameraStreamConfigurations { | |
// public static final CameraCharacteristics.Key<android.hardware.camera2.params.StreamConfiguration[]> SCALER_AVAILABLE_STREAM_CONFIGURATIONS = | |
// new CameraCharacteristics.Key<android.hardware.camera2.params.StreamConfiguration[]>("android.scaler.availableStreamConfigurations", android.hardware.camera2.params.StreamConfiguration[].class); | |
@SuppressLint("PrivateApi") | |
public static StreamConfigurationMap getCustomStreamConfigurationMap(CameraCharacteristics cameraCharacteristics) { | |
try { | |
// 1. get CUSTOM_SCALER_AVAILABLE_STREAM_CONFIGURATIONS field | |
// final CameraCharacteristics.Key<StreamConfiguration[]> CUSTOM_SCALER_AVAILABLE_STREAM_CONFIGURATIONS = | |
// new CameraCharacteristics.Key<>("XXX.XXX.availableStreamConfigurations", StreamConfiguration[].class); | |
Class<?> streamConfigurationArrayClazz = Class.forName("[Landroid.hardware.camera2.params.StreamConfiguration;"); | |
Class<?> cameraCharacteristicsKeyClazz = Class.forName("android.hardware.camera2.CameraCharacteristics$Key"); | |
Class<?>[] params = {String.class, Class.class}; | |
Object[] values = {"XXX.XXX.availableStreamConfigurations", streamConfigurationArrayClazz}; | |
Constructor<?> constructor = cameraCharacteristicsKeyClazz.getDeclaredConstructor(params); | |
CameraCharacteristics.Key<?> MI_SCALER_AVAILABLE_STREAM_CONFIGURATIONS | |
= (CameraCharacteristics.Key<?>) constructor.newInstance(values); | |
// 2. get StreamConfiguration[] | |
Object configurations = cameraCharacteristics.get(MI_SCALER_AVAILABLE_STREAM_CONFIGURATIONS); | |
// 3. get capabilities | |
int[] capabilities = cameraCharacteristics.get(CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES); | |
boolean listHighResolution = false; | |
for (int capability : capabilities) { | |
if (capability == CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_BURST_CAPTURE) { | |
listHighResolution = true; | |
break; | |
} | |
} | |
// 4. get StreamConfigurationMap | |
Class streamConfigurationMapClazz = Class.forName("android.hardware.camera2.params.StreamConfigurationMap"); | |
Class reprocessFormatsMapClazz = Class.forName("android.hardware.camera2.params.ReprocessFormatsMap"); | |
Class<?>[] streamConfigurationMapParams = { | |
Array.class, | |
Array.class, | |
Array.class, | |
Array.class, | |
Array.class, | |
Array.class, | |
Array.class, | |
reprocessFormatsMapClazz, | |
Boolean.class | |
}; | |
Object[] streamConfigurationMapValues = { | |
configurations, | |
cameraCharacteristics.get(getFiled("SCALER_AVAILABLE_MIN_FRAME_DURATIONS")), | |
cameraCharacteristics.get(getFiled("SCALER_AVAILABLE_STALL_DURATIONS")), | |
cameraCharacteristics.get(getFiled("DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS")), | |
cameraCharacteristics.get(getFiled("DEPTH_AVAILABLE_DEPTH_MIN_FRAME_DURATIONS")), | |
cameraCharacteristics.get(getFiled("DEPTH_AVAILABLE_DEPTH_STALL_DURATIONS")), | |
cameraCharacteristics.get(getFiled("CONTROL_AVAILABLE_HIGH_SPEED_VIDEO_CONFIGURATIONS")), | |
cameraCharacteristics.get(getFiled("SCALER_AVAILABLE_INPUT_OUTPUT_FORMATS_MAP")), | |
listHighResolution | |
}; | |
Constructor<?> streamConfigurationMapConstructor | |
= streamConfigurationMapClazz.getDeclaredConstructor(streamConfigurationMapParams); | |
StreamConfigurationMap mStreamConfigurationMap | |
= (StreamConfigurationMap) streamConfigurationMapConstructor.newInstance(streamConfigurationMapValues); | |
return mStreamConfigurationMap; | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} catch (NoSuchMethodException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} catch (InstantiationException e) { | |
e.printStackTrace(); | |
} catch (InvocationTargetException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
static CameraCharacteristics.Key<Object> getFiled(String filed) { | |
CameraCharacteristics.Key<Object> result = null; | |
try { | |
Field f = CameraCharacteristics.class.getField(filed); | |
result = (CameraCharacteristics.Key<Object>) f.get(null); | |
} catch (NoSuchFieldException | IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment