0
我想弄清楚如何設置camera2video使用前置攝像頭。我新的Android開發,並試圖找出這一點。Android Camera2video使用前置攝像頭
我想弄清楚如何設置camera2video使用前置攝像頭。我新的Android開發,並試圖找出這一點。Android Camera2video使用前置攝像頭
如果前方照相機確實存在,並返回其ID(-1,如果不存在的話)
private int findFrontFacingCamera() {
int cameraId = -1;
// Search for the front facing camera
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
Log.d("CAMERA", "Camera found");
cameraId = i;
break;
}
}
return cameraId;
}
這塊方法將幫助ü使用您的前置攝像頭這種方法發現:
private void useFrontFacingCamera() {
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG).show();
} else {
cameraId = findFrontFacingCamera();
if (cameraId < 0) {
Toast.makeText(this, "No front facing camera found.",Toast.LENGTH_LONG).show();
} else {
camera = Camera.open(cameraId);
camera.unlock();
}
}
//in my case i have to use frontal camera to make a scan on a qr code :
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.setCameraId(cameraId); // <-- use this method to use your camera
scanIntegrator.initiateScan();
}
重要提示:如果您想在其他活動中使用您的相機,請不要忘記以下方法:
@Override
protected void onPause() {
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
Log.d("camera", "releaseCamera -- done");
}
super.onPause();
}
@Override
protected void onResume() {
if (camera != null) {
Camera.open();
Log.d("camera", "openCamera -- done");
}
super.onResume();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
Log.e("camera", "releaseCamera -- done");
}
}