阅读本教程后,我已经写了一篇简短的Camera-Appliction:

http://www.brighthub.com/mobile/google-android/articles/43414.aspx

在模拟器中,一切正常(好,没有摄像头,但至少没有错误;))

然后,我尝试使其在HTC Wildfire S上运行。但是启动后,应用立即崩溃。

这是我的活动的代码,也许有人有一个想法,如何解决问题:

public class CameraView extends Activity implements SurfaceHolder.Callback {

    android.hardware.Camera mCamera;
    boolean mPreviewRunning = false;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.camera_surface);
        SurfaceView mSurfaceView = (SurfaceView)findViewById(R.id.surface_camera);
        SurfaceHolder mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }


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


    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        if(mPreviewRunning) {
            mCamera.stopPreview();
        }

        android.hardware.Camera.Parameters params = mCamera.getParameters();
        params.setPreviewSize(width, height);
        mCamera.setParameters(params);

        try {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        mCamera.startPreview();
        mPreviewRunning = true;
    }

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

    android.hardware.Camera.PictureCallback mPictureCallback = new android.hardware.Camera.PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, android.hardware.Camera camera) {

        }
    };
}

最佳答案

之所以会出现异常,是因为您试图使相机使用它不支持的宽度和高度。您要求它使用SurfaceView的宽度和高度,该宽度和高度很少与支持的预览尺寸之一匹配。相反,请尝试使用以下命令:

   public class CameraView extends Activity implements SurfaceHolder.Callback {

android.hardware.Camera mCamera;
boolean mPreviewRunning = false;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.camera_surface);
    SurfaceView mSurfaceView = (SurfaceView)findViewById(R.id.surface_camera);
    SurfaceHolder mSurfaceHolder = mSurfaceView.getHolder();
    mSurfaceHolder.addCallback(this);
    mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}


@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
        mCamera.setPreviewDisplay(previewHolder);
    }
    catch (Throwable t) {
        Log.e(TAG, "Exception in setPreviewDisplay()", t);
    }
}


@Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    Camera.Parameters parameters=camera.getParameters();
    Camera.Size size=getBestPreviewSize(width, height, parameters);

    if (size!=null) {
        parameters.setPreviewSize(size.width, size.height);
        camera.setParameters(parameters);
        camera.startPreview();
        inPreview=true;
    }
  }

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

android.hardware.Camera.PictureCallback mPictureCallback = new android.hardware.Camera.PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, android.hardware.Camera camera) {

    }
};

    private Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters) {
    Camera.Size result=null;

    for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
        if (size.width<=width && size.height<=height) {
            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);
}
}


这段代码全部改编自我为本书编写的代码。我更改了一些代码,因此可能需要修复一些变量名和导入,但它应该可以正常工作。

getBestPreviewSize()是这里的魔术方法,用于计算要使用的最佳和最佳相机预览大小。

10-06 06:31