CameraView仅适用于模拟器

时间:2012-08-23 08:41:31

标签: java android eclipse android-emulator

在阅读本教程之后,我写了一篇小小的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) {

        }
    };
}

1 个答案:

答案 0 :(得分:1)

您遇到异常,因为您试图让相机使用它不支持的宽度和高度。您要求它使用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()是一种神奇的方法,它可以计算出最佳和最佳的相机预览尺寸。