我知道有很多关于此的问题,但我认为我遇到的问题并没有在任何一个问题上得到解决。 从我写这个服务的所有不同问题。问题是我在onStartCommand方法的所有try-catch块中获得了“Success”,但是回调方法(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);
}
答案 0 :(得分: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);
}
答案 1 :(得分:0)
您已经错过了这些关键点:
SurfaceView
添加到WindowManager。view.getHolder()
上,使用addCallback
设置并初始化预览,并在回调的surfaceCreated
方法中拍摄照片。答案 2 :(得分:0)
你正在传递" null"作为takePicture方法中的第三个参数,属于PictureCallback。
您的代码:
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();
}