2

我知道对此有很多问题,但我认为我面临的问题没有在其中任何一个中得到解决。我写了这个服务的所有不同问题。问题是我在 onStartCommand 方法的所有 try-catch 块中都获得了“成功”,但是回调方法(ShutterCallback 和 PictureCallback)中的日志没有得到处理。我认为这意味着拍摄了照片但没有调用回调,这很奇怪。

Camera cam;
Parameters param;
PictureCallback rawCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.d("CAMERA", "onPictureTaken - raw");
    }
};

ShutterCallback shutterCallback = new ShutterCallback() {
    public void onShutter() {
        Log.i("CAMERA", "onShutter'd");
    }
};

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    try {
        cam = Camera.open();
        Log.i("CAMERA", "Success");
    } catch (RuntimeException e) {
        Log.e("CAMERA", "Camera currently unavailable");
        e.printStackTrace();
    }
    try {
        param = cam.getParameters();
        cam.setParameters(param);
        Log.i("CAMERA", "Success");
    } catch (Exception e1) {
        Log.e("CAMERA", "Parameter problem");
        e1.printStackTrace();
    }
    try {
        SurfaceView view = new SurfaceView(this);
        cam.setPreviewDisplay(view.getHolder());
        cam.startPreview();
        Log.i("CAMERA", "Success");
    } catch (Exception e) {
        Log.e("CAMERA", "Surface Problem");
        e.printStackTrace();
    }
    try {
        cam.takePicture(shutterCallback, rawCallback, null);
        Log.i("CAMERA", "Success");
    } catch (Exception e) {
        Log.e("CAMERA", "Click Failure");
        e.printStackTrace();
    }
    cam.release();
    return super.onStartCommand(intent, flags, startId);
}
4

3 回答 3

2

调用 takePicture 方法后不应立即释放相机。您需要给相机足够的时间来执行回调。顺便说一句,您最好还添加一个 JPG 回调。也可以在释放相机之前调用 stopPreview。您的代码可能如下所示:

Camera cam;
Parameters param;
PictureCallback rawCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.d("CAMERA", "onPictureTaken - raw");
        camera.stopPreview();
        camera.release();
    }
};

PictureCallback jpgCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.d("CAMERA", "onPictureTaken - jpg");
        camera.stopPreview();
        camera.release();
    }
};

ShutterCallback shutterCallback = new ShutterCallback() {
    public void onShutter() {
        Log.i("CAMERA", "onShutter'd");
    }
};

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    try {
        cam = Camera.open();
        Log.i("CAMERA", "Success");
    } catch (RuntimeException e) {
        Log.e("CAMERA", "Camera currently unavailable");
        e.printStackTrace();
    }
    try {
        param = cam.getParameters();
        cam.setParameters(param);
        Log.i("CAMERA", "Success");
    } catch (Exception e1) {
        Log.e("CAMERA", "Parameter problem");
        e1.printStackTrace();
    }
    try {
        SurfaceView view = new SurfaceView(this);
        cam.setPreviewDisplay(view.getHolder());
        cam.startPreview();
        Log.i("CAMERA", "Success");
    } catch (Exception e) {
        Log.e("CAMERA", "Surface Problem");
        e.printStackTrace();
    }
    try {
        cam.takePicture(shutterCallback, rawCallback, null);
        Log.i("CAMERA", "Success");
    } catch (Exception e) {
        Log.e("CAMERA", "Click Failure");
        e.printStackTrace();
    }
    // Commented out following line and moved it into your callbacks
    //cam.release();
    return super.onStartCommand(intent, flags, startId);
}
于 2013-10-14T23:39:19.303 回答
0

你错过了这些关键点:

  1. SurfaceView必须添加到 WindowManager 。
  2. 在尝试使用它之前,您没有等待创建表面。在回调的方法中view.getHolder(),使用addCallback设置和初始化预览和拍照。surfaceCreated
于 2014-11-22T23:13:19.840 回答
0

您在属于 PictureCallback 的 takePicture 方法中将“null”作为第三个参数传递。

你的代码:

try {
        cam.takePicture(shutterCallback, rawCallback, null);
        Log.i("CAMERA", "Success");
    } catch (Exception e) {
        Log.e("CAMERA", "Click Failure");
        e.printStackTrace();
    }

更改如下:

try {
            cam.takePicture(shutterCallback,null,rawCallback);
            Log.i("CAMERA", "Success");
        } catch (Exception e) {
            Log.e("CAMERA", "Click Failure");
            e.printStackTrace();
        }
于 2015-08-14T07:31:49.740 回答