[HTML payload içeriği buraya]
32.4 C
Jakarta
Wednesday, May 13, 2026

Excessive-Pace Seize and Gradual-Movement Video with CameraX 1.5


Capturing fast-moving motion with readability is a key characteristic for contemporary digital camera apps. That is achieved by means of high-speed seize—the method of buying frames at charges like 120 or 240 fps. This high-fidelity seize can be utilized for 2 distinct functions: making a high-frame-rate video for detailed, frame-by-frame evaluation, or producing a slow-motion video the place motion unfolds dramatically on display.

Beforehand, implementing these options with the Camera2 API was a extra hands-on course of. Now, with the brand new high-speed API in CameraX 1.5, your complete course of is simplified, providing you with the flexibleness to create both true high-frame-rate movies or ready-to-play slow-motion clips. This submit will present you how you can grasp each. For these new to CameraX, you may rise up to hurry with the CameraX Overview.


The Precept Behind Gradual-Movement

The basic precept of slow-motion is to seize video at a a lot larger body fee than it’s performed again. As an illustration, should you document a one-second occasion at 120 frames per second (fps) after which play that recording again at a regular 30 fps, the video will take 4 seconds to play. This “stretching” of time is what creates the dramatic slow-motion impact, permitting you to see particulars which can be too quick for the bare eye.

To make sure the ultimate output video is easy and fluid, it ought to usually be rendered at a minimal of 30 fps. Which means to create a 4x slow-motion video, the unique seize body fee have to be no less than 120 fps (120 seize fps ÷ 4 = 30 playback fps).

As soon as the high-frame-rate footage is captured, there are two major methods to attain the specified end result:

  • Participant-handled Gradual-Movement (Excessive-Body-Fee Video): The high-speed recording (e.g., 120 fps) is saved immediately as a high-frame-rate video file. It’s then the video participant’s duty to decelerate the playback pace. This provides the person flexibility to toggle between regular and slow-motion playback.

  • Prepared-to-play Gradual-Movement (Re-encoded Video): The high-speed video stream is processed and re-encoded right into a file with a regular body fee (e.g., 30 fps). The slow-motion impact is “baked in” by adjusting the body timestamps. The ensuing video will play in sluggish movement in any normal video participant with out particular dealing with. Whereas the video performs in sluggish movement by default, video gamers can nonetheless present playback pace controls that permit the person to extend the pace and watch the video at its authentic pace.

The CameraX API simplifies this by providing you with a unified manner to decide on which strategy you need, as you may see beneath.


The New Excessive-Pace Video API

The brand new CameraX resolution is constructed on two important elements:

  • Recorder#getHighSpeedVideoCapabilities(CameraInfo): This methodology permits you to verify if the digital camera can document in high-speed and, if that’s the case, which resolutions (High quality objects) are supported.

  • HighSpeedVideoSessionConfig: It is a particular configuration object that teams your VideoCapture and Preview use circumstances, telling CameraX to create a unified high-speed digital camera session. Observe that whereas the VideoCapture stream will function on the configured excessive body fee, the Preview stream will usually be restricted to a regular fee of no less than 30 FPS by the digital camera system to make sure a easy show on the display.

Getting Began

Earlier than you begin, be sure to have added the mandatory CameraX dependencies to your app’s construct.gradle.kts file. You’ll need the camera-video artifact together with the core CameraX libraries.

// construct.gradle.kts (Module: app)


dependencies {

    val camerax_version = “1.5.1”


    implementation(“androidx.digital camera:camera-core:$camerax_version”)

    implementation(“androidx.digital camera:camera-camera2:$camerax_version”)

    implementation(“androidx.digital camera:camera-lifecycle:$camerax_version”)

    implementation(“androidx.digital camera:camera-video:$camerax_version”)

    implementation(“androidx.digital camera:camera-view:$camerax_version”)

}

A Observe on Experimental APIs

It is vital to notice that the high-speed recording APIs are at the moment experimental. This implies they’re topic to alter in future releases. To make use of them, you should opt-in by including the next annotation to your code:

