2013-07-30 32 views
-3

我是新来的android编程,但已尝试使用相机应用程序的几种方法。在Android上使用应用程序写相机

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

科尔多瓦的'包装'这样做。 Android开发人员页面上的示例执行此操作,并且我在通过Android编程书籍工作时怀疑,相机pp示例也会这样做。

+1

您有问题吗?除了“请给我代码”? – Simon

回答

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()); 
       } 

    } 
};