简体   繁体   English

Android 2.1的Camera.Parameters.FLASH_MODE_TORCH替代品

[英]Camera.Parameters.FLASH_MODE_TORCH replacement for Android 2.1

I am trying to write an app that requires the LED flash to go into torch mode. 我正在尝试编写一个需要LED闪光灯进入火炬模式的应用程序。 The problem is, Android 2.1 does not support this mode and therefore I cannot support the platform yet. 问题是,Android 2.1不支持此模式,因此我无法支持该平台。 Wouldn't be an issue, but I am writing it for my fiance and her Epic 4G only has 2.1 right now. 这不是问题,但是我正在为我的未婚夫写它,她的Epic 4G现在只有2.1。 I found some code samples that use some undocumented API calls and therefore work on the Motorola Droid and such, but they do not work on the Epic. 我找到了一些使用一些未记录的API调用的代码示例,因此可以在Motorola Droid等上工作,但它们不适用于Epic。 Does anyone have some suggestions on where to look to find code that should help me get this working? 有没有人有什么建议可以帮助我找到合适的代码?

I'm finding that torch mode is generally working fine on 2.1 but I had the same problem with the Samsung Epic and found a hack around it. 我发现火炬模式通常在2.1上正常工作但我遇到了与三星史诗相同的问题,并发现了它周围的黑客。

Looking at the params returned by Camera.getParameters() when run on the Samsung Epic, I noticed that the flash-modes it claims to support are: flash-mode-values=off,on,auto; 看看在三星Epic上运行时Camera.getParameters()返回的参数,我注意到它声称支持的闪存模式是:flash-mode-values = off,on,auto;

torch-mode is not listed, implying it's not supported. 火炬模式未列出,暗示它不受支持。

However, I found that this model would still accept that mode and WOULD turn the LED on! 但是,我发现这个型号仍然会接受这种模式,并且会打开LED! The bad news was that when later setting the flash-mode back to auto or off left the LED still lit! 坏消息是,当稍后将闪光模式设置回自动或关闭时,LED仍然亮起! It will not turn off until you call Camera.release(). 在调用Camera.release()之前,它不会关闭。

I guess that's why Samsung dont include it in the list of supported!?! 我猜这就是为什么三星不把它包含在支持的列表中!?!

So...the method I use to toggle torch in a CameraHelper class is... 所以...我用来在CameraHelper类中切换火炬的方法是......

/***
 * Attempts to set camera flash torch/flashlight mode on/off
 * @param isOn true = on, false = off
 * @return boolean whether or not we were able to set it
 */
public boolean setFlashlight(boolean isOn)
{
    if (mCamera == null)
    {
        return false;
    }
    Camera.Parameters params = mCamera.getParameters();
    String value;
    if (isOn) // we are being ask to turn it on
    {
        value = Camera.Parameters.FLASH_MODE_TORCH;
    }
    else  // we are being asked to turn it off
    {
        value =  Camera.Parameters.FLASH_MODE_AUTO;
    }

    try{    
        params.setFlashMode(value);
        mCamera.setParameters(params);

        String nowMode = mCamera.getParameters().getFlashMode();

        if (isOn && nowMode.equals(Camera.Parameters.FLASH_MODE_TORCH))
        {
            return true;
        }
        if (! isOn && nowMode.equals(Camera.Parameters.FLASH_MODE_AUTO))
        {
            return true;
        }
        return false;
    }
    catch (Exception ex)
    {
        MyLog.e(mLOG_TAG, this.getClass().getSimpleName() +  " error setting flash mode to: "+ value + " " + ex.toString());
    }
}

The activities that use this call it as follows... 使用此活动的活动如下所示...

private void toggleFlashLight()
{
    mIsFlashlightOn = ! mIsFlashlightOn;

    /**
     * hack to fix an issue where the Samsung Galaxy will turn torch on,
     * even though it says it doesnt support torch mode,
     * but then will NOT turn it off via this param.
     */
    if (! mIsFlashlightOn && Build.MANUFACTURER.equalsIgnoreCase("Samsung"))
    {
        this.releaseCameraResources();
        this.initCamera();
    }
    else
    {
        boolean result = mCamHelper.setFlashlight(mIsFlashlightOn);
        if (! result)
        {
            alertFlashlightNotSupported();
        }
    }
}

The magic that makes this work in releaseCameraResources() is that it calls Camera.release()....and then I have to reinitialize all my camera stuff for Samsung devices. 在releaseCameraResources()中使这个工作的神奇之处在于它调用了Camera.release()....然后我必须重新初始化我的三星设备的所有相机内容。

Not pretty but seems to be working for plenty of users. 不漂亮,但似乎适用于大量用户。

Note that I do have a report of torch mode not working at all with this code on Nexus one but have been able to dig into it. 请注意,我确实有一个关于火炬模式的报告在Nexus one上完全无法使用此代码,但已经能够深入研究它。 It definitely works on HTC EVO and Samsung Epic. 它肯定适用于HTC EVO和三星Epic。

Hope this helps. 希望这可以帮助。

In my case for Samsung devices I needed to set focus mode to infinity and it started to work 在我的三星设备的情况下,我需要将焦点模式设置为无穷大,它开始工作

params.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
mCamera.setParameters(params);
mCamera.startPreview();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM