如何以编程方式为Android启用前置摄像头而不是后置摄像头?

时间:2018-08-10 06:18:34

标签: android android-camera2

我使用旧手机的后置摄像头已损坏,因此我想将默认摄像头设置为前置摄像头。是否可以在每次打开摄像头应用时始终打开前置摄像头?

3 个答案:

答案 0 :(得分:1)

尝试一下!

Camera.open(android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT);//avoid passing hardcode values

答案 1 :(得分:1)

这可能会有所帮助。来源网址:this

Button otherCamera = (Button) findViewById(R.id.OtherCamera);

otherCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inPreview) {
    camera.stopPreview();
}
//NB: if you don't release the current camera before switching, you app will crash
camera.release();

//swap the id of the camera to be used
if(currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK){
    currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
}
else {
    currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
}
camera = Camera.open(currentCameraId);

try {

    camera.setPreviewDisplay(previewHolder);
} catch (IOException e) {
    e.printStackTrace();
}
camera.startPreview();
}

答案 2 :(得分:0)

import { query } from 'itiriri';

const map = new Map();
map.set(1, 'Alice');
map.set(2, 'Bob');

const result = query(map)
  .filter([k, v] => v.indexOf('A') >= 0)
  .map([k, v] => `k - ${v.toUpperCase()}`);

for (const r of result) {
  console.log(r); // prints: 1 - ALICE
}

注意:

对于CAMERA_FACING_BACK为0

1(代表CAMERA_FACING_FRONT)