应用程序挂在camera.release()上

发布于 2024-12-25 15:09:31 字数 2225 浏览 1 评论 0原文

我正在开发一个使用相机闪光灯的手电筒应用程序。它似乎工作正常,但有时调用camera.release()会导致挂起大约一分钟左右。我已经包含了下面的代码。我看过很多例子,但没有看到任何可能导致这种情况的事情。有什么想法吗?

    //latest
    public void setOn(boolean on, Context context) {    

    if (lock) {
        Log.i(TAG, "lock: true");
        return;
    }


    if (on) {           
        if (mCamera == null) {
            mCamera = Camera.open();
        }

        Parameters params = mCamera.getParameters();
        params.setFlashMode(MODE_TORCH);
        mCamera.setParameters(params);
        mCamera.startPreview();
    } else {
        if (mCamera != null) {              
            try {                   
                Parameters params = mCamera.getParameters();
                params.setFlashMode(MODE_OFF);
                mCamera.setParameters(params);                  
            } finally {
                new Thread(new Runnable() {
                    public void run() {
                        Log.i(TAG, "new Thread - start");
                        lock = true;
                        mCamera.setPreviewCallback(null);
                        mCamera.stopPreview();
                        mCamera.release();
                        mCamera = null;
                        lock = false;
                        Log.i(TAG, "new Thread - end");
                    }
                }).start();                                     
            }
        }
    }
}


//original
public void setOn(boolean on, Context context) {        
    Camera camera = mCamera;
    if (on) {           
        if (camera == null) {
            mCamera = camera = Camera.open();
        }

        Parameters params = camera.getParameters();
        params.setFlashMode(MODE_TORCH);
        camera.setParameters(params);
        camera.startPreview();
    } else {
        if (camera != null) {

            try {                   
                Parameters params = camera.getParameters();
                params.setFlashMode(MODE_OFF);
                camera.setParameters(params);                   
            } finally {
                camera.stopPreview();
                camera.release();
                mCamera = camera = null;
            }
        }
    }
}

I'm working on a flashlight app using the camera flash. It seems to work fine but on occasion calling camera.release() causes a hang for about a minute or so. I've included the code below. I've looked at a bunch of examples and I don't see anything that could cause such a thing. Any ideas?

    //latest
    public void setOn(boolean on, Context context) {    

    if (lock) {
        Log.i(TAG, "lock: true");
        return;
    }


    if (on) {           
        if (mCamera == null) {
            mCamera = Camera.open();
        }

        Parameters params = mCamera.getParameters();
        params.setFlashMode(MODE_TORCH);
        mCamera.setParameters(params);
        mCamera.startPreview();
    } else {
        if (mCamera != null) {              
            try {                   
                Parameters params = mCamera.getParameters();
                params.setFlashMode(MODE_OFF);
                mCamera.setParameters(params);                  
            } finally {
                new Thread(new Runnable() {
                    public void run() {
                        Log.i(TAG, "new Thread - start");
                        lock = true;
                        mCamera.setPreviewCallback(null);
                        mCamera.stopPreview();
                        mCamera.release();
                        mCamera = null;
                        lock = false;
                        Log.i(TAG, "new Thread - end");
                    }
                }).start();                                     
            }
        }
    }
}


//original
public void setOn(boolean on, Context context) {        
    Camera camera = mCamera;
    if (on) {           
        if (camera == null) {
            mCamera = camera = Camera.open();
        }

        Parameters params = camera.getParameters();
        params.setFlashMode(MODE_TORCH);
        camera.setParameters(params);
        camera.startPreview();
    } else {
        if (camera != null) {

            try {                   
                Parameters params = camera.getParameters();
                params.setFlashMode(MODE_OFF);
                camera.setParameters(params);                   
            } finally {
                camera.stopPreview();
                camera.release();
                mCamera = camera = null;
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你没皮卡萌 2025-01-01 15:09:31

尝试将其放入一个线程中以在后台运行,这样它就不会挂起 UI。

new Thread(new Runnable(){
    public void run(){
        camera.setPreviewCallback(null); // PreviewCallback de_init.
        camera.stopPreview(); // stop Preview
        camera.release();
    }
}).start();

Try putting it in a thread to run in the background so it wont hang up the UI.

new Thread(new Runnable(){
    public void run(){
        camera.setPreviewCallback(null); // PreviewCallback de_init.
        camera.stopPreview(); // stop Preview
        camera.release();
    }
}).start();
决绝 2025-01-01 15:09:31

对我来说,有效的解决方案是:

Try{
    camera.stopPreview();
    camera.setPreviewCallback(null);   
    camera.release();
    camera = null;
} catch (Exception e){
    //...
}

For me, the solution that works is:

Try{
    camera.stopPreview();
    camera.setPreviewCallback(null);   
    camera.release();
    camera = null;
} catch (Exception e){
    //...
}
断念 2025-01-01 15:09:31

我已经解决了这个问题,在

camera.stopPreview();
camera.setPreviewCallback(null);
camera.unlock();
camera.release();
camera = null;

需要更多设备的release()测试之前添加camera.unlock()...

I`ve solved this isuue adding camera.unlock() before release()

camera.stopPreview();
camera.setPreviewCallback(null);
camera.unlock();
camera.release();
camera = null;

test on more devices needed...

漆黑的白昼 2025-01-01 15:09:31

只需调用您的方法,例如

mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();

您必须在 stopPreview 和 camera.releass 之间调用 setPreviewCallback(null)

just call your methods like

mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();

you have to call setPreviewCallback(null) betweeen stopPreview and camera.releass

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文