我知道有很多关于这个问题,但我想我面临的问题是不以任何人处理。从各种不同的问题,我写了这个服务。问题是我在onStartCommand方法所有的try-catch块获得成功,但在回调方法(ShutterCallback和PictureCallback)日志没有得到处理。我认为这意味着拍摄照片,但回调不叫,这是奇怪的。
照相机凸轮;参数参数;PictureCallback rawCallback =新PictureCallback(){ 公共无效onPictureTaken(字节[]数据,相机摄像头){ Log.d(照相机,onPictureTaken - 原始); }};ShutterCallback shutterCallback =新ShutterCallback(){ 公共无效onShutter(){ Log.i(照相机,onShutter'd); }};@覆盖公众的IBinder onBind(意向为arg0){ // TODO自动生成方法存根 返回null;}@覆盖公众诠释onStartCommand(意向意图,诠释标志诠释startId){ 尝试{ 凸轮= Camera.open(); Log.i(照相机,成功); }赶上(RuntimeException的E){ Log.e(照相机,摄像机当前不可用); e.printStackTrace(); } 尝试{ 参数= cam.getParameters(); cam.setParameters(参数); Log.i(照相机,成功); }赶上(例外E1){ Log.e(照相机,参数问题); e1.printStackTrace(); } 尝试{ SurfaceView视图=新SurfaceView(本); cam.set previewDisplay(view.getHolder()); cam.start preVIEW(); Log.i(照相机,成功); }赶上(例外五){ Log.e(照相机,表面问题); e.printStackTrace(); } 尝试{ cam.takePicture(shutterCallback,rawCallback,NULL); Log.i(照相机,成功); }赶上(例外五){ Log.e(照相机,点击失败); e.printStackTrace(); } cam.release(); 返回super.onStartCommand(意向,旗帜,startId);}解决方案
您不应该调用takePicture方法后立即释放相机。你需要给镜头足够的时间来执行回调。顺便说一句,你最好也加JPG回调。同时停止preVIEW可以释放相机之前被调用。您code可能看起来像如下:
照相机凸轮;参数参数;PictureCallback rawCallback =新PictureCallback(){ 公共无效onPictureTaken(字节[]数据,相机摄像头){ Log.d(照相机,onPictureTaken - 原始); camera.stop preVIEW(); camera.release(); }};PictureCallback jpgCallback =新PictureCallback(){ 公共无效onPictureTaken(字节[]数据,相机摄像头){ Log.d(照相机,onPictureTaken - JPG); camera.stop preVIEW(); camera.release(); }};ShutterCallback shutterCallback =新ShutterCallback(){ 公共无效onShutter(){ Log.i(照相机,onShutter'd); }};@覆盖公众的IBinder onBind(意向为arg0){ // TODO自动生成方法存根 返回null;}@覆盖公众诠释onStartCommand(意向意图,诠释标志诠释startId){ 尝试{ 凸轮= Camera.open(); Log.i(照相机,成功); }赶上(RuntimeException的E){ Log.e(照相机,摄像机当前不可用); e.printStackTrace(); } 尝试{ 参数= cam.getParameters(); cam.setParameters(参数); Log.i(照相机,成功); }赶上(例外E1){ Log.e(照相机,参数问题); e1.printStackTrace(); } 尝试{ SurfaceView视图=新SurfaceView(本); cam.set previewDisplay(view.getHolder()); cam.start preVIEW(); Log.i(照相机,成功); }赶上(例外五){ Log.e(照相机,表面问题); e.printStackTrace(); } 尝试{ cam.takePicture(shutterCallback,rawCallback,NULL); Log.i(照相机,成功); }赶上(例外五){ Log.e(照相机,点击失败); e.printStackTrace(); } //注释掉以下行和移动到你的回调 //cam.release(); 返回super.onStartCommand(意向,旗帜,startId);}I know there are a lot of questions regarding this, but i think the problem i am facing is not addressed in any of them. From all the different questions i wrote this Service. The problem is that i am getting "Success" in all the try-catch blocks in the onStartCommand method, but the logs in the Callback methods (ShutterCallback and PictureCallback) are not getting processed. Which I think means that the picture is taken but the Callbacks are not called, which is weird.
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); }解决方案
You should not release the camera immediately after calling takePicture method. You need to give the camera enough time to execute the callbacks. BTW, you'd better to also add a JPG callback. Also stopPreview can be called before releasing camera. Your code could look like below:
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); }
更多推荐
后台服务采取与摄像头像素
发布评论