Hel*_*oCW 7 android android-camerax
我正在学习CameraX API,CameraXBasic是office 示例代码。
CameraXBasic项目中的CameraFragment.kt显示真实的相机预览。
现在我希望显示负模式预览。如何使用 CameraX API?有示例代码吗?
相机片段.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)
}
Run Code Online (Sandbox Code Playgroud)
This is an android.media.effect.Effect being applied to the SurfaceTexture (the PreviewConfig does not have any such properties). See 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";
Run Code Online (Sandbox Code Playgroud)
Here it is also explained (the Kotlin and Java documentation are differently organized).
However, you might need to use your own SurfaceTexture, because it else it is difficult to get the GLContext. This example is almost working, but the 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);
}
}
Run Code Online (Sandbox Code Playgroud)
But SurfaceTexture (where long mSurfaceTexture would need to be exposed) reads:
/**
* These fields are used by native code, do not access or modify.
**/
private long mSurfaceTexture;
Run Code Online (Sandbox Code Playgroud)
Setting a shader on a GLSurfaceView might be easier to accomplish:
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";
Run Code Online (Sandbox Code Playgroud)
And a camera2 example for OpenGL ES preview.
从 CameraX 抽象中获取 GLContext 是实际问题,因为需要使用自定义 OpenGL 预览构建androidx.camera.core,以便应用着色器 - 或获取 input/ouput texId。
更新:
实现预览如下:
如果您需要直接访问
SurfaceTexture,例如执行 OpenGL 渲染,请参阅手动创建 SurfaceTexture。在这些情况下,使用 将 aSurfaceTexture传递给Preview对象Preview.PreviewSurfaceProvider。
请参阅 issue issue #146957342,Futures.immediateFuture(surface)不再起作用。
| 归档时间: |
|
| 查看次数: |
1176 次 |
| 最近记录: |