我无法打开 Google 玻璃相机
I can't open the Google Glass Camera
我有此代码(基于 http://developer.android.com/training/camera/cameradirect.html),但在 Google Glass 上我不适用。
代码:
public class MainActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
private GestureDetector mGestureDetector;
private Camera.PictureCallback mPicture;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGestureDetector = createGestureDetector(this);
mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
// Log.d("Camera", "Error creating media file, check storage permissions: " + e.getMessage());
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d("Camera", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("Camera", "Error accessing file: " + e.getMessage());
}
}
};
// Create an instance of 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);
}
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
}
这一行returns nullPointerException:
preview.addView(mPreview);
getCameraInstance 方法:
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
System.out.println(e.getMessage());
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
我假装没有"TAP"手势拍照...有什么建议吗?
我已经阅读了其他帖子,但我还没有找到任何解决方案:(
提前致谢!
已更新
空指针已经解决,但是现在,应用returns我这个错误:"Fail to connect to camera service"
问题:
由于您使用的是 Android 相机 API(http://developer.android.com/training/camera/cameradirect.html) versus Google Glasses specific Camera APIs(https://developers.google.com/glass/develop/gdk/camera),您很可能需要重新启动 Google Glass。
查看其他 SO 问题:java.lang.RuntimeException: Fail to Connect to camera service
这个问题我已经运行多次了。基本上,当您尝试连接相机并且没有正确关闭相机服务时,您将无法再次连接回它。我 运行 使用 Android API 和 OpenCV 的 API。
请在重启设备后按照以下示例操作。
示例:
主要活动:
public class MainActivity extends Activity {
private CameraSurfaceView cameraView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initiate CameraView
cameraView = new CameraSurfaceView(this);
// Set the view
this.setContentView(cameraView);
}
@Override
protected void onResume() {
super.onResume();
// Do not hold the camera during onResume
if (cameraView != null) {
cameraView.releaseCamera();
}
}
@Override
protected void onPause() {
super.onPause();
// Do not hold the camera during onPause
if (cameraView != null) {
cameraView.releaseCamera();
}
}
}
CameraSurfaceView:
public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private Camera camera;
@SuppressWarnings("deprecation")
public CameraSurfaceView(Context context) {
super(context);
final SurfaceHolder surfaceHolder = this.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
// Show the Camera display
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
this.releaseCamera();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// Start the preview for surfaceChanged
if (camera != null) {
camera.startPreview();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// Do not hold the camera during surfaceDestroyed - view should be gone
this.releaseCamera();
}
/**
* Release the camera from use
*/
public void releaseCamera() {
if (camera != null) {
camera.release();
camera = null;
}
}
}
参考文献:
我知道这一点是因为我在一个很大的 Google Glass 示例存储库中工作,位于此处:https://github.com/jaredsburrows/OpenQuartz.
请在此处查看我的个人相机示例:https://github.com/jaredsburrows/OpenQuartz/tree/master/examples/CameraPreview。
其他问题:java.lang.RuntimeException: Fail to Connect to camera service
我有此代码(基于 http://developer.android.com/training/camera/cameradirect.html),但在 Google Glass 上我不适用。
代码:
public class MainActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
private GestureDetector mGestureDetector;
private Camera.PictureCallback mPicture;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGestureDetector = createGestureDetector(this);
mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
// Log.d("Camera", "Error creating media file, check storage permissions: " + e.getMessage());
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d("Camera", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("Camera", "Error accessing file: " + e.getMessage());
}
}
};
// Create an instance of 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);
}
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
}
这一行returns nullPointerException:
preview.addView(mPreview);
getCameraInstance 方法:
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
System.out.println(e.getMessage());
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
我假装没有"TAP"手势拍照...有什么建议吗?
我已经阅读了其他帖子,但我还没有找到任何解决方案:(
提前致谢!
已更新
空指针已经解决,但是现在,应用returns我这个错误:"Fail to connect to camera service"
问题:
由于您使用的是 Android 相机 API(http://developer.android.com/training/camera/cameradirect.html) versus Google Glasses specific Camera APIs(https://developers.google.com/glass/develop/gdk/camera),您很可能需要重新启动 Google Glass。
查看其他 SO 问题:java.lang.RuntimeException: Fail to Connect to camera service
这个问题我已经运行多次了。基本上,当您尝试连接相机并且没有正确关闭相机服务时,您将无法再次连接回它。我 运行 使用 Android API 和 OpenCV 的 API。
请在重启设备后按照以下示例操作。
示例:
主要活动:
public class MainActivity extends Activity {
private CameraSurfaceView cameraView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initiate CameraView
cameraView = new CameraSurfaceView(this);
// Set the view
this.setContentView(cameraView);
}
@Override
protected void onResume() {
super.onResume();
// Do not hold the camera during onResume
if (cameraView != null) {
cameraView.releaseCamera();
}
}
@Override
protected void onPause() {
super.onPause();
// Do not hold the camera during onPause
if (cameraView != null) {
cameraView.releaseCamera();
}
}
}
CameraSurfaceView:
public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private Camera camera;
@SuppressWarnings("deprecation")
public CameraSurfaceView(Context context) {
super(context);
final SurfaceHolder surfaceHolder = this.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
// Show the Camera display
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
this.releaseCamera();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// Start the preview for surfaceChanged
if (camera != null) {
camera.startPreview();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// Do not hold the camera during surfaceDestroyed - view should be gone
this.releaseCamera();
}
/**
* Release the camera from use
*/
public void releaseCamera() {
if (camera != null) {
camera.release();
camera = null;
}
}
}
参考文献:
我知道这一点是因为我在一个很大的 Google Glass 示例存储库中工作,位于此处:https://github.com/jaredsburrows/OpenQuartz.
请在此处查看我的个人相机示例:https://github.com/jaredsburrows/OpenQuartz/tree/master/examples/CameraPreview。
其他问题:java.lang.RuntimeException: Fail to Connect to camera service