简体   繁体   English

如何在 Android 中以编程方式(无布局)获取 camera2 预览帧?

[英]How to get camera2 preview frames programmatically(without layout) in Android?

I've create a foreground service with the intention of getting frames from camera2 preview without displaying a layout to user.我创建了一个前台服务,目的是从 camera2 预览中获取帧而不向用户显示布局。 When I create a Textureview in the layout and use it, everything works fine and I can get the frames via calling getBitmap(int width, int height) of TextureView.当我在布局中创建一个 Textureview 并使用它时,一切正常,我可以通过调用 TextureView 的 getBitmap(int width, int height) 来获取帧。 I've saved the frames and I know it works fine.我已经保存了框架,我知道它工作正常。 Here is my code:这是我的代码:

textureView = mainActivity.findViewById(R.id.texture_view);
surfaceTexture = textureView.getSurfaceTexture();
surface = new Surface(surfaceTexture);

But I need my code to work without a layout.但是我需要我的代码在没有布局的情况下工作。 I've created a TextureView programmatically but when I run it and save the frames, they're blank.我已经以编程方式创建了一个 TextureView,但是当我运行它并保存帧时,它们是空白的。 This is the code I run to make it work without layout:这是我运行的代码,无需布局即可运行:

textureView = new TextureView(context);
textureView.setSurfaceTexture(new SurfaceTexture(100));
surfaceTexture = textureView.getSurfaceTexture();
surface = new Surface(surfaceTexture);

FYI, I run my code on a worker thread.仅供参考,我在工作线程上运行我的代码。 I've also tried using Listeners of these objects but no luck.我也试过使用这些对象的监听器,但没有成功。 Can anyone point me in the right direction please?谁能指出我正确的方向?

I don't think you can avoid using a surface.我不认为你可以避免使用表面。 But maybe you can get away with dynamically inflating it with layout height and width 0dp.但也许你可以通过布局高度和宽度 0dp 动态膨胀它来逃避。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM