JPCT的应用现在貌似还不多,用纯java语言在android平台上的3D实现目前还需要功能更强大的硬件支持,不过JPCT论坛的这个帖子很值得学习,就此翻译一下,译文如下:
所有的代码,这里提供的是免费使用,你使用它,我不负责。
我收到了一个问题,我发现很有意思,与大家分享。
关于这个主题的详细信息有极少部分是介绍如何在android上设置正确的工作布局。
因此,在这个主题,我会回答简单的问题:如何使用Android摄像头和JPCT- AE作为渲染覆盖摄像头。
(扩展现实的概念)必须具备已有的引擎,并且相关代码功能齐备。
首先,我们需要建立一个XML布局。
我们的最低要求是glSurfaceView,我们将用来绘制3D(JPCT引擎),及SurfaceView来绘制相机预览。<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <android.opengl.GLSurfaceView android:id="@+id/glsurfaceview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <SurfaceView android:id="@+id/surface_camera" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerInParent="true" android:keepScreenOn="true" /></FrameLayout>
这是初始化窗口和glSurfaceView。
// It talks from itself, please refer to android developer documentation. getWindow().setFormat(PixelFormat.TRANSLUCENT); // Fullscreen is not necessary... it's up to you. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.THE_XML_LAYOUT_CREATED_BEFORE); // attach our glSurfaceView to the one in the XML file. glSurfaceView = (GLSurfaceView) findViewById(R.id.glsurfaceview);
现在让我们创建摄像头和引擎,下面是一段例子的代码。
下面的代码是很容易理解的,
我要创建一个摄像头(Camera)并且给glSurfaceView做一个渲染,当然这之前要给它设置半透明窗体(8888)的像素格式和深度缓冲,如果没设置glSurfaceView会不支持alpha通道,就看不到摄像头层。
因此,基本上:
1)创建相机视图。2)设置的glSurfaceView。3)设置glSurfaceView渲染。4)为glSurfaceView持有者设置正确的像素格式。
try{ cameraView = new CameraView(this.getApplicationContext(), (SurfaceView) findViewById(R.id.surface_camera), imageCaptureCallback); } catch(Exception e){ e.printStackTrace(); } // Translucent window 8888 pixel format and depth buffer glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0); // GLEngine is a class I design to interact with JPCT and with all the basic function needed, // create a world, render it, OnDrawFrame event etc. glEngine = new GLEngine(getResources()); glSurfaceView.setRenderer(glEngine); game = new Game(glEngine, (ImageView) findViewById(R.id.animation_screen), getResources(), this .getBaseContext()); // Use a surface format with an Alpha channel: glSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT); // Start game game.start();
package com.dlcideas.ARescue.Camera;import java.io.IOException;import com.threed.jpct.Logger;import android.content.Context;import android.hardware.Camera;import android.view.SurfaceHolder;import android.view.SurfaceView;public class CameraView extends SurfaceView implements SurfaceHolder.Callback { /** * Create the cameraView and * * @param context * @param surfaceView */ public CameraView(Context context, SurfaceView surfaceView, ImageCaptureCallback imageCaptureCallback) { super(context); // Install a SurfaceHolder.Callback so we get notified when the // underlying surface is created and destroyed. previewHolder = surfaceView.getHolder(); previewHolder.addCallback(this); previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); //previewHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL); // Hold the reference of the caputreCallback (null yet, will be changed // on SurfaceChanged). this.imageCaptureCallback = imageCaptureCallback; } /** * Initialize the hardware camera. holder The holder */ public void surfaceCreated(SurfaceHolder holder) { camera = Camera.open(); try { camera.setPreviewDisplay(holder); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * */ public void surfaceDestroyed(SurfaceHolder holder) { this.onStop(); } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if (previewRunning) camera.stopPreview(); Camera.Parameters p = camera.getParameters(); p.setPreviewSize(width, height); // camera.setParameters(p); try { camera.setPreviewDisplay(holder); } catch (IOException e) { e.printStackTrace(); } previewRunning = true; Logger.log("camera callback huhihihihih", Logger.MESSAGE); camera.startPreview(); imageCaptureCallback = new ImageCaptureCallback(camera, width, height); //camera.startPreview(); } public void onStop() { // Surface will be destroyed when we return, so stop the preview. // Because the CameraDevice object is not a shared resource, it's very // important to release it when the activity is paused. imageCaptureCallback.stopImageProcessing(); camera.setPreviewCallback(null); camera.stopPreview(); previewRunning = false; camera.release(); } public void onResume() { camera = Camera.open(); camera.setPreviewCallback(imageCaptureCallback); previewRunning = true; } private Camera camera; private SurfaceHolder previewHolder; private boolean previewRunning; private ImageCaptureCallback imageCaptureCallback;}