如何在Android中使用CameraX显示负片预览?

7

我正在学习CameraX API,CameraXBasic是官方的示例代码。

CameraXBasic项目中的CameraFragment.kt可以显示实时摄像头预览。

现在我希望能够显示负片模式的预览,我应该如何使用CameraX API来实现?是否有示例代码?

CameraFragment.kt

private lateinit var viewFinder: TextureView

private fun bindCameraUseCases() {
    // Get screen metrics used to setup camera for full screen resolution
    val metrics = DisplayMetrics().also { viewFinder.display.getRealMetrics(it) }
    val screenAspectRatio = Rational(metrics.widthPixels, metrics.heightPixels)
    Log.d(TAG, "Screen metrics: ${metrics.widthPixels} x ${metrics.heightPixels}")

    // Set up the view finder use case to display camera preview
    val viewFinderConfig = PreviewConfig.Builder().apply {
        setLensFacing(lensFacing)
        // We request aspect ratio but no resolution to let CameraX optimize our use cases
        setTargetAspectRatio(screenAspectRatio)
        // Set initial target rotation, we will have to call this again if rotation changes
        // during the lifecycle of this use case
        setTargetRotation(viewFinder.display.rotation)
    }.build()

    // Use the auto-fit preview builder to automatically handle size and orientation changes
    preview = AutoFitPreviewBuilder.build(viewFinderConfig, viewFinder)

 ....

 CameraX.bindToLifecycle(
            viewLifecycleOwner, preview, imageCapture, imageAnalyzer)
}

什么是“负模式”? - Froyo
你可以使用 RenderScript 来实现这个功能。1. 如何在 cameraX 中使用 RenderScript https://gist.github.com/LiewJunTung/b9b7f8444037fbcbe5b30a915850d494 2. RenderScript 负效果 https://developer.android.com/reference/android/media/effect/EffectFactory.html#EFFECT_NEGATIVE - Froyo
1个回答

9

这是一个应用在SurfaceTexture上的android.media.effect.EffectPreviewConfig没有这样的属性)。请参见EffectFactory.EFFECT_NEGATIVE

/**
 * Applies negative film effect on image.<br/>
 * Parameters: scale (float): the degree of film grain.
**/
public final static String EFFECT_NEGATIVE = "android.media.effect.effects.NegativeEffect";

这里也有解释(Kotlin和Java文档组织方式不同)。

然而,您可能需要使用自己的SurfaceTexture,否则很难获得GLContext。这个示例几乎可以工作,但是texture.attachToGLContext()

private EffectContext fxContext = null;
private EffectFactory fxFactory = null;
private Effect fx = null;

protected void applyNegativeEffect(SurfaceTexture texture, int width, int height) {
    if(this.fxContext == null) {
        // texture.attachToGLContext(texture.mSurfaceTexture);
        this.fxContext = EffectContext.createWithCurrentGlContext();
    }
    if(this.fxFactory == null) {
        this.fxFactory = this.fxContext.getFactory();
    }
    if(this.fx == null) {
        this.fx = fxFactory.createEffect(EffectFactory.EFFECT_NEGATIVE);
        this.fx.setParameter("scale", 1.0f);
        // this.fx.apply(0, width, height, 0);
    }
}

但是 SurfaceTexture(需要暴露 long mSurfaceTexture)的读取方式为:

/**
 * These fields are used by native code, do not access or modify.
**/
private long mSurfaceTexture;

GLSurfaceView上设置着色器可能更容易实现:
String shader = "#extension GL_OES_EGL_image_external : require\n"
    + "precision mediump float;\n"
    + "varying vec2 vTextureCoord;\n"
    + "uniform samplerExternalOES sTexture;\n"
    + "void main() {\n"
    + "  vec4 color = texture2D(sTexture, vTextureCoord);\n"
    + "  float colorR = (1.0 - color.r) / 1.0;\n"
    + "  float colorG = (1.0 - color.g) / 1.0;\n"
    + "  float colorB = (1.0 - color.b) / 1.0;\n"
    + "  gl_FragColor = vec4(colorR, colorG, colorB, color.a);\n"
    + "}\n";

同时支持OpenGL ES预览的相机2 示例

从CameraX抽象中获取GLContext是实际问题,因为需要使用自定义的OpenGL预览构建androidx.camera.core,以应用着色器或获取输入/输出texId

更新:

实现预览的读取方式如下:

如果您需要直接访问SurfaceTexture,例如执行OpenGL渲染,请参见手动创建SurfaceTexture。在这些情况下,使用Preview.PreviewSurfaceProviderSurfaceTexture传递给Preview对象。
请参见问题#146957342Futures.immediateFuture(surface)不再起作用。

1
谢谢!你的意思是说,在Android中使用CameraX显示负模式预览有点困难,因为我们无法从CameraX获取GLContext? - HelloCW

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接