简体   繁体   中英

Using the deprecated Camera API, strobe light not working on some devices

I've used the deprecated Camera API since Camera2 involves too many unnecessary steps to simply enable the Flashlight.

But on some devices it doesn't seem to be working.

From the research I've done on Google and SO, it seems that some devices require that you that you need to provide a surface for the flashlight to work. But this is drastically slowing down the strobe functionality within my app.

mCamera = android.hardware.Camera.open(0);
mCameraParameters = mCamera.getParameters();

mHolder = aSurfaceView.getHolder();
mHolder.addCallback(this);

@Override
public void surfaceCreated(SurfaceHolder holder) {
    mHolder = holder;
    setPreviewDisplay(mHolder);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    mHolder = null;
}

private void setPreviewDisplay(SurfaceHolder aHolder) {
    try {
        mCamera.setPreviewDisplay(aHolder);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I've also tried using a surfaceTexture instead which also slows down the flashlight. It looks like startPreview with a surface provided runs slower. Using a thread to do the same still slows down the process.

This is how I'm currently handling flash changes:

String mFlashMode;

if (on) {
    if (noTorch) mFlashMode = FLASH_ON;
    else mFlashMode = FLASH_TORCH;

} else mFlashMode = FLASH_OFF;

mCameraParameters.setFlashMode(mFlashMode);
mCamera.setParameters(mCameraParameters);

if (on) mCamera.startPreview();
else mCamera.stopPreview();

Please suggest a solution that will work on all devices. My ultimate goal is to achieve a strobe light, so therefore please note that the responsiveness of the flashlight is of most importance.

You can just start the preview when the output SurfaceHolder is ready, and leave it running until the user stops the strobe.

Then just modify the flash mode and update the parameters when you want to switch it on/off; starting/stopping preview is slow as you've discovered, just leave it running until you're done with strobing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM