Android Camera SurfaceView take picture

I want to get a bitmap captured by calling a function from camera surface view class, but I always get error like this:

Any help will be appreciated~

java.lang.NullPointerException
        at com.etoff.appsopengl.CameraSurfaceView.setCapture(CameraSurfaceView.java:58)
        at com.etoff.appsopengl.Stage$MyRenderer.onDrawFrame(Stage.java:168)
        at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1467)
        at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1221)

this is the Stage class coding the calling coding:

public class Stage extends GLSurfaceView {

    CameraSurfaceView csv;
    Bitmap imgB;

public Stage(Context context, AttributeSet attrs) {
super(context, attrs);

    csv = new CameraSurfaceView(context);

}
     //inside renderer I call the function
     if(c==true){
            csv.setCapture();
            imgB = csv.getBitmap();
        }

}

this is the CameraSurfaceView class coding:

public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

Camera mCamera;
boolean isPreviewRunning = false;
Bitmap mBitmap;

CameraSurfaceView(Context context) {
    super(context);
    SurfaceHolder mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
    synchronized(this) {
        mCamera = Camera.open();
        try {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            Log.e("Camera", "mCamera.setPreviewDisplay(holder);");
        }
        mCamera.setDisplayOrientation(90);
        mCamera.startPreview();

    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    synchronized(this) {
        try {
            if (mCamera!=null) {
                mCamera.stopPreview();
                isPreviewRunning=false;
                mCamera.release();
            }
        } catch (Exception e) {
            Log.e("Camera", e.getMessage());
        }
    }
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

}

public void setCapture(){
    mCamera.takePicture(null,null,mPicture);
}

public Bitmap getBitmap(){
    return mBitmap;
}

private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inMutable = true;
        mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opt);

    }
};

}

Use this code to get your back camera id

private int findFrontFacingCamera() {
    int cameraId = -1;
    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
      CameraInfo info = new CameraInfo();
      Camera.getCameraInfo(i, info);
      if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
        Log.d(DEBUG_TAG, "Camera found");
        cameraId = i;
        break;
      }
    }
    return cameraId;
}

And use it like this

    // do we have a camera?
    if (!getPackageManager()
        .hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
      Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG)
          .show();
    } else {
      int cameraId = findFrontFacingCamera();
      if (cameraId < 0) {
        Toast.makeText(this, "No front facing camera found.",
            Toast.LENGTH_LONG).show();
      } else {
        mCamera = Camera.open(cameraId);
      }
    }
链接地址: http://www.djcxy.com/p/46520.html

上一篇: 从相机捕获图像并在活动中显示

下一篇: Android Camera SurfaceView拍照