【问题标题】:Android: how to blink LED/flashlight rapidlyAndroid:如何快速闪烁 LED/手电筒
【发布时间】:2013-04-27 05:42:20
【问题描述】:

我正在尝试使用 LED/手电筒显示一些效果,但它没有快速闪烁。我什至试过sleep(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();
}

【问题讨论】:

    标签: android led flashlight


    【解决方案1】:

    您在哪里设置预览显示?

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

    重要提示:将完全初始化的 SurfaceHolder 传递给 设置预览显示(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
    

    【讨论】:

    • 我自己项目的代码在这里:NotifFlashlight
    • 这在我的带有 Lollipop 的 Nexus 5 上不起作用。然而,它确实适用于带有 KitKat 的 Sony Z3。需要一个真正的 PreviewDisplay
    • 这就是 Android 文档所说的(以及我所引用的),但是由于免费实现,这在不同的设备上会有所不同。相机的新包装中还需要一个 Surface,可以从中读取描述here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多