Skip to content

Instantly share code, notes, and snippets.

Avatar

Gerry ggfan

  • google
  • mountain view
View GitHub Profile
View android-camerax-videocapture-api-gists-FinalizeEvent.kt
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
View android-camerax-videocapture-api-gists-contentValues.kt
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()
View android-camerax-videocapture-api-gists-recordingListener.kt
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 -> {
View android-camerax-videocapture-api-gists-cameraSelector.kt
@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
}
View android-camerax-videocapture-api-gists-bindLifeCycle.kt
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)
View android-camerax-videocapture-api-gists-recorder.kt
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)
View android-camerax-videocapture-api-gists-getResolutions.kt
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()
View android-camerax-videocapture-api-gists-QualitySelector.kt
val selector = QualitySelector
.from(
Quality.UHD,
FallbackStrategy.higherQualityOrLowerThan(Quality.SD)
)
View android-camerax-videocapture-api-gists-build.gradle
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}"
}
View gist:30d34c8cc57f7f958495c21fd1e81db3
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