我应该怎么做,使摄像头的工作每次? [英] What should i do so that camera work everytime?

查看:136
本文介绍了我应该怎么做,使摄像头的工作每次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用相机的服务。有时相机服务正在运行在应用精细,有时它给出了一个运行时异常。

我在try块把Camera.Open()和我擦肩而过,例外和显示日志猫

  13 03-12:52:42.211:D /疯狂(12686):在catch1
03-12 13:52:42.211:D /疯狂(12686):了java.lang.RuntimeException:无法连接到相机服务

在code,我做的是...

  TelephonyManager经理=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
            INT callState = mgr.getCallState();            //状态= intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            如果(callState == TelephonyManager.CALL_STATE_RINGING){
            尝试{
                凸轮= Camera.open();
                p值= cam.getParameters();                字符串的myString =0101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101011;
                长blinkDelay = 50;
                的for(int i = 0; I< myString.length();我++){
                    //state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
                    callState = mgr.getCallState();
                    如果(callState == TelephonyManager.CALL_STATE_IDLE){
                        p.setFlashMode(Parameters.FLASH_MODE_OFF);
        cam.release();
                        打破;                        }否则如果(callState == TelephonyManager.CALL_STATE_OFFHOOK){
p.setFlashMode(Parameters.FLASH_MODE_OFF);
        cam.release();
                        打破;
                        }                    如果(myString.charAt(ⅰ)=='0'){
                        p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                        cam.setParameters(P);
                    }其他{
                        p.setFlashMode(Parameters.FLASH_MODE_OFF);
                        cam.setParameters(P);
                    }                        视频下载(blinkDelay);                }
            }赶上(例外五){
                // TODO:处理异常
                Log.d(标签,在catch1);
                Log.d(标签,e.toString());        }


解决方案

这可能是因为它已被使用。

有关打开状态:


  

如果同样的相机由其他应用程序打开时,这会抛出RuntimeException。


  
  

您必须调用发布()当您使用相机完成,否则将保持锁定,无法使用其他应用程序。


  
  

您的应用程序应该只有一个Camera对象在同一时间为特定的硬件相机活跃。


请确保您始终释放相机(甚至在异常情况下,使用最后),并检查是否有使用它没有其他应用程序。

I am using camera service in my application. Sometimes the camera service is running fine in the application and sometimes it gives a runtime exception.

I have put Camera.Open() in try block and i have catched the exception and its showing in log cat

03-12 13:52:42.211: D/crazy(12686): in catch1
03-12 13:52:42.211: D/crazy(12686): java.lang.RuntimeException: Fail to connect to camera service

The code that i done is...

    TelephonyManager mgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            int callState = mgr.getCallState();

            //state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            if(callState==TelephonyManager.CALL_STATE_RINGING) {
            try {


                cam = Camera.open();
                p = cam.getParameters();

                String myString = "0101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101011";
                long blinkDelay = 50;


                for (int i = 0; i < myString.length(); i++) {
                    //state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
                    callState = mgr.getCallState();
                    if (callState==TelephonyManager.CALL_STATE_IDLE){
                        p.setFlashMode(Parameters.FLASH_MODE_OFF);
        cam.release();
                        break;                  

                        }else if (callState==TelephonyManager.CALL_STATE_OFFHOOK){
p.setFlashMode(Parameters.FLASH_MODE_OFF);
        cam.release();
                        break;  
                        }               

                    if (myString.charAt(i) == '0') {
                        p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                        cam.setParameters(p);
                    } else {
                        p.setFlashMode(Parameters.FLASH_MODE_OFF);
                        cam.setParameters(p);
                    }

                        Thread.sleep(blinkDelay);

                }
            }catch (Exception e) {
                // TODO: handle exception
                Log.d(tag, "in catch1");
                Log.d(tag, e.toString());

        }

解决方案

It's probably because it is already used.

The javadoc for open states :

If the same camera is opened by other applications, this will throw a RuntimeException.

You must call release() when you are done using the camera, otherwise it will remain locked and be unavailable to other applications.

Your application should only have one Camera object active at a time for a particular hardware camera.

Make sure you always release the camera (even in case of exception, use finally) and check if there is no other application using it.

这篇关于我应该怎么做,使摄像头的工作每次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