如何在Android中拍照时使用夜景关闭相机的闪光灯?

时间:2017-10-27 05:57:50

标签: android android-camera flashlight camera-flash

问题:当我将场景模式设置为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-->

权限:

params.getFlashMode()

Logcat正确显示:I/mm-jpeg-intf: process_sensor_data: Flash value 1 flash mode 4 flash state 3:关闭,但闪光灯被触发 Logcat(Nexus 5)在调用mCamera.takePicture后显示params.setSceneMode(Camera.Parameters.SCENE_MODE_NIGHT);

当我评论时:Camera.Parameters.SCENE_MODE_AUTO(或更改为I/mm-jpeg-intf: process_sensor_data: Flash value 0 flash mode 0 flash state 2)闪光灯未被触发。
Logcat(Nexus 5)在调用mCamera.takePicture后显示SCENE_MODE_NIGHT

目标:同时使用以下参数:FLASH_MODE_OFFjava.io.FileNotFoundException: http://archives.jenkins-ci.org/plugins/git/3.6.3/git.hpi at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1872) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474) at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(HttpURLConnection.java:2982) at java.net.URLConnection.getHeaderFieldLong(URLConnection.java:629) at java.net.URLConnection.getContentLengthLong(URLConnection.java:501) at java.net.URLConnection.getContentLength(URLConnection.java:485) at hudson.model.UpdateCenter$UpdateCenterConfiguration.download(UpdateCenter.java:1126) Caused: java.io.FileNotFoundException: http://archives.jenkins-ci.org/plugins/git/3.6.3/git.hpi at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1926) at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1921) at java.security.AccessController.doPrivileged(Native Method) at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1920) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1490) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474) at hudson.model.UpdateCenter$UpdateCenterConfiguration.download(UpdateCenter.java:1139) Caused: java.io.IOException: Failed to load http://updates.jenkins-ci.org/download/plugins/git/3.6.3/git.hpi to /var/lib/jenkins/plugins/git.jpi.tmp at hudson.model.UpdateCenter$UpdateCenterConfiguration.download(UpdateCenter.java:1146) Caused: hudson.util.IOException2: Failed to download from http://updates.jenkins-ci.org/download/plugins/git/3.6.3/git.hpi (redirected to: http://archives.jenkins-ci.org/plugins/git/3.6.3/git.hpi) at hudson.model.UpdateCenter$UpdateCenterConfiguration.download(UpdateCenter.java:1172) at hudson.model.UpdateCenter$DownloadJob._run(UpdateCenter.java:1677) at hudson.model.UpdateCenter$InstallationJob._run(UpdateCenter.java:1874) at hudson.model.UpdateCenter$DownloadJob.run(UpdateCenter.java:1651) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at hudson.remoting.AtmostOneThreadExecutor$Worker.run(AtmostOneThreadExecutor.java:110) at java.lang.Thread.run(Thread.java:748)
使用:Android Camera 1 API。后置摄像头。
发生:Android 6,7,也许也是Android 5 的测试

  • Nexus 5:不行。
  • 华为荣耀4C:好的

可能是Nexus 5“bug”,因为我尝试从Google Play打开相机应用程序,将场景设置为夜晚,禁用闪光灯,但拍照时闪光灯仍处于活动状态。

1 个答案:

答案 0 :(得分:0)

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");
    }
}