Android 相机表面视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10482057/
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 03:56:00  来源:igfitidea点击:

Android Camera Surface View

androidandroid-camera

提问by Kitaro

I am trying to create a surface view for a camera so it renders on the surface whenever is in the view of the camera. At the moment all I can see on my camera view is a black screen view. I have tried to look on Google and here but so far I haven't found what I am looking for. Anyone can suggest me some idea.

我正在尝试为相机创建一个表面视图,以便它在相机视图中时呈现在表面上。目前,我在相机视图上只能看到黑屏视图。我曾尝试在谷歌和这里查看,但到目前为止我还没有找到我要找的东西。任何人都可以给我一些想法。

回答by Bharat Sharma

I have written a class that can help you.

我写了一个可以帮助你的类。

    public class Preview_can_work extends Activity {
        private SurfaceView surface_view;  
        private Camera mCamera;
        SurfaceHolder.Callback sh_ob = null;
        SurfaceHolder surface_holder        = null;
        SurfaceHolder.Callback sh_callback  = null;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            getWindow().setFormat(PixelFormat.TRANSLUCENT);

            surface_view = new SurfaceView(getApplicationContext());
            addContentView(surface_view, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

            if (surface_holder == null) {
                surface_holder = surface_view.getHolder();
            }

            sh_callback = my_callback();
            surface_holder.addCallback(sh_callback);
        }

            SurfaceHolder.Callback my_callback() {      
                SurfaceHolder.Callback ob1 = new SurfaceHolder.Callback() {

                    @Override
                    public void surfaceDestroyed(SurfaceHolder holder) {
                          mCamera.stopPreview();
                          mCamera.release();
                          mCamera = null;
                    }

                    @Override
                    public void surfaceCreated(SurfaceHolder holder) {
                        mCamera = Camera.open();

                          try {
                               mCamera.setPreviewDisplay(holder);  
                          } catch (IOException exception) {  
                                mCamera.release();  
                                mCamera = null;  
                          }
                    }

                    @Override
                    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                            int height) {
                        mCamera.startPreview();
                    }
                };
                return ob1;
        }
    }

in your manifest file copy this code for camera permission

在您的清单文件中复制此代码以获得相机权限

<uses-permission android:name="android.permission.CAMERA"/>

Explanation:

解释:

SurfaceView is a type of View which contains a SurfaceHolder. SurfaceHolder holds the surface on which we can display our media (generally frames).

SurfaceView 是一种包含 SurfaceHolder 的视图。SurfaceHolder 持有我们可以显示我们的媒体(通常是框架)的表面。

mCamerais a Camera object which will contains the camera instance.

mCamera是一个包含相机实例的 Camera 对象。

When you want to hold default Camera instance then you can simply call Camera.open();

当您想保留默认的 Camera 实例时,您只需调用 Camera.open();

Camera mCamera = Camera.open();

Now you have an open camera or you are having default camera instance. Now you need to capture frames from the camera and display it on a surface. But you cannot display it without any

现在你有一个打开的相机或者你有默认的相机实例。现在您需要从相机捕获帧并将其显示在表面上。但是你不能在没有任何的情况下显示它

surface. Here the surfaceViewprovides surfaceHolderand surfaceHolderprovides surfaceto display camera frames. Now when surfacewill be created three callback functions will be

surface. 这里surfaceView提供surfaceHoldersurfaceHolder提供surface显示相机帧。现在什么时候surface会创建三个回调函数

called.

叫。

1. public void surfaceCreated(SurfaceHolder holder)
2. public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
3. public void surfaceDestroyed(SurfaceHolder holder)

Note:- Surface will be destroyed when your application will go on pause.

注意:- 当您的应用程序暂停时,Surface 将被销毁。

surfaceCreated:surfaceCreated is a callback function which will be called when your surface will be created. In this, you can open your camera and set other attributes.

surfaceCreated:surfaceCreated 是一个回调函数,将在创建表面时调用。在这里,您可以打开相机并设置其他属性。

surfaceChanged: This will be called atleast one time when your surface will be created. After that it will be called whenever your surface will change(In device rotation). Here you can

surfaceChanged:这将至少在创建表面时调用一次。之后,只要您的表面发生变化(在设备旋转中),它就会被调用。在这里你可以

start your preview because your surface have already created.

开始预览,因为您的表面已经创建。

surfaceDestroyed: This will be called every time when your surface will destroy. Now if you dont have surface then where you can display you camera frames so I have released camera by using

surfaceDestroyed:每次您的表面将被破坏时都会调用此方法。现在,如果您没有表面,那么您可以在何处显示相机框架,因此我已使用

mCamera.release(). This is very important because if your activity will be on pause and any other activity tries to open camera then it will not able to open it as you have

mCamera.release(). 这非常重要,因为如果您的活动将暂停并且任何其他活动尝试打开相机,那么它将无法像您一样打开它

already open camera. Camera is a shared resource so one time only one application can use it. So remember one thing whenever you open a camera then always release it.

已经打开相机。相机是一种共享资源,因此一次只有一个应用程序可以使用它。因此,无论何时打开相机,请记住一件事,然后始终将其松开。

stopPreview: When you start preview then your camera starts capturing your frames and display it on a surface. Now if your surface have destroyed then you need to stop capturing frames

stopPreview:当您开始预览时,您的相机将开始捕捉您的画面并将其显示在一个表面上。现在,如果您的表面已损坏,那么您需要停止捕获帧

from camera so you have to call mCamera.stopPreview.

从相机所以你必须打电话mCamera.stopPreview

回答by opc0de

Make shure you added the permission :

确保您添加了权限:

<uses-permission android:name="android.permission.CAMERA"/>

<uses-permission android:name="android.permission.CAMERA"/>

Also these window properties:

还有这些窗口属性:

getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                 WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Post some code if that doesn't work in order to help you

如果这不起作用,请发布一些代码以帮助您