简体   繁体   English

需要知道Android中的LED闪光灯代码

[英]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. 我已经从diff资源中获得了一些代码,但是所有代码都无法在我的手机上使用LED手电筒。我有LG Optimus。 But If i download some app from play store it works very fine. 但是,如果我从Play商店下载一些应用程序,则效果很好。 My activity code is 我的活动代码是

Camera camera = null; 摄影机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. 这是该站点上Java代码的一部分,请在onCreate方法中调用getCamera,然后通过您的按钮调用其余部分。

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. 对于切换和播放方法,请检查站点,但是它们主要用于图形和UI,您可以选择在初始阶段忽略它们。

// 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/ 而且您可以从http://www.androidhive.info/2013/04/android-developing-flashlight-application/上的任何其他内容的链接获取帮助

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

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