我正在寻找一种在没有用户互动的情况下在Android中自动拍照的方法,我可以打开并拍照但我不能拍摄自动照片吗?
public void capturePhoto() {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(G.DIR_APP + "/tmp")));
startActivityForResult(intent, TAKE_PICTURE);
}
答案 0 :(得分:0)
你可以使用这样的方法
private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(final byte[] bytes,final Camera camera) {
// Do something here ... save, display ...
}
};
public void takePictureBack(ControllerState state){
Camera camera = null;
int cameraCount = Camera.getNumberOfCameras();
for (int cameraId = 0; cameraId < cameraCount; cameraId++) {
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
camera = Camera.open(cameraId);
break;
}
}
if (camera!= null) {
Camera.Parameters params = camera.getParameters();
// Check what resolutions are supported by your camera
List<Camera.Size> sizes = params.getSupportedPictureSizes();
// Iterate through all available resolutions and choose one.
for (Camera.Size size : sizes) {
...
}
params.setPictureSize( ... );
camera.setParameters(params);
camera.setPreviewDisplay(mCameraSourcePreview.getSurfaceView().getHolder());
camera.startPreview();
camera.takePicture(null,null,mPictureCallback)
}
}
但请注意,因为Android在用户不知道拍摄照片时不允许这种行为。用于预览的SurfaceView需要具有可见性visible
。