Open Camera in a blank surfaceView

2019-09-20 15:32发布

问题:

I am very new to android programming and All I want to do is open camera app in surface view so that I can open camera into it and set some parameters like following?

Camera camera = Camera.open();
 Parameters p = camera.getParameters();
 p.setFlashMode(Parameters.FLASH_MODE_ON);
 camera.setParameters(p);
 camera.startPreview();        
 camera.release();

I came across comments saying that I HAVE to pass a surface. so I have created following surface:

package com.example.fcloader;

import java.io.IOException;

import android.content.Context;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.hardware.Camera;


public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback
{
        private SurfaceHolder holder;
        private Camera camera;

        public CameraSurfaceView(Context context) 
        {
                super(context);

                //Initiate the Surface Holder properly
                this.holder = this.getHolder();
                this.holder.addCallback(this);
                this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) 
        {
                try
                {
                        //Open the Camera in preview mode
                        this.camera = Camera.open();
                        this.camera.setPreviewDisplay(this.holder);
                }
                catch(IOException ioe)
                {
                        ioe.printStackTrace(System.out);
                }
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
        {
                // Now that the size is known, set up the camera parameters and begin
                // the preview.
                Camera.Parameters parameters = camera.getParameters();
                parameters.setPreviewSize(width, height);
                camera.setParameters(parameters);
                camera.startPreview();
        }


        @Override
        public void surfaceDestroyed(SurfaceHolder holder) 
        {
                // Surface will be destroyed when replaced with a new screen
                //Always make sure to release the Camera instance
                camera.stopPreview();
                camera.release();
                camera = null;
        }

        public Camera getCamera()
        {
                return this.camera;
        }
}

This is code of basic surface that I got online. My question is that What should I change to make these things work with each other?

回答1:

After camera.startPreview() you have to write preview.setCamera(camera) to set camera. preview is the object of your class which extends SurfaceView and implements SurfaceHolder.Callback.

camera.startPreview();
preview.setCamera(camera);


回答2:

Manifest should include the following:

 <uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />

Have you added CameraSurfaceView to your layout? You can deploy it in your layout.xml or programmatically add it in onCreate() by layout.addView.