android - 使用没有surfaceview或textureview的相机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22462360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:59:58  来源:igfitidea点击:

android - use camera without surfaceview or textureview

androidandroid-layoutopencvandroid-intentandroid-camera

提问by pree

I have been trying to figure out if there is a way to use camera to take either video/pictures without defining surfaceview or textureview. I found this link: Use Android Camera without surface View

我一直在试图弄清楚是否有一种方法可以使用相机在不定义表面视图或纹理视图的情况下拍摄视频/图片。我找到了这个链接:使用没有表面视图的 Android 相机

I used this trick with textureview on my nexus tablet but no luck! Also, http://handycodeworks.com/?p=19says that this approach doesn't work on all the devices.

我在我的 nexus 平板电脑上使用了带有纹理视图的这个技巧,但没有运气!此外,http: //handycodeworks.com/?p=19表示这种方法不适用于所有设备。

Does anyone have any idea if there is a way to do it at all? or its just something which android framework doesn't support at all and the GUI has to have some surface/texture element in layout? Then the only option is to just manipulate the layout so its not visible on the screen as per the app requirements.

有没有人知道是否有办法做到这一点?或者它只是android框架根本不支持的东西,并且GUI必须在布局中有一些表面/纹理元素?那么唯一的选择就是根据应用程序的要求操作布局,使其在屏幕上不可见。

EDIT 1: As explained in the above link http://handycodeworks.com/?p=19, I tried the below code:

编辑 1:如上述链接http://handycodeworks.com/?p=19 所述,我尝试了以下代码:

public class CameraCapture {

  // I pass the getApplicationContext() from the main activity.
  public void startCameraCapture(Context contx) {
    SurfaceView sv = new SurfaceView(contx);
    mCamera = Camera.open();
    mCamera.setPreviewDisplay(sv.getHolder());
    mCamera.setPreviewCallback(new PreviewCallback() {
      @Override
      public void onPreviewFrame(byte[] data, Camera camera) {
        Log.v("TAG", "on preview frame called");
      }
    Thread.sleep(1000);
    mCamera.startPreview();
}

However, onPreviewFrame() never invoked. Am I missing something?

但是,从未调用过 onPreviewFrame()。我错过了什么吗?

EDIT 2:

编辑2:

Is it possible to do in native code? Capturing video/using camera without GUI element (surfaceview/textureview) using OpenCV? I looked at this link: http://docs.opencv.org/trunk/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#application-development-with-static-initialization. However, they also show the sample code with some camera view element defined in main layout xml file.

是否可以在本机代码中执行?使用 OpenCV 捕获视频/使用没有 GUI 元素(表面视图/纹理视图)的相机?我查看了这个链接:http: //docs.opencv.org/trunk/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#application-development-with-static-initialization。但是,它们还显示了带有在主布局 xml 文件中定义的一些相机视图元素的示例代码。

采纳答案by pree

I'm able to do it by defining SurfaceTexture object as below:

我可以通过如下定义 SurfaceTexture 对象来做到这一点:

SurfaceTexture surfaceTexture = new SurfaceTexture(10);
<camera object>.setPreviewTexture(surfaceTexture);

In this, I don't need to define a GUI element in the app.

在这里,我不需要在应用程序中定义 GUI 元素。

回答by fadden

This comes up periodically.

这会定期出现

It depends on what API level you're targeting. API 11 (Honeycomb) introduced the SurfaceTextureclass, which directs incoming frames to a GLES texture rather than a visible window. You can see it in used in CameraToMpegTest, which does a "headless" recording of video to .mp4 (requires API 18 for all the video stuff).

这取决于您的目标 API 级别。API 11 (Honeycomb) 引入了SurfaceTexture类,它将传入帧定向到 GLES 纹理而不是可见窗口。您可以在CameraToMpegTest 中看到它的使用,它将视频“无头”录制为 .mp4(所有视频内容都需要 API 18)。

If you're targeting 2.3.x, you will need a different solution.

如果您的目标是 2.3.x,您将需要不同的解决方案。