Android How to blink led/flashlight rapidly

寵の児 提交于 2019-12-19 11:27:23

问题


I am trying to give some effects with led/flashlight. but it is not blinking rapidly, even i tried sleep(2), bt it takes time to blink. i want to blink it faster.

public void flash_effect() throws InterruptedException
{
    cam = Camera.open();     
    final Parameters p = cam.getParameters();
    p.setFlashMode(Parameters.FLASH_MODE_TORCH);


    Thread a = new Thread()
    {
        public void run()
        {
            for(int i =0; i < 10; i++)
            {
                cam.setParameters(p);
                cam.startPreview();
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                cam.stopPreview();
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }
    };
    a.start();
}

回答1:


Where do you set your preview display?

https://developer.android.com/reference/android/hardware/Camera.html

Important: Pass a fully initialized SurfaceHolder to setPreviewDisplay(SurfaceHolder). Without a surface, the camera will be unable to start the preview.

cam.setPreviewDisplay(null);

Maybe you should try this:

Thread t = new Thread() {
    public void run() {
        try {
            // Switch on the cam for app's life
            if (mCamera == null) {
                // Turn on Cam
                mCamera = Camera.open();
                try {
                    mCamera.setPreviewDisplay(null);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mCamera.startPreview();
            }

            for (int i=0; i < times*2; i++) {
                toggleFlashLight();
                sleep(delay);
            }

            if (mCamera != null) {
                mCamera.stopPreview();
                mCamera.release();
                mCamera = null;
            }
        } catch (Exception e){ 
            e.printStackTrace(); 
        }
    }
};

t.start();

Needed functions:

/** Turn the devices FlashLight on */
public void turnOn() {
    if (mCamera != null) {
    // Turn on LED
    mParams = mCamera.getParameters();
    mParams.setFlashMode(Parameters.FLASH_MODE_TORCH);
    mCamera.setParameters(mParams);

    on = true;
}
}

/** Turn the devices FlashLight off */
public void turnOff() {
    // Turn off flashlight
    if (mCamera != null) {
        mParams = mCamera.getParameters();
        if (mParams.getFlashMode().equals(Parameters.FLASH_MODE_TORCH)) {
            mParams.setFlashMode(Parameters.FLASH_MODE_OFF);
            mCamera.setParameters(mParams);
        }
    }
    on = false;
}

/** Toggle the flashlight on/off status */
public void toggleFlashLight() {
    if (!on) { // Off, turn it on
        turnOn();
    } else { // On, turn it off
        turnOff();
    }
}

and needed instance vars:

Camera mCamera;
Camera.Parameters mParameters;
int delay = 100; // in ms


来源:https://stackoverflow.com/questions/16348418/android-how-to-blink-led-flashlight-rapidly

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