開始使用 Media Enhancement API

Media Enhancement API 是一項強大工具,可使用裝置端 GPU 加速功能,以低延遲時間執行高品質圖片強化。包括自動色調映射、去模糊、去噪和畫質提升等功能。

初始化 API 前,您必須先設定專案依附元件,並在資訊清單中宣告硬體加速需求。略過這些設定是導致 GLOBAL_INIT_FAILED 執行階段錯誤的主要原因。

Gradle 依附元件

app/build.gradle.kts 檔案中新增下列依附元件。為方便非同步執行作業,請加入 Kotlin 協同程式和 Jetpack Media3,以處理硬體介面。

dependencies {
    // Google Play services Media Enhancement Library (Beta)
    implementation("com.google.android.gms:play-services-media-effect-enhancement:16.0.0-beta04")
}

請參閱 Google 的 Maven 存放區,瞭解 play-services-media-effect-enhancement 的套件詳細資料。

此外,我們也建議使用 Kotlin 協同程式,以非同步方式管理強化工作階段。

// Kotlin coroutines for asynchronous API handling
  implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")
  implementation("org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.7.3")

Android 資訊清單規定

AndroidManifest.xml 檔案的 <application> 標記內新增下列元素:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application>
        <!-- 1. Google Play services version for runtime compatibility checks -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <!-- 2. Declare OpenCL compute libraries for NPU/GPU neural acceleration -->
        <uses-native-library android:name="libOpenCL.so" android:required="false" />
        <uses-native-library android:name="libOpenCL-car.so" android:required="false" />
        <uses-native-library android:name="libOpenCL-pixel.so" android:required="false" />
        <!-- 3. Declare OpenGL ES for high-performance graphics rendering -->
        <uses-native-library android:name="libGLESv2.so" android:required="false" />
        <uses-native-library android:name="libGLESv3.so" android:required="false" />
    </application>
</manifest>

OpenCL 提供必要的原生運算程式庫,可為媒體增強工作啟用 NPU 和 GPU 神經網路加速功能。在資訊清單中宣告這些程式庫,是 API 運用硬體加速功能的前提,而這對於執行高品質的低延遲強化作業至關重要。如要進一步瞭解 OpenCL,請參閱「OpenCL 實作」。

OpenGL ES 提供原生圖形程式庫,可高效能地轉譯媒體增強輸出內容。在資訊清單中宣告這些程式庫,可確保算繪管道能在硬體加速表面上有效顯示處理過的媒體。如要瞭解 OpenGL,請參閱 OpenGL API 說明文件總覽

Android 轉譯管道必須採用硬體加速,才能避免出現瓶頸。如果應用程式的目標 API 級別為 14 以上,系統預設會啟用這項功能,但您也可以在 <activity> 標記中明確設定 android:hardwareAccelerated="true"

裝置相容性和模組設定

Google Play 服務會動態提供機器學習模型,節省初始 APK 儲存空間。執行強化作業前,應用程式必須使用 EnhancementClient 驗證硬體支援,並確保必要的模型權重已下載並快取至本機。每部裝置只需要執行一次這項程序。

使用 suspendCancellableCoroutine,您可以將以工作為準的用戶端回呼包裝在標準 Kotlin 暫停函式中,以便進行更乾淨的循序執行:

// Verifies if host hardware supports NPU/GPU acceleration
suspend fun EnhancementClient.isDeviceSupportedAsync(): Boolean = suspendCancellableCoroutine { continuation ->
    this.isDeviceSupported()
        .addOnSuccessListener { result -> continuation.resume(result) }
        .addOnFailureListener { exception -> continuation.resumeWithException(exception) }
}
// Verifies the presence of required neural network models
suspend fun EnhancementClient.isModuleInstalledAsync(): Boolean = suspendCancellableCoroutine { continuation ->
    this.isModuleInstalled()
        .addOnSuccessListener { result -> continuation.resume(result) }
        .addOnFailureListener { exception -> continuation.resumeWithException(exception) }
}