1

我收到 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();

4

3 回答 3

2

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

请参阅文档以获取与拍照有关的清单

于 2012-04-05T17:16:29.357 回答
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() 中重新打开()它)。

于 2012-04-05T17:42:37.493 回答
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
   }

瞧!陷阱已停用。

当然,不要忘记清单权限....

于 2014-09-05T07:54:26.320 回答