How to use camera flash/led as torch on a Samsung Galaxy Tab?

故事扮演 提交于 2019-11-27 11:23:37
Error 454

You aren't doing anything wrong. In fact, you are doing everything correct. You are encountering a Device-Specific issue that is very prevalent in the Android world. I have found the following behavioral patterns for FLASH_MODE_TORCH:

  • Works fine in all cases
  • Works fine, but not with autofocus on
  • Doesn't work at all

Frustratingly, getSupportedFlashModes() will return FLASH_MODE_TORCH on nearly every device when only a handful actually support it.

Also, some device implementations swizzle the supported flash modes. If you go through Camera.Parameters you can try setting the flash mode to FLASH_MODE_ON, FLASH_MODE_AUTO or FLASH_MODE_RED_EYE and see whether any of them work. Note - this is a device-specific hack.

I have filed these types of bugs with Google regarding the DroidX and Nexus S. They closed it as a device-specific issue. I would say to report this to Samsung in hopes for a driver or firmware fix, but their Android support channels do not exist.

JM Macariola

Took me a while but I think you're missing a startPreview() there.

After you do your Camera.open() and after you set the parametrs, do a mCamera.startPreview(). That should do the trick.

This is how I made it work.

if (Build.MODEL.equals("GT-P1000")) {

            Log.d(FlashlightActivity.TAG, "This is Samsung Galaxy Tab.");

            params.setFlashMode(Parameters.FLASH_MODE_ON);
            camera.setParameters(params);
            camera.startPreview();
            camera.autoFocus(new AutoFocusCallback() {
                public void onAutoFocus(boolean success, Camera camera) {
                }
            });

            isLEDturnedOn = true;
            Log.d(FlashlightActivity.TAG, "LED turned ON.");

} 

This is how I turn on and of the torch in LG Nexus 4 and Samsung Galaxy Ace 2.

public void changeTorch() {
            try {
                camera = Camera.open();
                // try to open the camera to turn on the torch
                Camera.Parameters param = camera.getParameters();
                param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                camera.setParameters(param);
                camera.startPreview(); // needed for some devices
                Log.v("BSW torch", "Torch ON");
            } catch (Exception e) {
                // if open camera fails, try to release camera
                Log.w("BSW torch", "Camera is being used trying to turn Torch OFF");
                try {
                    camera.release();
                } catch (Exception ex) {
                    Log.e("BSF torch", "Error releasing camera");
                }
            }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!