gpt4 book ai didi

java - 我怎样才能在android代码中同时打开手电筒(闪光灯)和后置摄像头

转载 作者:行者123 更新时间:2023-12-01 18:17:35 28 4
gpt4 key购买 nike

当我打开闪光灯时。如果我使用camera.release()方法将相机用于后置摄像头图像拍摄闪光灯关闭,则相机停止。

switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@SuppressLint("MissingPermission")
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
camera = Camera.open(); // for camera on purpose
Camera.Parameters para = camera.getParameters();
para.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(para);
camera.startPreview();
cameraSource.release();
camera = null;
try {
cameraSource.start(surfaceView.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
      //   when I turned on the flash. the camera stops if I use camera.release() method to use the camera for back camera image taking the flash turns off.  

最佳答案

在 API 23 或更高版本中

这就是开启方式

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
String cameraId = null;
try {
cameraId = camManager.getCameraIdList()[0];
camManager.setTorchMode(cameraId, true); //Turn ON
} catch (CameraAccessException e) {
e.printStackTrace();
}
}

这就是关闭的方法

camManager.setTorchMode(cameraId, false);

此操作所需的权限

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>

完整的实现可能如下所示

public class CameraAndFlashProvider {

private static final String TAG = FlashlightProvider.class.getSimpleName();
private Camera mCamera;
private Camera.Parameters parameters;
private CameraManager camManager;
private Context context;

public CameraAndFlashProvider(Context context) {
this.context = context;
}

private void turnFlashlightOn() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
camManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
String cameraId = null;
if (camManager != null) {
cameraId = camManager.getCameraIdList()[0];
camManager.setTorchMode(cameraId, true);
}
} catch (CameraAccessException e) {
Log.e(TAG, e.toString());
}
} else {
mCamera = Camera.open();
parameters = mCamera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
}

private void turnFlashlightOff() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
String cameraId;
camManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
if (camManager != null) {
cameraId = camManager.getCameraIdList()[0]; // Usually front camera is at 0 position.
camManager.setTorchMode(cameraId, false);
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
} else {
mCamera = Camera.open();
parameters = mCamera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(parameters);
mCamera.stopPreview();
}
}
}

关于java - 我怎样才能在android代码中同时打开手电筒(闪光灯)和后置摄像头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60338485/

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