在安卓系统中拍照时,如何使用夜景关闭相机的闪光灯



问题:当我将场景模式设置为SCENE_MODE_NIGHT时,当我调用takePicture()方法时,闪光灯无法设置为关闭。

伪:

Camera mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK)
Camera.Parameters params = camera.getParameters();
//AUTO-FOCUS
List<String> focus = params.getSupportedFocusModes();
if (focus != null && focus.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
//AUTO NIGHT SCENE
List<String> scene = params.getSupportedSceneModes();
if (scene != null && scene.contains(Camera.Parameters.SCENE_MODE_NIGHT)){
params.setSceneMode(Camera.Parameters.SCENE_MODE_NIGHT);
}
//FLASH
List<String> flash = params.getSupportedFlashModes();
if(flash != null && flash.contains(Camera.Parameters.FLASH_MODE_OFF)) {
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
}
mCamera.setParameters(params);
mPreview = new CameraPreview(this, mCamera, params); //surfaceView (CameraPreview extends SurfaceView implements SurfaceHolder.Callback)

呼叫拍照:

if(params.isVideoSnapshotSupported()){//Some device not supported this mode
mCamera.takePicture(null, null, new Camera.PictureCallback(){
...
}

权限:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" /> <!--somewhere i find this strange permission, but still not working-->

Logcat显示正确:params.getFlashMode():关闭,但闪烁已启动
Logcat(Nexus 5)在调用mCamera.takePicture后显示:I/mm-jpeg-intf: process_sensor_data: Flash value 1 flash mode 4 flash state 3

当i注释行时:params.setSceneMode(Camera.Parameters.SCENE_MODE_NIGHT);(或更改为Camera.Parameters.SCENE_MODE_AUTO)闪光灯未启动
Logcat(Nexus 5)在调用mCamera.takePicture后显示:I/mm-jpeg-intf: process_sensor_data: Flash value 0 flash mode 0 flash state 2

目标:同时使用这些参数:SCENE_MODE_NIGHTFLASH_MODE_OFF
使用:Android相机1 API。后置摄像头
Occurrence:Android6,7,也许还有Android5
测试

  • 天枢5:不好
  • 华为荣耀4C:可以

可能是Nexus 5"bug">,因为我尝试了Google Play的Open Camera应用程序,将场景设置为夜间,禁用闪光灯,但在拍照时闪光灯仍然处于活动状态。

use the below code on onClick of flash on and flash off Button
/**
* Action to be performed when image is capture is clicked
*/
private OnClickListener OnFlashClick = new OnClickListener()
{
@Override
public void onClick(View v)
{
if(btn_cameraFlashOn.isSelected())
{
btn_cameraFlashOn.setSelected(false);
mCamUtils.setFlashParams(Parameters.FLASH_MODE_OFF);
}
else
{
btn_cameraFlashOn.setSelected(true);
mCamUtils.setFlashParams(Parameters.FLASH_MODE_TORCH);
}

}
};
private OnClickListener OffFlashClick = new OnClickListener()
{
@Override
public void onClick(View v)
{
mCamUtils.toggleFlashSettings();
}
};
private String mFlashSetting = null;
/**
* Set the flash mode for camera
*/
public void toggleFlashSettings()
{

if (null == mCamera)
{
return;
}
Camera.Parameters params = mCamera.getParameters();
List<String> flashModes = params.getSupportedFlashModes();
if (flashModes == null)
{
mCallback.enableFlashButton(false);
return;
}
else
{
mCallback.enableFlashButton(true);
}
//      Log.d(LOGTAG, " mFlashSetting mode = " + mFlashSetting);
if ((Camera.Parameters.FLASH_MODE_AUTO.equals(mFlashSetting))
&& (flashModes.contains(Camera.Parameters.FLASH_MODE_ON)))
{
setFlashParams(Camera.Parameters.FLASH_MODE_ON);
} else if ((Camera.Parameters.FLASH_MODE_ON.equals(mFlashSetting))
&& (flashModes.contains(Camera.Parameters.FLASH_MODE_OFF)))
{
setFlashParams(Camera.Parameters.FLASH_MODE_OFF);
}
else if (flashModes.contains(Camera.Parameters.FLASH_MODE_AUTO))
{
setFlashParams(Camera.Parameters.FLASH_MODE_AUTO);
}
}
/**
* Returns the current flash setting of the device
*
* @return
*/
public String getFlashMode()
{
if (null == mCamera)
{
return null;
}
mFlashSetting = mCamera.getParameters().getFlashMode();
return mFlashSetting;
}
/**
* Sets the flash mode of the camera. this is a internal method only
*
* @return
*/
public void setFlashParams(String flash)
{
if (null == mCamera)
{
return;
}
Camera.Parameters params = mCamera.getParameters();
List<String> flashModes = params.getSupportedFlashModes();
if (flashModes == null)
{
mCallback.enableFlashButton(false);
return;
}
else
{
mCallback.enableFlashButton(true);
}
if (flashModes.contains(flash))
{
mFlashSetting = flash;
params.setFlashMode(flash);
mCallback.flashSet(flash);
mCamera.setParameters(params);
//      Log.d(LOGTAG, " new flash mode = " + flash);
}
else
{
//      Log.e(LOGTAG, " INVALID FLASH MODE");
}
}

相关内容

  • 没有找到相关文章

最新更新