camera上flash的常亮

來源:互聯網
上載者:User
 

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.

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;

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! The bad news was that when later setting the
flash-mode back to auto or off left the LED still lit! It will not turn
off until you call 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...

/*** * 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.

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. It definitely
works on HTC EVO and Samsung Epic.

Hope this helps.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.