gpt4 book ai didi

android - 即时切换后置/前置摄像头

转载 作者:IT老高 更新时间:2023-10-28 22:14:24 27 4
gpt4 key购买 nike

如果我只使用 CAMERA_FACING_BACKCAMERA_FACING_FRONT 一切正常。

我在从 CAMERA_FACING_BACK 切换到 CAMERA_FACING_FRONT 时遇到问题。

我的代码 fragment :

public class PhotoCameraActivity extends Activity implements OnClickListener {
private SurfaceView cameraView;
private Button turnButton;
private Camera camera = null;
private Callback listener;
private static int camId = Camera.CameraInfo.CAMERA_FACING_BACK;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo_camera_main);
prepareActivity();
}

private void prepareActivity() {
cameraView = (SurfaceView) findViewById(R.id.photo_camera_surface_view);

turnButton = (ImageButton) findViewById(R.id.turn_button);
turnButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
if (v.equals(turnButton)) {
if (Camera.getNumberOfCameras() > 1 && camId < Camera.getNumberOfCameras() - 1) {
startCamera(camId + 1);
} else {
startCamera(Camera.CameraInfo.CAMERA_FACING_BACK);
}
}
}

@Override
protected void onResume() {
startCamera(camId);
super.onResume();
}

@Override
protected void onPause() {
stopCamera();
super.onPause();
}

private void startCamera(int cameraId) {
if (camera != null) {
stopCamera();
}
holder = cameraView.getHolder();
listener = new Callback() {

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
};
holder.addCallback(listener);


camId = cameraId;
camera = Camera.open(cameraId);
Camera.Parameters params = camera.getParameters();
if (cameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
params.setPreviewSize(1280, 800);
} else {
params.setPreviewSize(640, 480);
}
camera.setParameters(params);

}

private void stopCamera(){
System.out.println("stopCamera method");
if (camera != null){
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
holder.removeCallback(listener);
holder = null;
}
}

}

最佳答案

在我的 Activity 的 onCreate() 中,我将以下 onClick 监听器添加到覆盖在我的 Preview SurfaceView 上的按钮(网络上有许多示例用于预览):

ImageButton useOtherCamera = (ImageButton) findViewById(R.id.useOtherCamera);
//if phone has only one camera, hide "switch camera" button
if(Camera.getNumberOfCameras() == 1){
useOtherCamera.setVisibility(View.INVISIBLE);
}
else {
useOtherCamera.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);
//Code snippet for this method from somewhere on android developers, i forget where
setCameraDisplayOrientation(CameraActivity.this, currentCameraId, camera);
try {
//this step is critical or preview on new camera will no know where to render to
camera.setPreviewDisplay(previewHolder);
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}

在我的测试设备上,后置摄像头的 ID 为 0,前置摄像头的 ID 为 1。我建议使用 Camera.CameraInfo 静态变量作为摄像头 ID,而不是硬编码值。我确信这只会在其他设备上引起问题。

关于android - 即时切换后置/前置摄像头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6599454/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com