在摄像机设置参数上获取空指针异常

时间:2012-04-05 17:09:14

标签: android parameters nullpointerexception android-camera

我收到NullPointerException错误,它引用了Camera.Parameters:

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int w,
        int h) {
    // TODO Auto-generated method stub
        Log.e(TAG, "surfaceChanged");

        if (mPreviewRunning) {
             mCamera.stopPreview();
             mPreviewRunning = false;
             Log.e(TAG, "stopPeview");
            }
        Camera.Parameters p = mCamera.getParameters();
        Log.e(TAG, "paarameters");

        p.setPreviewSize(w, h);

        mCamera.setParameters(p);  
        Log.e(TAG, " set parameters");
            try {
                mCamera.setPreviewDisplay(holder);

                Log.e(TAG, "setPreview");
                } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mCamera.startPreview();
            mPreviewRunning = true;
    }   

有人能告诉我上面代码中的错误是什么。

我将不得不再次测试它以获取日志,但这是整个代码。我确实改变了清单。

public class Picture extends Activity implements SurfaceHolder.Callback{

private String TAG;
private LayoutInflater mInflater = null;
private Camera mCamera;
boolean mPreviewRunning = false;
private SurfaceHolder mSurfaceHolder;
private SurfaceView mSurfaceView;
Button takepicture;
byte[] tempdata;

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    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);

    mCamera = getCameraInstance (); 

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

    mInflater = LayoutInflater.from(this);
    View overView = mInflater.inflate(R.layout.cameraoverlay, null);
    this.addContentView(overView, new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));

    takepicture = (Button) findViewById(R.id.button);
    takepicture.setOnClickListener(new OnClickListener() {

    public void onClick(View view) {
                mCamera.takePicture(shutterCallback, mPictureCallback,
                jpegCallback);
        }
    });

}

private Camera getCameraInstance() {
    // TODO Auto-generated method stub
    Camera mCamera = null;
    try {
        mCamera = Camera.open();
    } catch (Exception e) {

    }
    return mCamera;
}


ShutterCallback shutterCallback = new ShutterCallback() {
    @Override
    public void onShutter() {}
};
    PictureCallback mPictureCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera c) {}
};
PictureCallback jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera c) {
            if(data != null) {
                tempdata=data;
                done();
            }
        }
    };

    void done(){
        Bitmap bm = BitmapFactory.decodeByteArray(tempdata, 0, tempdata.length);
        String url = Images.Media.insertImage(getContentResolver(),
                bm, null, null);        
        bm.recycle();
        Bundle bundle = new Bundle();
        if(url!=null) {
            bundle.putString("url", url);

            Intent mIntent = new Intent();
            mIntent.putExtras(bundle);
            setResult(RESULT_OK, mIntent);          
        } else {
            Toast.makeText(this, "Picture can not be saved",
                            Toast.LENGTH_SHORT).show();
        }
        finish();
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int w,
        int h) {
    // TODO Auto-generated method stub
        Log.e(TAG, "surfaceChanged");

        if (mPreviewRunning) {
             mCamera.stopPreview();
             mPreviewRunning = false;
             Log.e(TAG, "stopPeview");
            }
        Camera.Parameters p = mCamera.getParameters();
        Log.e(TAG, "paarameters");

        p.setPreviewSize(w, h);

        mCamera.setParameters(p);  
        Log.e(TAG, " set parameters");
            try {
                mCamera.setPreviewDisplay(holder);

                Log.e(TAG, "setPreview");
                } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mCamera.startPreview();
            mPreviewRunning = true;
    }   

@Override
public void surfaceCreated(SurfaceHolder holder) {
    Log.e(TAG, "surfaceCreated");
    try {
    mCamera = Camera.open();
    Log.e(TAG, "camera open");

    } catch(Exception e) {}

    }



@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    Log.e(TAG, "surfaceDestroyed");

    mCamera.stopPreview();  
    mPreviewRunning = false;
    mCamera.release();        // release the camera for other applications
    mCamera = null;

}
}

此外,引用的日志行是行:Camera.Parameters p = mCamera.getParameters();

3 个答案:

答案 0 :(得分:2)

看起来mCamera是null ...你初始化了吗? (mCamera = Camera.open();

请参阅docs获取与拍照相关的清单

答案 1 :(得分:2)

在表现文件::

中添加以下三行
 <uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />

要使用此课程拍摄照片,请使用以下步骤:

1)从open(int)获取Camera实例。

2)使用getParameters()获取现有(默认)设置。

3)如有必要,修改返回的Camera.Parameters对象并调用 setParameters(Camera.Parameters)。

4)如果需要,请调用setDisplayOrientation(int)。

5)重要事项:将完全初始化的SurfaceHolder传递给setPreviewDisplay(SurfaceHolder)。没有表面,相机将无法开始预览。

6)重要提示:调用startPreview()开始更新预览曲面。必须先开始预览才能拍照。

7)如果需要,可以调用takePicture(Camera.ShutterCallback,Camera.PictureCallback,Camera.PictureCallback,Camera.PictureCallback)来捕捉照片。等待回调提供实际的图像数据。

8)拍照后,预览显示将停止。要拍摄更多照片,请先再次调用startPreview()。

9)调用stopPreview()以停止更新预览表面。

10)重要事项:调用release()以释放相机以供其他应用程序使用。应用程序应立即在onPause()中释放相机(并在onResume()中重新打开())。

答案 2 :(得分:1)

有许多解决方案,但这是一个简单,便宜的选项,对我有用:

try{
   mCamera.autoFocus(autoFocusCB); //Or whatever part of code that crashes
   }
catch(Exception e){
   Log.v("joshtag","THIS PHONE DOES NOT SUPPORT AUTOFOCUS!!"); //a warning, popup, whatever
   }

瞧!陷阱已停用。

当然,不要忘记明显的权限......