因此,我正在为Google Glass编写一个使用相机显示预览的应用。我现在设置它的方式,应用加载到用户导航到相机部分的主屏幕。预览显示然后创建一个拍摄照片并处理照片的意图。这占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();
答案 0 :(得分:0)
尝试以安全的方式捕捉相机,以获取相机实例:
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>