@kotlin.OptIn(ExperimentalSessionConfig::class, ExperimentalHighSpeedVideo::class)


Implementation

The implementation for each outcomes begins with the identical setup steps. The selection between making a high-frame-rate video or a slow-motion video comes all the way down to a single setting.

1. Arrange Excessive-Pace Seize

First, no matter your aim, you’ll want to get the ProcessCameraProvider, verify for machine capabilities, and create your use circumstances.

The next code block exhibits the entire setup circulate inside a droop perform. You’ll be able to name this perform from a coroutine scope, like lifecycleScope.launch.

// Add the OptIn annotation on the prime of your perform or class

@kotlin.OptIn(ExperimentalSessionConfig::class, ExperimentalHighSpeedVideo::class)

personal droop enjoyable setupCamera() {

    // Asynchronously get the CameraProvider

    val cameraProvider = ProcessCameraProvider.awaitInstance(this)


    // — CHECK CAPABILITIES —

    val cameraInfo = cameraProvider.getCameraInfo(CameraSelector.DEFAULT_BACK_CAMERA)

    val videoCapabilities = Recorder.getHighSpeedVideoCapabilities(cameraInfo)

    if (videoCapabilities == null) {

        // This digital camera machine doesn’t assist high-speed video.

        return

    }


    // — CREATE USE CASES —

    val preview = Preview.Builder().construct()    


    // You’ll be able to create a Recorder with default settings.

    // CameraX will mechanically choose an acceptable high quality.

    val recorder = Recorder.Builder().construct()


    // Alternatively, to make use of a selected decision, you may configure the
    // Recorder with a QualitySelector. That is helpful in case your app has
    // particular decision necessities otherwise you wish to supply person
    // preferences. 

    // To make use of a selected high quality, you may uncomment the next strains.

    // Get the record of qualities supported for high-speed video. 

    // val supportedQualities = videoCapabilities.getSupportedQualities(DynamicRange.SDR)

    // Construct the Recorder utilizing the standard from the supported record.

    // val recorderWithQuality = Recorder.Builder()

    //     .setQualitySelector(QualitySelector.from(supportedQualities.first()))

    //     .construct()


    // Create the VideoCapture use case, utilizing both recorder or recorderWithQuality

    val videoCapture = VideoCapture.withOutput(recorder)

    // Now you might be able to configure the session to your desired output…

}


2. Selecting Your Output

Now, you determine what sort of video you wish to create. This code would run contained in the setupCamera() droop perform proven above.

Choice A: Create a Excessive-Body-Fee Video

Select this feature in order for you the ultimate file to have a excessive body fee (e.g., a 120fps video).

// Create a builder for the high-speed session

val sessionConfigBuilder = HighSpeedVideoSessionConfig.Builder(videoCapture)

    .setPreview(preview)


// Question and apply a supported body fee. Widespread supported body charges embrace 120 and 240 fps.

val supportedFrameRateRanges =

    cameraInfo.getSupportedFrameRateRanges(sessionConfigBuilder.construct())


sessionConfigBuilder.setFrameRateRange(supportedFrameRateRanges.first())

Choice B: Create a Prepared-to-play Gradual-Movement Video

Select this feature in order for you a video that performs in sluggish movement mechanically in any normal video participant.

// Create a builder for the high-speed session

val sessionConfigBuilder = HighSpeedVideoSessionConfig.Builder(videoCapture)

    .setPreview(preview)


// That is the important thing: allow computerized slow-motion!

sessionConfigBuilder.setSlowMotionEnabled(true)


// Question and apply a supported body fee. Widespread supported body charges embrace 120, 240, and 480 fps.

val supportedFrameRateRanges =

   cameraInfo.getSupportedFrameRateRanges(sessionConfigBuilder.construct())

sessionConfigBuilder.setFrameRateRange(supportedFrameRateRanges.first())

This single flag is the important thing to making a ready-to-play slow-motion video. When setSlowMotionEnabled is true, CameraX processes the high-speed stream and saves it as a regular 30 fps video file. The slow-motion pace is set by the ratio of the seize body fee to this normal playback fee.

