Android如何快速闪烁LED /手电筒

时间:2013-05-02 21:58:07

标签: android led flashlight

我想用led /手电筒给一些效果。但是它没有快速闪烁,即使我试着睡觉(2),但它需要时间才能眨眼。我想更快地眨眼。

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 个答案:

答案 0 :(得分:4)

您在哪里设置预览显示?

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

  

重要提示:将完全初始化的SurfaceHolder传递给   setPreviewDisplay(SurfaceHolder)。没有表面,相机会   无法开始预览。

cam.setPreviewDisplay(null);

也许你应该试试这个:

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();

需要的功能:

/** 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();
    }
}

和所需的实例变量:

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