Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 谷歌玻璃碰撞相机预览_Android_Android Camera_Google Glass - Fatal编程技术网

Android 谷歌玻璃碰撞相机预览

Android 谷歌玻璃碰撞相机预览,android,android-camera,google-glass,Android,Android Camera,Google Glass,所以我正在为谷歌眼镜编写一个应用程序,它使用相机来显示预览。按照我现在设置的方式,应用程序加载到主屏幕,用户从主屏幕导航到相机部分。预览显示,然后创建一个拍摄图片的意图,并对图片进行处理。这在70%的时间里有效;然而,偶尔,应用程序会进入黑屏而不是预览,此时玻璃会崩溃,需要硬重启 这是来自LogCat的消息: 06-18 09:04:05.717: I/CameraService(18740): CameraService::connect X (id 0, this pid is 18740)

所以我正在为谷歌眼镜编写一个应用程序,它使用相机来显示预览。按照我现在设置的方式,应用程序加载到主屏幕,用户从主屏幕导航到相机部分。预览显示,然后创建一个拍摄图片的意图,并对图片进行处理。这在70%的时间里有效;然而,偶尔,应用程序会进入黑屏而不是预览,此时玻璃会崩溃,需要硬重启

这是来自LogCat的消息:

06-18 09:04:05.717: I/CameraService(18740): CameraService::connect X (id 0, this pid is 18740)
06-18 09:04:05.717: I/CameraService-GoogleCamera(18740): Acquire hardware jpeg encoder lock took: 0 mS 
06-18 09:04:05.717: D/libgcam(18740): [gcam.cc:3305]: Gcam::Pause
06-18 09:04:05.741: D/debug(9063): ONRESUME
06-18 09:04:05.756: I/Choreographer(9063): Skipped 194 frames!  The application may be doing too much work on its main thread.
06-18 09:04:06.413: I/CameraHal(18740): (b79e90f0)   hardware/ti/omap4xxx/camera/CameraHalCommon.cpp:114 PPM - PPM: Standby to first shot: Sensor Change completed -  :161.11 ms :  1403096646415 ms
06-18 09:04:06.459: I/Choreographer(9063): Skipped 41 frames!  The application may be doing too much work on its main thread.
06-18 09:04:06.772: I/ActivityManager(19034): Displayed com.example.d31testing/.CameraActivity: +4s282ms
这是我在OnCreate中的代码的开头:

Log.d("debug", "ONCREATE");
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_camera);  
// determining display dimensions to place the reticle
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
Log.d("tag", "x:" + width + "y:" + height);
// Create an instance of Camera
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
// code to get camera
mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
CropDraw mCrop = new CropDraw(this, width, height);
// adding the reticle to the screen
preview.addView(mCrop);
Toast.makeText(
this,
"Press Camera Button to take a picture! \n Take a picture of a side view",    Toast.LENGTH_SHORT).show();

尝试以这样的安全方式捕获相机,以获取get camera实例:

public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open();
    } catch (Exception e){
    }
    return c;
}
然后,要获得预览,只需调用一个方法

public void getPreview()
{
 setContentView(R.layout.activity_quick_scan);
 autoFocusHandler = new Handler();

//For me at least, the camera had some trouble so I retried 3 times
 for(int i=0; i < 3; i++)
    {
        mCamera = getCameraInstance();
        if(mCamera != null) break;
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    if(mCamera == null)
    {
        Toast.makeText(this, "Camera cannot be locked", Toast.LENGTH_SHORT).show();
        finish();
    }


 FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
 mPreview = new CameraPreview(this, mCamera, null , autoFocusCB);
 preview.addView(mPreview);
}
作为参考,xml看起来有点像这样:

<FrameLayout
    android:id="@+id/cameraPreview"
    android:layout_width="480dp"
    android:layout_height="0dp"
    android:layout_weight="0.34" >
</FrameLayout>

你不应该睡在主线程上,绝对不应该睡3秒钟!您应该在背景线程中打开用getCameraInstance方法包装的相机。要获得最佳结果,请使用处理程序事件线程,请参阅。问题是,每当我在活动中调用Camera.open时,都会出现运行时异常。我知道打开摄像头的安全方法——我想知道如何告诉当前使用摄像头的任何其他进程让我访问该服务。