简体   繁体   中英

Need to know LED flash light code in Android

I already got some code from diff resources but all the codes are not working for LED flashlight in my mobile.I have LG Optimus. But If i download some app from play store it works very fine. My activity code is

Camera camera = null; Parameters parameters;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         final Button FlashLightControl = (Button)findViewById(R.id.flashcontrol);
            FlashLightControl.setText("Set FLASH_MODE_TORCH");
            FlashLightControl.setOnClickListener(new Button.OnClickListener(){
                @Override
        public void onClick(View arg0) {
        if(camera == null){
        camera = Camera.open();
                parameters = camera.getParameters();
                     parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        camera.setParameters(parameters);
        FlashLightControl.setText("Set FLASH_MODE_OFF");
                     }else{
               parameters = camera.getParameters();
                      parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                         camera.setParameters(parameters);
                         camera.release();
                         camera = null;
                         FlashLightControl.setText("Set FLASH_MODE_TORCH");
                        }
                }});

and Manifest:

<uses-feature android:name="android.hardware.camera" />

<!-- Camera Permissions -->   

<!-- Features -->
<uses-feature android:name="android.hardware.camera.flash" />
<uses-feature android:name="android.hardware.camera.autofocus" />
     <uses-permission android:name="android.permission.CAMERA"/>

this is the part of the java code on the site, call the getCamera in onCreate method and the rest through your button.

For toggle and playsound method check the site but they are mainly for graphics and UI purpose you can choose to ignore them on initial stage.

// getting camera parameters
 private void getCamera() {
if (camera == null) {
    try {
        camera = Camera.open();
        params = camera.getParameters();
    } catch (RuntimeException e) {
        Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
    }
}
}    

private void turnOnFlash() {
if (!isFlashOn) {
    if (camera == null || params == null) {
        return;
    }
    // play sound
    playSound();

    params = camera.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_TORCH);
    camera.setParameters(params);
    camera.startPreview();
    isFlashOn = true;

    // changing button/switch image
    toggleButtonImage();
} 
}
    /*
 * Turning Off flash
 */
private void turnOffFlash() {
if (isFlashOn) {
    if (camera == null || params == null) {
        return;
    }
    // play sound
    playSound();

    params = camera.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_OFF);
    camera.setParameters(params);
    camera.stopPreview();
    isFlashOn = false;

    // changing button/switch image
    toggleButtonImage();
}
}

and you can take the help from the link for any-other stuff on this http://www.androidhive.info/2013/04/android-developing-flashlight-application/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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