Android摄像头表面观摄像头、表面、Android

2023-09-12 07:37:19 作者:尸血腥色

我想创建一个摄像头的表面视图,它呈现在表面上,只要是在相机的看法。目前,我可以在我的摄影机视图看到的是一个黑色的屏幕视图。我曾试图寻找谷歌和这里,但到目前为止,我还没有找到我所期待的。任何人都可以提出我的一些想法。

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.

推荐答案

我已经写了这个类可以帮助你。

I have written a class this 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;
        }
    }

在你的manifest文件复制此code相机许可

in your manifest file copy this code for camera permission

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

说明:

SurfaceView是一种视图,其中包含一个SurfaceHolder。 SurfaceHolder持有,我们可以展示我们的媒体(一般帧)。

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

mCamera是一个相机对象,它会包含相机的实例。

mCamera is a Camera object which will contains the camera instance.

当你想抱着默认摄像头的实例,那么你可以简单地调用Camera.open();

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

相机mCamera = Camera.open();

Camera mCamera = Camera.open();

现在您打开相机或者你有相机默认实例。现在,你需要从摄像头中捕获帧,并在表面上显示。但你不能没有任何显示它

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

面。在这里,surfaceView提供surfaceHolder和surfaceHolder提供表面显示摄像机帧。现在,表面会被创建时,3回调函数将

surface. Here surfaceView provides surfaceHolder and surfaceHolder provides surface to display camera frames. Now when surface will be created three callback functions will be

调用。

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.

surfaceCreated:      surfaceCreated是你的面将被创建时将调用的回调函数。在此,你可以打开你的摄像头,并设置其他属性。

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.

surfaceChanged:      这被称为你的面将被创建时,用于至少一个时间。之后,它将会被调用时,您的面将发生变化(在设备旋转)。在这里,您可以

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

启动preVIEW因为你的面已经创建。

start your preview because your surface have already created.

surfaceDestroyed:      这被称为每次当你的面将摧毁。现在,如果你没有表面那么在这里你可以显示你的相机框架所以我用相机发布

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

mCamera.release()。这是非常重要的,因为如果你的活动将在暂停和任何其他活动,试图打开摄像头那么它将无法打开它,你有

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

已经打开摄像头。相机是一个共享的资源,以便一次只有一个应用程序可以使用它。所以请记住一件事,每当你打开一个摄像头,然后一直将其释放。

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.

停止preVIEW:      当您启动preVIEW那么你的摄像机开始捕捉你的框架,并在表面上显示。现在,如果你面已经破坏,那么你需要停止捕获帧

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

从相机,所以你必须调用mCamera.stop preVIEW。

from camera so you have to call mCamera.stopPreview.