我无法打开Goog​​le Glass相机

时间:2015-04-27 14:21:28

标签: android android-camera google-glass google-gdk

我已获得此代码(基于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;
    }
}

此行返回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"手势......有什么建议吗?

我已阅读其他帖子,但我还没有找到任何解决方案:(

提前致谢!

已更新

空指针已经解决,但现在,应用程序返回了我的错误:"无法连接到相机服务"

1 个答案:

答案 0 :(得分:2)

问题:

由于您使用的是Android相机API(http://developer.android.com/training/camera/cameradirect.html)与特定于Google眼镜的相机API(https://developers.google.com/glass/develop/gdk/camera),因此您很可能需要重新启动Google Glass。

请参阅其他问题:java.lang.RuntimeException: Fail to Connect to camera service

我多次遇到过这个问题。基本上,当您尝试连接相机并且没有正确关闭相机服务时,您将无法再次连接到它。我使用Android API和OpenCV的API遇到了这个问题。

请在>重启设备后按照下面的示例进行操作。

实施例

<强> MainActivity:

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