我是 android 编程新手,但尝试了几种使用相机应用程序的方法。
我希望每 5 分钟拍摄一张照片并将其发送到我的服务器,但我尝试过的每种方法最终都会为我提供一个应用程序,该应用程序为我提供了内置的相机应用程序,希望我按下快门。我需要这个自动化
Cordova 中的“包装器”就是这样做的。android 开发者页面上的示例就是这样做的,当我阅读 Android 编程书籍时,我怀疑相机 pp 示例也会这样做。
//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());
}
}
};