在android上使用app编写相机

时间:2013-07-30 09:02:41

标签: android eclipse camera

我是Android编程的新手,但已经尝试了几种方法来使用相机应用程序。

我希望每隔5分钟拍一张照片并发送到我的服务器,但我尝试的每种方法最终都会向我提供一个应用程序,它给我内置的相机应用程序,希望我按下快门。我需要这个自动化

科尔多瓦的'包装'就是这样做的。 Android开发者页面上的示例就是这样,我怀疑在我编写Android编程书时,相机pp示例也会这样做。

1 个答案:

答案 0 :(得分:0)

//somewhere in your code call this (Maybe you need to set up a timer):
mCamera.takePicture(null, null, mCall); 

//this should be done before using camera
Camera mCamera; 
private boolean safeCameraOpen(int id) {
boolean qOpened = false;

    try {
        releaseCamera();
        mCamera = Camera.open(id);
        qOpened = (mCamera != null);
    } catch (Exception e) {
        Log.e(getString(R.string.app_name), "failed to open Camera");
        e.printStackTrace();
    }

    return qOpened;    
}

private void releaseCamera() {
    if (mCamera != null) {
        mCamera.release();
        mCamera = null;
    }
}

Camera.PictureCallback mCall = new Camera.PictureCallback() {

   // you need to change this method due to your needs
   public void onPictureTaken(byte[] data, Camera camera) {
         //decode the data obtained by the camera into a Bitmap

         FileOutputStream outStream = null;
              try {
                  outStream = new FileOutputStream("/sdcard/Image.jpg");
                  outStream.write(data);
                  outStream.close();
              } catch (FileNotFoundException e){
                  Log.d("CAMERA", e.getMessage());
              } catch (IOException e){
                  Log.d("CAMERA", e.getMessage());
              }

   }
};