1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| ublic class MainActivity extends AppCompatActivity{
GLSurfaceView glView;
CameraView cameraView;
GLClearRenderer clearRenderer;
@Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
// When working with the camera, it's useful to stick to one orientation.
setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE );
// Next, we disable the application's title bar...
requestWindowFeature( Window.FEATURE_NO_TITLE );
// ...and the notification bar. That way, we can use the full screen.
getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN );
// Now let's create an OpenGL surface.
glView = new GLSurfaceView( this );
// To see the camera preview, the OpenGL surface has to be created translucently.
// See link above.
glView.setEGLConfigChooser( 8, 8, 8, 8, 16, 0 );
glView.getHolder().setFormat( PixelFormat.TRANSLUCENT );
// The renderer will be implemented in a separate class, GLView, which I'll show next.
clearRenderer = new GLClearRenderer();
glView.setRenderer( clearRenderer );
// Now set this as the main view.
setContentView( glView );
// Now also create a view which contains the camera preview...
cameraView = new CameraView( this );
// ...and add it, wrapping the full screen size.
addContentView( cameraView, new ActionBar.LayoutParams( ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT ) );
}
public class GLClearRenderer implements GLSurfaceView.Renderer {
private BackgroundTexture mBackgroundTexture;
private SquareTexture mSquareTexture;
private Square mSquare;
// mMVPMatrix is an abbreviation for "Model View Projection Matrix"
private final float[] mMVPMatrix = new float[16];
private final float[] mProjectionMatrix = new float[16];
private final float[] mViewMatrix = new float[16];
private final float[] mRotationMatrix = new float[16];
private final float[] mRotationMatrixlX = new float[16];
private final float[] mRotationMatrixlY = new float[16];
private final float[] mRotationMatrixX = new float[16];
private final float[] mRotationMatrixY = new float[16];
private final float[] mRotationMatrixZ = new float[16];
private final float[] mRotationMatrix0 = new float[16];
float angleX ,angleY ,angleZ ;
private float mAngleX;
private float mAngleY;
public void onDrawFrame( GL10 gl ) {
// This method is called per frame, as the name suggests.
// For demonstration purposes, I simply clear the screen with a random translucent gray.
float c = 1.0f / 256 * ( System.currentTimeMillis() % 256 );
GLES20.glClearColor( c, c, c, 0.5f );
GLES20.glClear( GLES20.GL_COLOR_BUFFER_BIT );
// Draw background color
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
// Set the camera position (View matrix)
Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -10, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
// Calculate the projection and view transformation
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
// Draw square
mSquare.draw(mMVPMatrix);
}
public void onSurfaceChanged( GL10 gl, int width, int height ) {
// This is called whenever the dimensions of the surface have changed.
// We need to adapt this change for the GL viewport.
GLES20.glViewport( 0, 0, width, height );
float ratio = (float) width / height;
// this projection matrix is applied to object coordinates
// in the onDrawFrame() method
Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 3, 50);
}
public void onSurfaceCreated( GL10 gl, EGLConfig config ) {
// No need to do anything here.
angleX = 0;
angleY = 0;
angleZ = 0;
// Set the background frame color
//GLES20.glClearColor(1.0f, 0.1f, 0.0f, 1.0f);
mSquare = new Square(); %%%%%%% NOT DISPLAY
}
}
public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
private Camera camera;
public CameraView( Context context ) {
super( context );
// We're implementing the Callback interface and want to get notified
// about certain surface events.
getHolder().addCallback( this );
// We're changing the surface to a PUSH surface, meaning we're receiving
// all buffer data from another component - the camera, in this case.
getHolder().setType( SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS );
}
public void surfaceCreated( SurfaceHolder holder ) {
// Once the surface is created, simply open a handle to the camera hardware.
camera = Camera.open();
}
public void surfaceChanged( SurfaceHolder holder, int format, int width, int height ) {
// This method is called when the surface changes, e.g. when it's size is set.
// We use the opportunity to initialize the camera preview display dimensions.
Camera.Parameters p = camera.getParameters();
p.setPreviewSize( width, height );
//camera.setParameters( p );
// We also assign the preview display to this surface...
try {
camera.setPreviewDisplay( holder );
} catch( IOException e ) {
e.printStackTrace();
}
// ...and start previewing. From now on, the camera keeps pushing preview
// images to the surface.
camera.startPreview();
}
public void surfaceDestroyed( SurfaceHolder holder ) {
// Once the surface gets destroyed, we stop the preview mode and release
// the whole camera since we no longer need it.
camera.stopPreview();
camera.release();
camera = null;
}
}
} |
Partager