android flash turn on/off using button while camera is running

醉酒当歌 提交于 2019-12-12 18:35:14

问题


I saw that there are many questions liek this but I didn't find any way. In my app I have my own Camera with some options , and also have button named btnFlash for turning on/off camera's flash light while camera is running. I tried many ways, but nothing worked as on Samsung tab and HTC. here is one of them

ImageButton btnFlash = (ImageButton) findViewById(R.id.btn_flash);
        btnFlash.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Context context = getApplicationContext();
                if( context .getPackageManager().hasSystemFeature(getPackageManager().FEATURE_CAMERA_FLASH)) {
                    Parameters params = mCamera.getParameters();
                    if(isFlashOn) {
                        params.setFlashMode(Parameters.FLASH_MODE_OFF);
                        mCamera.setParameters(params);
                        isFlashOn = false;
                    } else {
                        params.setFlashMode(Parameters.FLASH_MODE_ON);
                        mCamera.setParameters(params);
                        isFlashOn = true;
                    }
                }
            }
        });
  1. Can anyone suggest some good tutorial or some code?
  2. Can same code work on e.g. HTC but not work on samsung? I mean can I wrote some code for any type of android device?

Thanks in advance..


回答1:


Go through this link. This helps me to achieve flash functionality in almost all devices.

Import this SVN project.

http://code.google.com/p/torch/source/checkout




回答2:


I had the same problem with you and I think you should replace

    params.setFlashMode(Parameters.FLASH_MODE_ON);

with this

    params.setFlashMode(Parameters.FLASH_MODE_TORCH);

I tested the param

    Parameters.FLASH_MODE_ON
    Parameters.FLASH_MODE_RED_EYE

and had the same problem, I think the problem occur because the device does not have appropriate hardware parametes



来源:https://stackoverflow.com/questions/14442559/android-flash-turn-on-off-using-button-while-camera-is-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!