简体   繁体   English

使用不推荐使用的Camera API,频闪灯在某些设备上不起作用

[英]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. 我使用了不赞成使用的Camera API,因为Camera2涉及太多不必要的步骤,无法简单地启用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. 根据我在Google和SO上所做的研究,似乎有些设备要求您需要为手电筒提供工作表面。 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. 我还尝试过使用surfaceTexture来代替它,这也会降低手电筒的速度。 It looks like startPreview with a surface provided runs slower. 看起来带有表面的startPreview运行速度较慢。 Using a thread to do the same still slows down the process. 使用线程执行相同操作仍会减慢该过程。

This is how I'm currently handling flash changes: 这是我当前处理Flash更改的方式:

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. 您可以仅在输出SurfaceHolder准备就绪时开始预览,并使其保持运行状态,直到用户停止选通。

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. 正如您所发现的那样,开始/停止预览很慢,只要让它运行就可以,直到您完成选通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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