Skip to content

Instantly share code, notes, and snippets.

if(event == VideoRecordEvent.Finalize) {
if (!event.hasError()) {
val playback = Intent(Intent.ACTION_VIEW).apply {
setDataAndType(event.outputResults.outputUri, "video/*")
}
startActivity(playback);
} else {
// update app state when the capture failed.
recording?.close()
recording = null
val contentValues = ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, “CameraX-VideoCapture”)
put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4")
}
val mediaStoreOutputOptions = MediaStoreOutputOptions
.Builder(contentResolver, MediaStore.Video.Media.EXTERNAL_CONTENT_URI)
.setContentValues(contentValues)
.build()
val recordingListener = Consumer<VideoRecordEvent> { event ->
when(event) {
is VideoRecordEvent.Start -> {
val msg = “Capture Started”
Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT)
.show()
// update app internal recording state
}
is VideoRecordEvent.Finalize -> {
@androidx.annotation.OptIn(ExperimentalCamera2Interop::class)
val cameraSelector = CameraSelector.Builder()
.addCameraFilter {
it.filter { camInfo ->
val level = Camera2CameraInfo.from(camInfo)
.getCameraCharacteristic(
CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL
)
level == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_3
}
try {
camera = cameraProvider.bindToLifecycle(
viewLifecycleOwner,
CameraSelector.DEFAULT_BACK_CAMERA,
videoCapture,
preview
)
} catch (exc: Exception) {
// we are on the main thread, let's reset the controls on the UI.
Log.e(TAG, "Use case binding failed", exc)
val recorder = Recorder.Builder()
.setQualitySelector(qualitySelector)
.build()
// Create the VideoCapture UseCase and make it available to use
// in the other part of the application.
videoCapture = VideoCapture.withOutput(recorder)
fun getResolutions(selector:CameraSelector,
provider:ProcessCameraProvider
): Map<Quality, Size> {
return selector.filter(provider.availableCameraInfos).firstOrNull()
?.let { camInfo ->
QualitySelector.getSupportedQualities(camInfo)
.associateWith { quality ->
QualitySelector.getResolution(camInfo, quality)!!
}
} ?: emptyMap()
val selector = QualitySelector
.from(
Quality.UHD,
FallbackStrategy.higherQualityOrLowerThan(Quality.SD)
)
@ggfan
ggfan / android-camerax-videocapture-api-gists-build.gradle
Last active February 22, 2022 22:14
camerax-videocapture-api-gists
dependencies {
def camerax_version = "1.1.0-beta01"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-video:${camerax_version}"
implementation "androidx.camera:camera-view:${camerax_version}"
implementation "androidx.camera:camera-extensions:${camerax_version}"
}
D/AndroidRuntime( 7236):
D/AndroidRuntime( 7236): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
D/AndroidRuntime( 7236): CheckJNI is OFF
E/art ( 7236): Unrecognized option -XX:mainThreadStackSize=24K
W/art ( 7236): Ignoring unknown -Xgc option: precise
W/art ( 7236): Failed to find ELF magic in /system/framework/am.odex: dey
E/memtrack( 7236): Couldn't load memtrack module (No such file or directory)
E/android.os.Debug( 7236): failed to load memtrack module: -2
W/art ( 7236): JNI RegisterNativeMethods: attempt to register 0 native methods for android.content.res.Configuration
D/AndroidRuntime( 7236): Calling main entry com.android.commands.am.Am