開始使用 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-beta09")
}

請參閱 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) }
}