For instance:


Placing It All Collectively: Recording the Video

Upon getting configured your HighSpeedVideoSessionConfig and sure it to the lifecycle, the ultimate step is to begin the recording. The method of making ready output choices, beginning the recording, and dealing with video occasions is identical as it’s for the standard video seize.

This submit focuses on high-speed configuration, so we cannot cowl the recording course of intimately. For a complete information on all the pieces from making ready a FileOutputOptions or MediaStoreOutputOptions object to dealing with the VideoRecordEvent callbacks, please discuss with the VideoCapture documentation.

// Bind the session config to the lifecycle

cameraProvider.bindToLifecycle(

    this as LifecycleOwner,

    CameraSelector.DEFAULT_BACK_CAMERA,

    sessionConfigBuilder.construct() // Bind the config object from Choice A or B

)


// Begin the recording utilizing the VideoCapture use case

val recording = videoCapture.output

    .prepareRecording(context, outputOptions) // See docs for creating outputOptions

    .begin(ContextCompat.getMainExecutor(context)) { recordEvent ->

        // Deal with recording occasions (e.g., Begin, Pause, Finalize)

    }


Google Pictures Help for Gradual-Movement Movies

Whenever you allow setSlowMotionEnabled(true) in CameraX, the ensuing video file is designed to be immediately recognizable and playable as slow-motion in normal video gamers and gallery apps. Google Pictures, particularly, provides enhanced performance for these slow-motion movies, when the seize body fee is 120, 240, 360, 480 or 960fps:

  • Distinct UI Recognition in Thumbnail: In your Google Pictures library, slow-motion movies might be recognized by particular UI parts, distinguishing them from regular movies.

Regular video thumbnail

Gradual-motion video thumbnail

  • Adjustable Pace Segments throughout Playback: When taking part in a slow-motion video, Google Pictures supplies controls to regulate which elements of the video play at sluggish pace and which play at regular pace, giving customers artistic management. The edited video can then be exported as a brand new video file utilizing the Share button, preserving the slow-motion segments you outlined.

Regular video playback

Gradual-motion video playback with enhancing controls


A Observe on System Help

CameraX’s high-speed API depends on the underlying Android CamcorderProfile system to find out which high-speed resolutions and body charges a tool helps. CamcorderProfiles are validated by the Android Compatibility Take a look at Suite (CTS), which implies you might be assured within the machine’s reported video recording capabilities.

Which means a tool’s capacity to document slow-motion video with its built-in digital camera app doesn’t assure that the CameraX high-speed API will perform. This discrepancy happens as a result of machine producers are accountable for populating the CamcorderProfile entries of their machine’s firmware, and generally needed high-speed profiles like CamcorderProfile.QUALITY_HIGH_SPEED_1080P and CamcorderProfile.QUALITY_HIGH_SPEED_720P usually are not included. When these profiles are lacking, Recorder.getHighSpeedVideoCapabilities() will return null.

Due to this fact, it is important to at all times use Recorder.getHighSpeedVideoCapabilities() to verify for supported options programmatically, as that is essentially the most dependable manner to make sure a constant expertise throughout completely different units. In case you attempt to bind a HighSpeedVideoSessionConfig on a tool the place Recorder.getHighSpeedVideoCapabilities() returns null, the operation will fail with an IllegalArgumentException. You’ll be able to verify assist on Google Pixel units, as they constantly embrace these high-speed profiles. Moreover, numerous units from different producers, such because the Motorola Edge 30, OPPO Discover N2 Flip, and Sony Xperia 1 V, additionally assist the high-speed video capabilities.


Conclusion

The CameraX high-speed video API is each highly effective and versatile. Whether or not you want true high-frame-rate footage for technical evaluation or wish to add cinematic slow-motion results to your app, the HighSpeedVideoSessionConfig supplies a unified and easy resolution. By understanding the position of the setSlowMotionEnabled flag, you may simply assist each use circumstances and provides your customers extra artistic management.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles