我试图用 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();
}
最佳答案
在哪里设置预览显示?
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);
也许你应该尝试这个:
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
关于Android:如何快速闪烁 LED/手电筒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16348418/