最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - How to Add a TextWatermark to Video Using CameraX OverlayEffect API? - Stack Overflow

programmeradmin3浏览0评论

I'm trying to add a watermark text to a video while recording using CameraX's OverlayEffect API.

Initially, I considered using FFmpeg for this, but:

  1. Significantly increases the app size.
  2. Requires significant processing time.

Instead, I chose CameraX Effects API because it provides more flexibility, allowing real-time rendering directly onto the preview and recorded video.

Issues I'm Facing

  1. Text orientation is inconsistent between the front and back cameras.
  2. The watermark appears rotated incorrectly.
  3. I'm applying sensorToBufferTransform, but it doesn't seem to correctly align the text. Do I need additional transformations?
// Simple CameraX Effect implementation
private val handler = Handler(Looper.getMainLooper())
private val overlayEffect = OverlayEffect(
    VIDEO_CAPTURE or PREVIEW,
    0,
    handler
) {
    Log.e("Error", "overlayEffect error")
}.apply {
    val textPaint = Paint().apply {
        color = Color.RED
        textSize = 50f
    }

    clearOnDrawListener()

    // Rendering the text on the overlay
    setOnDrawListener {
        it.overlayCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR) // Clear previous frame
        it.overlayCanvas.setMatrix(it.sensorToBufferTransform) // Apply transformation to align with the camera sensor

        val text = "Watermark 12345"
        val centerX = it.overlayCanvas.width / 2f
        val centerY = it.overlayCanvas.height / 2f

        it.overlayCanvas.drawText(text, centerX, centerY, textPaint) // Draw text at the center

        true
    }
}

private val useCaseGroupBuilder = UseCaseGroup.Builder()
    .addUseCase(cameraPreviewUseCase)
    .addUseCase(videoCapture)
    .addEffect(overlayEffect) // Attach overlay effect to the camera pipeline

// Initialize and update the camera preview surface
private val cameraPreviewUseCase = Preview.Builder()
    .setTargetRotation(Surface.ROTATION_0)
    .build()
    .apply {
        setSurfaceProvider { newSurfaceRequest ->
            _state.update { it.copy(surfaceRequest = newSurfaceRequest) }
        }
    }

// Compose UI: Rendering surface with CameraXViewfinder
state.surfaceRequest?.let { request ->
    CameraXViewfinder(
        surfaceRequest = request,
        modifier = modifier,
        implementationMode = ImplementationMode.EMBEDDED,
    )
}

Screenshots: Back Camera Front Camera

What I'm Looking For: How can I correctly position and orient the text on both front and back cameras?

发布评论

评论列表(0)

  1. 暂无评论