Google Glass Camera:setPreviewCallback

时间:2014-04-25 08:59:17

标签: callback null camera google-glass preview

我有一个操作Android应用程序,它利用预览回调,在预览的每一帧之后处理图像(预览在计算发生时暂停一分钟)。它很棒!但是,现在我正在努力让Google Glass做同样的事情:激活预览,激活预览回调,激活计算。

此问题可能与Camera的定义方式有关:     相机mCamera = null;

但是,我无法将其定义为Camera.open();使用谷歌眼镜,因为它失败了。因此,当我定义回调时,我被告知它可能会产生NullPointerException,实际上我得到以下错误字符串:

  

流程:com.ead.glasscam.app,PID:14231       java.lang.RuntimeException:无法启动活动ComponentInfo {com.ead.glasscam.app/com.ead.glasscam.app.MainActivity}:   显示java.lang.NullPointerException               在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2235)               在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2285)               在android.app.ActivityThread.access $ 800(ActivityThread.java:138)               在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1236)               在android.os.Handler.dispatchMessage(Handler.java:102)               在android.os.Looper.loop(Looper.java:149)               在android.app.ActivityThread.main(ActivityThread.java:5061)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:515)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:794)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)               at dalvik.system.NativeStart.main(Native Method)        引起:java.lang.NullPointerException               在com.ead.glasscam.app.MainActivity.onCreate(MainActivity.java:100)               在android.app.Activity.performCreate(Activity.java:5236)               在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1089)               在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)   在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2285)   在android.app.ActivityThread.access $ 800(ActivityThread.java:138)   在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1236)   在android.os.Handler.dispatchMessage(Handler.java:102)   在android.os.Looper.loop(Looper.java:149)   在android.app.ActivityThread.main(ActivityThread.java:5061)   at java.lang.reflect.Method.invokeNative(Native Method)   在java.lang.reflect.Method.invoke(Method.java:515)   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:794)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)   在dalvik.system.NativeStart.main(本地方法)

所以,代码:

// Attach a callback for preview
mPreviewCallback camCallback = new mPreviewCallback();
mCamera.setPreviewCallback(camCallback);

什么是mPreviewCallback?:

public class mPreviewCallback implements Camera.PreviewCallback {

    public void onPreviewFrame(byte[] data, final Camera camera){
        Log.i("CAMERA", "Triggered Preview Frame");
    }
}

我真的不明白如何使用Google Glass在Android上执行这个简单的过程。谢谢你的阅读。

1 个答案:

答案 0 :(得分:2)

从jaredsburrows查看此示例,它使用Glass上的相机预览,我在项目中使用过一次(It Works !;))

https://github.com/jaredsburrows/OpenQuartz/tree/master/example-apps/OG_CameraApp

public class CameraView extends SurfaceView implements SurfaceHolder.Callback
{
    private SurfaceHolder surfaceHolder = null;
    private Camera camera = null;

    @SuppressWarnings("deprecation")
    public CameraView(Context context) 
    {
        super(context);

        surfaceHolder = this.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    /*
     * (non-Javadoc)
     * @see android.view.SurfaceHolder.Callback#surfaceCreated(android.view.SurfaceHolder)
     */
    @Override
    public void surfaceCreated(SurfaceHolder holder) 
    {
        camera = Camera.open();

        // Set the Hotfix for Google Glass
        this.setCameraParameters(camera);

        // Show the Camera display
        try 
        {
            camera.setPreviewDisplay(holder);
        } 
        catch (Exception e) 
        {
            this.releaseCamera();
        }
    }

    /*
     * (non-Javadoc)
     * @see android.view.SurfaceHolder.Callback#surfaceChanged(android.view.SurfaceHolder, int, int, int)
     */
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
    {
        // Start the preview for surfaceChanged
        if (camera != null)
        {
            camera.startPreview();
        }
    }

    /*
     * (non-Javadoc)
     * @see android.view.SurfaceHolder.Callback#surfaceDestroyed(android.view.SurfaceHolder)
     */
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) 
    {
        // Do not hold the camera during surfaceDestroyed - view should be gone
        this.releaseCamera();
    }

    /**
     * Important HotFix for Google Glass (post-XE11) update
     * @param camera Object
     */
    public void setCameraParameters(Camera camera)
    {
        if (camera != null)
        {
            Parameters parameters = camera.getParameters();
            parameters.setPreviewFpsRange(30000, 30000);
            camera.setParameters(parameters);   
        }
    }

    /**
     * Release the camera from use
     */
    public void releaseCamera() 
    {
        if (camera != null) 
        {
            camera.release();
            camera = null;
        }
    }
}