Android Moto Droid相机每次都挂起onTakePicture

时间:2010-01-07 14:50:28

标签: android camera

所有,我一遍又一遍地用Google搜索找到解决方案,虽然我发现了一个关于相机释放的错误等等,但我无法为我的生活找到凸轮代码。每次我执行takePicture时,系统都会挂起,有时它会调用PictureCallback,但大部分时间它都会挂起。

关于无法读取/data/ap_gain.bin文件等的奇怪问题

以下是代码:

public class CameraActivity extends Activity implements Camera.PictureCallback, RequestConstants {

 private static final String TAG = "Camera";

 private Preview preview;

 private boolean previewRunning;

 private int addressNotificationId;

 @Override
 protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  addressNotificationId = getIntent().getIntExtra(REQ_RA_ID, 0);

  getWindow().setFormat(PixelFormat.TRANSLUCENT);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

  if (preview == null) {
   preview = new Preview(this);
  }
  setContentView(preview);
 }

 @Override
 protected void onDestroy() {
  if (isFinishing()) {
   preview.cleanup();
  }

  super.onDestroy();
 }

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_CAMERA) {

   /*
   preview.setDrawingCacheEnabled(true);
   Bitmap ss = preview.getDrawingCache();
   byte[] data = ImageUtility.getImageData(ss,75,1);
   Log.v(TAG, "Pic with size: " + data.length);

   ApplicationManager.getInstance().createPacketRecord(PacketConstants.PT_FLAG_ADDRESS_PHOTO, ApplicationDatabaseManager.getInstance().getRouteAddressBySystemId(addressNotificationId), data);
   finish();
   */
   preview.getCamera().takePicture(new Camera.ShutterCallback() {
    @Override
    public void onShutter() {

    }
   }, null, this);
   return true;
  }
  return super.onKeyDown(keyCode, event);
 }

 @Override
 public void onPictureTaken(byte[] data, Camera camera) {
  /*
  if (data == null || isFinishing())
   return;

  camera.stopPreview();
  previewRunning = false;
  camera.release();
  */
  //Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,data.length);
  //data = null;
  //data = ImageUtility.getImageData(bitmap, 75,1);

  Log.v(TAG, "Pic with size: " + data.length);


  ApplicationManager.getInstance().createPacketRecord(PacketConstants.PT_FLAG_ADDRESS_PHOTO, ApplicationDatabaseManager.getInstance().getRouteAddressBySystemId(addressNotificationId), data);
  finish();
 }

}

class Preview extends SurfaceView implements SurfaceHolder.Callback {
    SurfaceHolder mHolder;
    Camera mCamera;

    Preview(Context context) {
        super(context);

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();

        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    Camera getCamera() {
     return mCamera;
    }

    void cleanup() {
     mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }

    public void surfaceCreated(SurfaceHolder holder) {
        if (mCamera == null)
         mCamera = Camera.open();

        try {
           mCamera.setPreviewDisplay(holder);
        } catch (IOException exception) {
            mCamera.release();
            mCamera = null;
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(w, h);
        parameters.setPictureSize(w, h);
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }

}

2 个答案:

答案 0 :(得分:1)

您有时会拍照,有时候不拍照,因为您在发生回调之前正在释放相机。

当回叫触发时,根据相机是否已经释放,它将会或将无法访问拍摄的照片。

我建议您在拍照时确保没有放开相机或关闭表格。

更好的是,关闭照片回调中的表单。

此外,拍摄照片时,android的默认操作是停止预览。这不是一个错误,而是预期的性质。

答案 1 :(得分:0)

调用JpegPictureCallback后,您需要再次调用mCamera.startPreview。

API Camera示例中没有足够的信息来真正编写相机应用程序,但您可以在此处获取Google自己的相机应用程序的源代码。 Camera.java包含许多重要且有用的代码:

GIT中://android.git.kernel.org/platform/packages/apps/Camera.git