0
這個想法很簡單:我的應用程序應該創建一個攝像頭對象,顯示預覽並拍照(不按任何按鈕),然後關閉攝像頭。不幸的是,它甚至沒有拍照。 (意思是:我的回調代碼應在拍攝照片後執行)永遠不會達到。)android:攝像頭不拍攝
我正在爲android相機api使用一個小包裝類。我的活動代碼如下所示:
camera = new SurveillanceCamera(this, layoutForPreview);
camera.start();
camera.takePicture();
包裝類:
public SurveillanceCamera(Context context, LinearLayout previewLayout) {
cameraInstance = Camera.open(findBackFacingCamera());
pictureCallback = getPictureCallback();
cameraPreview = new CameraPreview(context, cameraInstance);
this.previewLayout = previewLayout;
this.previewLayout.addView(cameraPreview);
cameraInstance.startPreview();
}
public void takePicture() {
//this code is reached
cameraInstance.takePicture(null, null, pictureCallback);
}
public void start() {
if (cameraInstance == null) {
cameraInstance = Camera.open(findBackFacingCamera());
pictureCallback = getPictureCallback();
cameraPreview.setCamera(cameraInstance);
cameraInstance.startPreview();
}
}
private PictureCallback getPictureCallback() {
PictureCallback picture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.e(TAG, "Jey picture was taken...");
// refresh camera to continue preview
cameraPreview.refreshCamera(cameraInstance);
}
};
return picture;
}
private int findBackFacingCamera() {
int cameraId = -1;
// Search for the back facing camera
// get the number of cameras
int numberOfCameras = Camera.getNumberOfCameras();
// for every camera check
for (int i = 0; i < numberOfCameras; i++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
cameraId = i;
break;
}
}
return cameraId;
}
沒有例外的代碼中引發,但在onPictureTaken的Log.e()不顯示。看起來,相機預覽顯示,但沒有任何反應。是否有可能,執行camera.takePicture()時,預覽未完全加載?有什麼建議麼?
http://examples.javacodegeeks.com/android/core/hardware/camera-hardware/android-camera-example/&http://www.tutorialspoint.com/android/android_camera.htm –
對不起,但那是不是一個非常有用的答案。我已經檢查了很多教程。我的相機正在拍照,如果我只是使用一個按鈕來觸發camera.takePicture()方法。但如果我試圖直接拍攝照片,如上所示,它不起作用。爲什麼?以及如何在沒有按鈕觸發事件的情況下拍照? –