2

我正在尝试使用 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();
}
4

1 回答 1

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
于 2013-10-21T18:57:12.767 回答