首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2ME开发 >

关于Android Camera 新手

2012-12-19 
关于Android Camera新手求助本帖最后由 syl7752 于 2012-02-10 11:15:02 编辑一个测试用例 多处调用Camera

关于Android Camera 新手求助
本帖最后由 syl7752 于 2012-02-10 11:15:02 编辑 一个测试用例 多处调用Camera 弹出错误fail to connect to camera service 
部分代码如下:

private void initializeMessageLooper(final int cameraId) throws Exception{
   final ConditionVariable startDone = new ConditionVariable();
   new Thread() {
       @Override
       public void run() {
           Log.v(TAG, "start loopRun");
           // Set up a looper to be used by camera.
           Looper.prepare();
           // Save the looper so that we can terminate this thread
           // after we are done with it.
           mLooper = Looper.myLooper();
           try{
           mCamera = Camera.open(cameraId);
           }catch (Exception e) {
// TODO: handle exception
          
}
           Log.v(TAG, "camera is opened");
           startDone.open();
           Looper.loop(); // Blocks forever until Looper.quit() is called.
           if (LOGV) Log.v(TAG, "initializeMessageLooper: quit.");
       
       }
   }.start();

   Log.v(TAG, "start waiting for looper");
   if (!startDone.block(WAIT_FOR_COMMAND_TO_COMPLETE)) {
       Log.v(TAG, "initializeMessageLooper: start timeout");
       fail("initializeMessageLooper: start timeout");
   }
}

private void terminateMessageLooper() throws Exception {
   mLooper.quit();
   // Looper.quit() is asynchronous. The looper may still has some
   // preview callbacks in the queue after quit is called. The preview
   // callback still uses the camera object (setHasPreviewCallback).
   // After camera is released, RuntimeException will be thrown from
   // the method. So we need to join the looper thread here.
   mLooper.getThread().join();
   mCamera.release();
   mCamera = null;

}

@UiThreadTest
public void testTakePicture() throws Exception {
  int nCameras = Camera.getNumberOfCameras();
  for (int id = 0; id < nCameras; id++) {


      Log.v(TAG, "Camera id=" + id);
      testTakePictureByCamera(id);
  }
}

private void testTakePictureByCamera(int cameraId) throws Exception {

  initializeMessageLooper(cameraId);
  Size pictureSize = mCamera.getParameters().getPictureSize();
  mCamera.setPreviewDisplay(getActivity().getSurfaceView().getHolder());
  mCamera.startPreview();
  mCamera.autoFocus(mAutoFocusCallback);
  assertTrue(waitForFocusDone());
  mCamera.autoFocus(mAutoFocusCallback);
  assertTrue(waitForFocusDone());
  mJpegData = null;
  mCamera.takePicture(mShutterCallback, mRawPictureCallback, mJpegPictureCallback);
  waitForSnapshotDone();
  terminateMessageLooper();
  assertTrue(mShutterCallbackResult);
  assertTrue(mJpegPictureCallbackResult);
  assertNotNull(mJpegData);
  Bitmap b = BitmapFactory.decodeByteArray(mJpegData, 0, mJpegData.length);
  assertEquals(pictureSize.width, b.getWidth());
  assertEquals(pictureSize.height, b.getHeight());
}


我在每次用完Camera之后都释放了。却老提示Camera 空指针,在获得对象时fail to connect camera service ,是代码有错误吗?
[解决办法]
自己顶下
[解决办法]
啥问题。。。
[解决办法]
引用:
啥问题。。。
java.lang.RuntimeException fail to connect camera service 出在Camera.open()
[解决办法]
该回复于2012-02-15 15:10:25被版主删除
[解决办法]
该回复于2012-02-16 09:06:11被版主删除
[解决办法]
问题解决了,来人给分结贴 
[解决办法]
请问是怎么解决的呢?   能分享一下吗!

热点排行