0

在我的库项目中,我有以下代码来初始化相机并开始预览。

public class CameraView  {

private SurfaceView preview;
private SurfaceHolder previewHolder;
private Camera camera;
private boolean activeCamera;
private boolean cameraConfigured;
private Activity activity;
private final static int NINTY_DEGREES = 90; 


public CameraView(Activity activity) {
    this.activity = activity;

    View rootView;
    LayoutInflater inflater = activity.getLayoutInflater();
    rootView = inflater.inflate(R.layout.layout_cameraview, null);
    preview = (SurfaceView) rootView.findViewById(R.id.surfaceView);
    previewHolder = preview.getHolder();
    previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    cameraConfigured = false;
}

private void initializeCamera(int w, int h) {
    if(camera != null && previewHolder.getSurface() != null) {

        try 
        {
            camera.setPreviewDisplay(previewHolder);
        }
        catch(Exception e)
        {
            Log.e("Camera: ", e.getMessage());
        }


        if(!cameraConfigured) {
            Camera.Parameters params = camera.getParameters();
            Camera.Size size = getBestPreviewSize(w, h, params);

            if(size != null) {
                params.setPreviewSize(size.width, size.height);
                cameraConfigured = true;
            }
        }
    }
}

public void startCamera() {
    camera = Camera.open();
    previewHolder.addCallback(surfaceCallback);
}

public void stopCamera() {
    stopPreviewingOnDisplay();
}

public void takePicture() {
    View rootView;
    LayoutInflater inflater = activity.getLayoutInflater();
    rootView = inflater.inflate(R.layout.layout_cameraview, null);
    Button capture = (Button) rootView.findViewById(R.id.capture);
    capture.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.e("CameraView: ", "Capture button pressed!");
        }
    });
}

private void startPreviewingOnDisplay() {
    if(camera != null) {
        camera.setDisplayOrientation(NINTY_DEGREES);
        camera.startPreview();
        activeCamera = true;
    }
}

private void stopPreviewingOnDisplay() {
    if(camera != null && activeCamera) {
        camera.stopPreview();
        activeCamera = false;
    }
}

private SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {    
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        initializeCamera(width, height);
        startPreviewingOnDisplay();
        Log.d("CameraView: ", "SurfaceChanged");
    }
};

private Camera.Size getBestPreviewSize(int w, int h, Camera.Parameters params) {

    Camera.Size result = null;

    for(Camera.Size size : params.getSupportedPreviewSizes()) {
        if(size.width < w && size.height <= h) {
            if(result == null) {
                result = size;
            }
            else {
                int resultArea = result.width * result.height;
                int newArea = size.width * size.height;

                if(newArea > resultArea)
                    result = size;
            }
        }
    }       
    return(result);
}

}

这个类的用法:

public class History extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
                ......
       camera = new CameraView(this);
    }

    @Override
    public void onResume() { 
       super.onResume();
       Log.i("History.class", "onResume called!");
       camera.startCamera();
    }

    @Override
    public void onPause() {
       super.onPause();
       camera.stopCamera();
    }
}

我已经调试了代码,我注意到相机对象正在初始化,但屏幕上没有出现任何内容,它应该已经预览了相机。谁能看出这种方法有什么问题?

谢谢。

4

0 回答 0