-1

我对android编程很陌生,我想做的就是在表面视图中打开相机应用程序,以便我可以打开相机并设置一些参数,如下所示?

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

我看到评论说我必须通过一个表面。所以我创建了以下曲面:

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;
        }
}

这是我上网的基本表面代码。我的问题是,我应该改变什么才能使这些东西相互配合?

4

2 回答 2

1

之后camera.startPreview()你必须写preview.setCamera(camera)设置相机。preview是扩展SurfaceView和实现的类的对象SurfaceHolder.Callback

camera.startPreview();
preview.setCamera(camera);
于 2014-06-10T10:19:32.327 回答
0

清单应包括以下内容:

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

你添加CameraSurfaceView到你的布局了吗?您可以将其部署在您的layout.xml或以编程方式将其添加onCreate()layout.addView.

于 2013-10-30T03:04:13.683 回答