您在哪里设置预览显示?
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