61 | * The regex is suitable for use by Matcher.matches(), which matches the entire string, so 62 | * we don't specify leading '^' or trailing '$'. 63 | */ 64 | private static String globToRegex(String glob) { 65 | // Quick, overly-simplistic implementation -- just want to handle something simple 66 | // like "*.mp4". 67 | // 68 | // See e.g. http://stackoverflow.com/questions/1247772/ for a more thorough treatment. 69 | StringBuilder regex = new StringBuilder(glob.length()); 70 | //regex.append('^'); 71 | for (char ch : glob.toCharArray()) { 72 | switch (ch) { 73 | case '*': 74 | regex.append(".*"); 75 | break; 76 | case '?': 77 | regex.append('.'); 78 | break; 79 | case '.': 80 | regex.append("\\."); 81 | break; 82 | default: 83 | regex.append(ch); 84 | break; 85 | } 86 | } 87 | //regex.append('$'); 88 | return regex.toString(); 89 | } 90 | 91 | /** 92 | * Obtains the approximate refresh time, in nanoseconds, of the default display associated 93 | * with the activity. 94 | *
95 | * The actual refresh rate can vary slightly (e.g. 58-62fps on a 60fps device). 96 | */ 97 | public static long getDisplayRefreshNsec(Activity activity) { 98 | Display display = ((WindowManager) 99 | activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 100 | double displayFps = display.getRefreshRate(); 101 | long refreshNs = Math.round(1000000000L / displayFps); 102 | Log.d(TAG, "refresh rate is " + displayFps + " fps --> " + refreshNs + " ns"); 103 | return refreshNs; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /zero-camera/src/main/java/com/cry/zero_camera/ref/gles/FlatShadedProgram.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.cry.zero_camera.ref.gles; 18 | 19 | import android.opengl.GLES20; 20 | import android.util.Log; 21 | 22 | import java.nio.FloatBuffer; 23 | 24 | /** 25 | * GL program and supporting functions for flat-shaded rendering. 26 | */ 27 | public class FlatShadedProgram { 28 | private static final String TAG = GlUtil.TAG; 29 | 30 | private static final String VERTEX_SHADER = 31 | "uniform mat4 uMVPMatrix;" + 32 | "attribute vec4 aPosition;" + 33 | "void main() {" + 34 | " gl_Position = uMVPMatrix * aPosition;" + 35 | "}"; 36 | 37 | private static final String FRAGMENT_SHADER = 38 | "precision mediump float;" + 39 | "uniform vec4 uColor;" + 40 | "void main() {" + 41 | " gl_FragColor = uColor;" + 42 | "}"; 43 | 44 | // Handles to the GL program and various components of it. 45 | private int mProgramHandle = -1; 46 | private int muColorLoc = -1; 47 | private int muMVPMatrixLoc = -1; 48 | private int maPositionLoc = -1; 49 | 50 | 51 | /** 52 | * Prepares the program in the current EGL context. 53 | */ 54 | public FlatShadedProgram() { 55 | mProgramHandle = GlUtil.createProgram(VERTEX_SHADER, FRAGMENT_SHADER); 56 | if (mProgramHandle == 0) { 57 | throw new RuntimeException("Unable to create program"); 58 | } 59 | Log.d(TAG, "Created program " + mProgramHandle); 60 | 61 | // get locations of attributes and uniforms 62 | 63 | maPositionLoc = GLES20.glGetAttribLocation(mProgramHandle, "aPosition"); 64 | GlUtil.checkLocation(maPositionLoc, "aPosition"); 65 | muMVPMatrixLoc = GLES20.glGetUniformLocation(mProgramHandle, "uMVPMatrix"); 66 | GlUtil.checkLocation(muMVPMatrixLoc, "uMVPMatrix"); 67 | muColorLoc = GLES20.glGetUniformLocation(mProgramHandle, "uColor"); 68 | GlUtil.checkLocation(muColorLoc, "uColor"); 69 | } 70 | 71 | /** 72 | * Releases the program. 73 | */ 74 | public void release() { 75 | GLES20.glDeleteProgram(mProgramHandle); 76 | mProgramHandle = -1; 77 | } 78 | 79 | /** 80 | * Issues the draw call. Does the full setup on every call. 81 | * 82 | * @param mvpMatrix The 4x4 projection matrix. 83 | * @param color A 4-element color vector. 84 | * @param vertexBuffer Buffer with vertex data. 85 | * @param firstVertex Index of first vertex to use in vertexBuffer. 86 | * @param vertexCount Number of vertices in vertexBuffer. 87 | * @param coordsPerVertex The number of coordinates per vertex (e.g. x,y is 2). 88 | * @param vertexStride Width, in bytes, of the data for each vertex (often vertexCount * 89 | * sizeof(float)). 90 | */ 91 | public void draw(float[] mvpMatrix, float[] color, FloatBuffer vertexBuffer, 92 | int firstVertex, int vertexCount, int coordsPerVertex, int vertexStride) { 93 | GlUtil.checkGlError("draw start"); 94 | 95 | // Select the program. 96 | GLES20.glUseProgram(mProgramHandle); 97 | GlUtil.checkGlError("glUseProgram"); 98 | 99 | // Copy the model / view / projection matrix over. 100 | GLES20.glUniformMatrix4fv(muMVPMatrixLoc, 1, false, mvpMatrix, 0); 101 | GlUtil.checkGlError("glUniformMatrix4fv"); 102 | 103 | // Copy the color vector in. 104 | GLES20.glUniform4fv(muColorLoc, 1, color, 0); 105 | GlUtil.checkGlError("glUniform4fv "); 106 | 107 | // Enable the "aPosition" vertex attribute. 108 | GLES20.glEnableVertexAttribArray(maPositionLoc); 109 | GlUtil.checkGlError("glEnableVertexAttribArray"); 110 | 111 | // Connect vertexBuffer to "aPosition". 112 | GLES20.glVertexAttribPointer(maPositionLoc, coordsPerVertex, 113 | GLES20.GL_FLOAT, false, vertexStride, vertexBuffer); 114 | GlUtil.checkGlError("glVertexAttribPointer"); 115 | 116 | // Draw the rect. 117 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, firstVertex, vertexCount); 118 | GlUtil.checkGlError("glDrawArrays"); 119 | 120 | // Done -- disable vertex array and program. 121 | GLES20.glDisableVertexAttribArray(maPositionLoc); 122 | GLES20.glUseProgram(0); 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /zero-camera/src/main/java/com/cry/zero_camera/ref/gles/FullFrameRect.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.cry.zero_camera.ref.gles; 18 | 19 | /** 20 | * This class essentially represents a viewport-sized sprite that will be rendered with 21 | * a texture, usually from an external source like the camera or video decoder. 22 | */ 23 | public class FullFrameRect { 24 | private final Drawable2d mRectDrawable = new Drawable2d(Drawable2d.Prefab.FULL_RECTANGLE); 25 | private Texture2dProgram mProgram; 26 | 27 | /** 28 | * Prepares the object. 29 | * 30 | * @param program The program to use. FullFrameRect takes ownership, and will release 31 | * the program when no longer needed. 32 | */ 33 | public FullFrameRect(Texture2dProgram program) { 34 | mProgram = program; 35 | } 36 | 37 | /** 38 | * Releases resources. 39 | *
40 | * This must be called with the appropriate EGL context current (i.e. the one that was 41 | * current when the constructor was called). If we're about to destroy the EGL context, 42 | * there's no value in having the caller make it current just to do this cleanup, so you 43 | * can pass a flag that will tell this function to skip any EGL-context-specific cleanup. 44 | */ 45 | public void release(boolean doEglCleanup) { 46 | if (mProgram != null) { 47 | if (doEglCleanup) { 48 | mProgram.release(); 49 | } 50 | mProgram = null; 51 | } 52 | } 53 | 54 | /** 55 | * Returns the program currently in use. 56 | */ 57 | public Texture2dProgram getProgram() { 58 | return mProgram; 59 | } 60 | 61 | /** 62 | * Changes the program. The previous program will be released. 63 | *
64 | * The appropriate EGL context must be current. 65 | */ 66 | public void changeProgram(Texture2dProgram program) { 67 | mProgram.release(); 68 | mProgram = program; 69 | } 70 | 71 | /** 72 | * Creates a texture object suitable for use with drawFrame(). 73 | */ 74 | public int createTextureObject() { 75 | return mProgram.createTextureObject(); 76 | } 77 | 78 | /** 79 | * Draws a viewport-filling rect, texturing it with the specified texture object. 80 | */ 81 | public void drawFrame(int textureId, float[] texMatrix) { 82 | // Use the identity matrix for MVP so our 2x2 FULL_RECTANGLE covers the viewport. 83 | mProgram.draw(GlUtil.IDENTITY_MATRIX, mRectDrawable.getVertexArray(), 0, 84 | mRectDrawable.getVertexCount(), mRectDrawable.getCoordsPerVertex(), 85 | mRectDrawable.getVertexStride(), 86 | texMatrix, mRectDrawable.getTexCoordArray(), textureId, 87 | mRectDrawable.getTexCoordStride()); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /zero-camera/src/main/java/com/cry/zero_camera/ref/gles/OffscreenSurface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.cry.zero_camera.ref.gles; 18 | 19 | /** 20 | * Off-screen EGL surface (pbuffer). 21 | *
22 | * It's good practice to explicitly release() the surface, preferably from a "finally" block. 23 | */ 24 | public class OffscreenSurface extends EglSurfaceBase { 25 | /** 26 | * Creates an off-screen surface with the specified width and height. 27 | */ 28 | public OffscreenSurface(EglCore eglCore, int width, int height) { 29 | super(eglCore); 30 | createOffscreenSurface(width, height); 31 | } 32 | 33 | /** 34 | * Releases any resources associated with the surface. 35 | */ 36 | public void release() { 37 | releaseEglSurface(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /zero-camera/src/main/java/com/cry/zero_camera/ref/gles/WindowSurface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.cry.zero_camera.ref.gles; 18 | 19 | import android.graphics.SurfaceTexture; 20 | import android.view.Surface; 21 | 22 | /** 23 | * Recordable EGL window surface. 24 | *
25 | * It's good practice to explicitly release() the surface, preferably from a "finally" block. 26 | */ 27 | public class WindowSurface extends EglSurfaceBase { 28 | private Surface mSurface; 29 | private boolean mReleaseSurface; 30 | 31 | /** 32 | * Associates an EGL surface with the native window surface. 33 | *
34 | * Set releaseSurface to true if you want the Surface to be released when release() is 35 | * called. This is convenient, but can interfere with framework classes that expect to 36 | * manage the Surface themselves (e.g. if you release a SurfaceView's Surface, the 37 | * surfaceDestroyed() callback won't fire). 38 | */ 39 | public WindowSurface(EglCore eglCore, Surface surface, boolean releaseSurface) { 40 | super(eglCore); 41 | createWindowSurface(surface); 42 | mSurface = surface; 43 | mReleaseSurface = releaseSurface; 44 | } 45 | 46 | /** 47 | * Associates an EGL surface with the SurfaceTexture. 48 | */ 49 | public WindowSurface(EglCore eglCore, SurfaceTexture surfaceTexture) { 50 | super(eglCore); 51 | createWindowSurface(surfaceTexture); 52 | } 53 | 54 | /** 55 | * Releases any resources associated with the EGL surface (and, if configured to do so, 56 | * with the Surface as well). 57 | *
58 | * Does not require that the surface's EGL context be current. 59 | */ 60 | public void release() { 61 | releaseEglSurface(); 62 | if (mSurface != null) { 63 | if (mReleaseSurface) { 64 | mSurface.release(); 65 | } 66 | mSurface = null; 67 | } 68 | } 69 | 70 | /** 71 | * Recreate the EGLSurface, using the new EglBase. The caller should have already 72 | * freed the old EGLSurface with releaseEglSurface(). 73 | *
74 | * This is useful when we want to update the EGLSurface associated with a Surface. 75 | * For example, if we want to share with a different EGLContext, which can only 76 | * be done by tearing down and recreating the context. (That's handled by the caller; 77 | * this just creates a new EGLSurface for the Surface we were handed earlier.) 78 | *
79 | * If the previous EGLSurface isn't fully destroyed, e.g. it's still current on a
80 | * context somewhere, the create call will fail with complaints from the Surface
81 | * about already being connected.
82 | */
83 | public void recreate(EglCore newEglCore) {
84 | if (mSurface == null) {
85 | throw new RuntimeException("not yet implemented for SurfaceTexture");
86 | }
87 | mEglCore = newEglCore; // switch to new context
88 | createWindowSurface(mSurface); // create new surface
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/zero-camera/src/main/java/com/cry/zero_camera/render/fliter/I2DFilter.java:
--------------------------------------------------------------------------------
1 | package com.cry.zero_camera.render.fliter;
2 |
3 | import android.opengl.GLES20;
4 | import android.opengl.Matrix;
5 |
6 | import com.cry.zero_common.opengl.GLESUtils;
7 |
8 | import java.nio.ByteBuffer;
9 | import java.nio.ByteOrder;
10 | import java.nio.FloatBuffer;
11 |
12 | public abstract class I2DFilter {
13 | protected float[] mMVPMatrix = new float[16];
14 | protected int width;
15 | protected int height;
16 | //顶点坐标
17 | private float sPos[] = {
18 | -1.0f, 1.0f,
19 | -1.0f, -1.0f,
20 | 1.0f, 1.0f,
21 | 1.0f, -1.0f,
22 | };
23 | //纹理坐标
24 | private float[] sCoord = {
25 | 0.0f, 0.0f,
26 | 0.0f, 1.0f,
27 | 1.0f, 0.0f,
28 | 1.0f, 1.0f,
29 | };
30 | private FloatBuffer mVerBuffer;
31 | private FloatBuffer mTexBuffer;
32 | private int mProgram;
33 | private int glPosition;
34 | private int glTexture;
35 | private int glCoordinate;
36 | private int glMatrix;
37 | private float[] mViewMatrix = new float[16];
38 | private float[] mProjectMatrix = new float[16];
39 | private int mTextureId;
40 |
41 |
42 | public I2DFilter() {
43 | initBuffer();
44 | }
45 |
46 | private void initBuffer() {
47 | ByteBuffer bb = ByteBuffer.allocateDirect(sPos.length * 4);
48 | bb.order(ByteOrder.nativeOrder());
49 | mVerBuffer = bb.asFloatBuffer();
50 | mVerBuffer.put(sPos);
51 | mVerBuffer.position(0);
52 | ByteBuffer cc = ByteBuffer.allocateDirect(sCoord.length * 4);
53 | cc.order(ByteOrder.nativeOrder());
54 | mTexBuffer = cc.asFloatBuffer();
55 | mTexBuffer.put(sCoord);
56 | mTexBuffer.position(0);
57 | }
58 |
59 | public void onCreate() {
60 | GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
61 | GLES20.glEnable(GLES20.GL_TEXTURE_2D);
62 | mProgram = GLESUtils.createProgram(obtainVertex(), obtainFragment());
63 | glPosition = GLES20.glGetAttribLocation(mProgram, "aPosition");
64 | glCoordinate = GLES20.glGetAttribLocation(mProgram, "aCoordinate");
65 | glMatrix = GLES20.glGetUniformLocation(mProgram, "uMatrix");
66 | glTexture = GLES20.glGetUniformLocation(mProgram, "uTexture");
67 | onExtraCreated(mProgram);
68 | }
69 |
70 | protected abstract String obtainVertex();
71 |
72 | protected abstract String obtainFragment();
73 |
74 | protected abstract void onExtraCreated(int mProgram);
75 |
76 | public void onSizeChange(int width, int height) {
77 | GLES20.glViewport(0, 0, width, height);
78 | this.width = width;
79 | this.height = height;
80 | float sWidthHeight = width / (float) height;
81 | float sWH = sWidthHeight;
82 | if (width > height) {
83 | if (sWH > sWidthHeight) {
84 | Matrix.orthoM(mProjectMatrix, 0, -sWidthHeight * sWH, sWidthHeight * sWH, -1, 1, 3, 5);
85 | } else {
86 | Matrix.orthoM(mProjectMatrix, 0, -sWidthHeight / sWH, sWidthHeight / sWH, -1, 1, 3, 5);
87 | }
88 | } else {
89 | if (sWH > sWidthHeight) {
90 | Matrix.orthoM(mProjectMatrix, 0, -1, 1, -1 / sWidthHeight * sWH, 1 / sWidthHeight * sWH, 3, 5);
91 | } else {
92 | Matrix.orthoM(mProjectMatrix, 0, -1, 1, -sWH / sWidthHeight, sWH / sWidthHeight, 3, 5);
93 | }
94 | }
95 | //设置相机位置
96 | Matrix.setLookAtM(mViewMatrix, 0, 0, 0, 5.0f, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
97 | //计算变换矩阵
98 | Matrix.multiplyMM(mMVPMatrix, 0, mProjectMatrix, 0, mViewMatrix, 0);
99 | }
100 |
101 | public void onDrawFrame() {
102 | beforeDraw();
103 | onClear();
104 | onUseProgram();
105 | onExtraData();
106 | onBindTexture();
107 | onDraw();
108 | afterDraw();
109 | }
110 |
111 |
112 | protected void beforeDraw() {
113 |
114 | }
115 |
116 | protected void afterDraw() {
117 |
118 | }
119 |
120 | protected void onClear() {
121 | GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
122 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
123 | }
124 |
125 | protected void onUseProgram() {
126 | GLES20.glUseProgram(mProgram);
127 | }
128 |
129 | protected void onExtraData() {
130 | GLES20.glUniformMatrix4fv(glMatrix, 1, false, mMVPMatrix, 0);
131 | }
132 |
133 | protected void onBindTexture() {
134 | GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
135 | GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, getTextureId());
136 | GLES20.glUniform1i(glTexture, 0);
137 | }
138 |
139 | protected void onDraw() {
140 | GLES20.glEnableVertexAttribArray(glPosition);
141 | GLES20.glVertexAttribPointer(glPosition, 2, GLES20.GL_FLOAT, false, 0, mVerBuffer);
142 | GLES20.glEnableVertexAttribArray(glCoordinate);
143 | GLES20.glVertexAttribPointer(glCoordinate, 2, GLES20.GL_FLOAT, false, 0, mTexBuffer);
144 |
145 | GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
146 |
147 | GLES20.glDisableVertexAttribArray(glPosition);
148 | GLES20.glDisableVertexAttribArray(glCoordinate);
149 | }
150 |
151 |
152 | public void release() {
153 | if (mTextureId != 0) {
154 | int[] values = new int[1];
155 | values[0] = mTextureId;
156 | GLES20.glDeleteTextures(1, values, 0);
157 | mTextureId = 0;
158 | }
159 | if (mProgram != 0) {
160 | GLES20.glDeleteProgram(mProgram);
161 | mProgram = 0;
162 | }
163 | }
164 |
165 | public int getTextureId() {
166 | return mTextureId;
167 | }
168 |
169 | public void setTextureId(int mTextureId) {
170 | this.mTextureId = mTextureId;
171 | }
172 |
173 | public void setMVPMatrix(float[] mMVPMatrix) {
174 | this.mMVPMatrix = mMVPMatrix;
175 | }
176 |
177 | public float[] getMVPMatrix() {
178 | return mMVPMatrix;
179 | }
180 | }
181 |
--------------------------------------------------------------------------------
/zero-camera/src/main/java/com/cry/zero_camera/render/fliter/PhotoAlphaFilter.java:
--------------------------------------------------------------------------------
1 | package com.cry.zero_camera.render.fliter;
2 |
3 | import android.graphics.Bitmap;
4 | import android.opengl.GLES20;
5 |
6 | import com.cry.zero_common.opengl.GLESUtils;
7 | import com.cry.zero_common.opengl.MatrixUtils;
8 |
9 | public class PhotoAlphaFilter extends I2DFilter {
10 | private Bitmap mBitmap;
11 | private int uAlphaLocation;
12 | private float mAlpha = 0.2f;
13 | ;
14 |
15 | public PhotoAlphaFilter() {
16 | }
17 |
18 | @Override
19 | public String obtainVertex() {
20 | return "attribute vec4 aPosition;\n" +
21 | "attribute vec2 aCoordinate;\n" +
22 | "uniform mat4 uMatrix;\n" +
23 | "varying vec2 vTextureCoordinate;\n" +
24 | "\n" +
25 | "void main(){\n" +
26 | " gl_Position = uMatrix*aPosition;\n" +
27 | " vTextureCoordinate = aCoordinate;\n" +
28 | "}";
29 | }
30 |
31 | @Override
32 | public String obtainFragment() {
33 | return "precision mediump float;\n" +
34 | "\n" +
35 | "varying vec2 vTextureCoordinate;\n" +
36 | "uniform sampler2D uTexture;\n" +
37 | "uniform float uAlphaLocation;\n" +
38 | "void main() {\n" +
39 | " gl_FragColor = vec4(texture2D(uTexture,vTextureCoordinate).rgb,uAlphaLocation);\n" +
40 | "}";
41 | }
42 |
43 | @Override
44 | public void onClear() {
45 |
46 | }
47 |
48 | @Override
49 | public void onExtraCreated(int mProgram) {
50 | uAlphaLocation = GLES20.glGetUniformLocation(mProgram, "uAlphaLocation");
51 | }
52 |
53 | @Override
54 | protected void onExtraData() {
55 | super.onExtraData();
56 |
57 | GLES20.glUniform1f(uAlphaLocation, mAlpha);
58 | }
59 |
60 | public Bitmap getBitmap() {
61 | return mBitmap;
62 | }
63 |
64 | public void setBitmap(Bitmap bitmap) {
65 | // if (this.mBitmap == bitmap) {
66 | // return;
67 | // }
68 | this.mBitmap = bitmap;
69 | // deleteTextureId();
70 | setTextureId(GLESUtils.createTexture(bitmap));
71 | setMVPMatrix(MatrixUtils.calculateMatrixForBitmap(bitmap, width, height));
72 | }
73 |
74 | private void deleteTextureId() {
75 | int preTextureId = getTextureId();
76 | if (preTextureId != 0) {
77 | int[] values = new int[1];
78 | values[0] = preTextureId;
79 | GLES20.glDeleteTextures(1, values, 0);
80 | }
81 | }
82 |
83 | public void setAlpha(float mAlpha) {
84 | this.mAlpha = mAlpha;
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/zero-camera/src/main/java/com/cry/zero_camera/render/fliter/PhotoAlphaFilter2.java:
--------------------------------------------------------------------------------
1 | package com.cry.zero_camera.render.fliter;
2 |
3 | import android.graphics.Bitmap;
4 | import android.opengl.GLES20;
5 |
6 | import com.cry.zero_common.opengl.GLESUtils;
7 | import com.cry.zero_common.opengl.MatrixUtils;
8 |
9 | public class PhotoAlphaFilter2 extends I2DFilter {
10 | private Bitmap mBitmap;
11 | private int uAlphaLocation;
12 | private float mAlpha = 0.2f;
13 | ;
14 |
15 | public PhotoAlphaFilter2() {
16 | }
17 |
18 | @Override
19 | public String obtainVertex() {
20 | return "attribute vec4 aPosition;\n" +
21 | "attribute vec2 aCoordinate;\n" +
22 | "uniform mat4 uMatrix;\n" +
23 | "varying vec2 vTextureCoordinate;\n" +
24 | "\n" +
25 | "void main(){\n" +
26 | " gl_Position = uMatrix*aPosition;\n" +
27 | " vTextureCoordinate = aCoordinate;\n" +
28 | "}";
29 | }
30 |
31 | @Override
32 | public String obtainFragment() {
33 | return "precision mediump float;\n" +
34 | "\n" +
35 | "varying vec2 vTextureCoordinate;\n" +
36 | "uniform sampler2D uTexture;\n" +
37 | "uniform float uAlphaLocation;\n" +
38 | "void main() {\n" +
39 | " gl_FragColor = vec4(texture2D(uTexture,vTextureCoordinate).rgb,uAlphaLocation);\n" +
40 | "}";
41 | }
42 |
43 | @Override
44 | public void onClear() {
45 |
46 | }
47 |
48 | @Override
49 | public void onExtraCreated(int mProgram) {
50 | uAlphaLocation = GLES20.glGetUniformLocation(mProgram, "uAlphaLocation");
51 | }
52 |
53 | @Override
54 | protected void onExtraData() {
55 | super.onExtraData();
56 |
57 | GLES20.glUniform1f(uAlphaLocation, mAlpha);
58 | }
59 |
60 | public Bitmap getBitmap() {
61 | return mBitmap;
62 | }
63 |
64 | public void setBitmap(Bitmap bitmap) {
65 | // if (this.mBitmap == bitmap) {
66 | // return;
67 | // }
68 | this.mBitmap = bitmap;
69 | // deleteTextureId();
70 | setTextureId(GLESUtils.createTexture(bitmap));
71 | setMVPMatrix(MatrixUtils.calculateMatrixForBitmap(bitmap, width, height));
72 | }
73 |
74 | private void deleteTextureId() {
75 | int preTextureId = getTextureId();
76 | if (preTextureId != 0) {
77 | int[] values = new int[1];
78 | values[0] = preTextureId;
79 | GLES20.glDeleteTextures(1, values, 0);
80 | }
81 | }
82 |
83 | public void setAlpha(float mAlpha) {
84 | this.mAlpha = mAlpha;
85 | }
86 |
87 | @Override
88 | public void release() {
89 | releaseBitmap();
90 | super.release();
91 | }
92 |
93 | private void releaseBitmap() {
94 | if (this.mBitmap != null) {
95 | mBitmap.recycle();
96 | mBitmap = null;
97 | }
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/zero-camera/src/main/java/com/cry/zero_camera/render/fliter/PhotoFilter.java:
--------------------------------------------------------------------------------
1 | package com.cry.zero_camera.render.fliter;
2 |
3 | import android.graphics.Bitmap;
4 |
5 | import com.cry.zero_common.opengl.GLESUtils;
6 | import com.cry.zero_common.opengl.MatrixUtils;
7 |
8 | public class PhotoFilter extends I2DFilter {
9 | private Bitmap mBitmap;
10 |
11 | public PhotoFilter() {
12 | }
13 |
14 | @Override
15 | public String obtainVertex() {
16 | return "attribute vec4 aPosition;\n" +
17 | "attribute vec2 aCoordinate;\n" +
18 | "uniform mat4 uMatrix;\n" +
19 | "varying vec2 vTextureCoordinate;\n" +
20 | "\n" +
21 | "void main(){\n" +
22 | " gl_Position = uMatrix*aPosition;\n" +
23 | " vTextureCoordinate = aCoordinate;\n" +
24 | "}";
25 | }
26 |
27 | @Override
28 | public String obtainFragment() {
29 | return "precision mediump float;\n" +
30 | "\n" +
31 | "varying vec2 vTextureCoordinate;\n" +
32 | "uniform sampler2D uTexture;\n" +
33 | "void main() {\n" +
34 | " gl_FragColor = texture2D(uTexture,vTextureCoordinate);\n" +
35 | "}";
36 | }
37 |
38 | @Override
39 | public void onExtraCreated(int mProgram) {
40 |
41 | }
42 |
43 | public Bitmap getBitmap() {
44 | return mBitmap;
45 | }
46 |
47 | public void setBitmap(Bitmap bitmap) {
48 | if (mBitmap == bitmap) {
49 | return;
50 | }
51 | if (this.mBitmap != null) {
52 | releaseBitmap();
53 | }
54 | this.mBitmap = bitmap;
55 | setTextureId(GLESUtils.createTexture(bitmap));
56 | setMVPMatrix(MatrixUtils.calculateMatrixForBitmap(bitmap, width, height));
57 | }
58 |
59 | @Override
60 | public void release() {
61 | releaseBitmap();
62 | super.release();
63 | }
64 |
65 | private void releaseBitmap() {
66 | if (this.mBitmap != null) {
67 | mBitmap.recycle();
68 | mBitmap = null;
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/zero-camera/src/main/java/com/cry/zero_camera/render/fliter/PhotoFilter2.java:
--------------------------------------------------------------------------------
1 | package com.cry.zero_camera.render.fliter;
2 |
3 | import android.graphics.Bitmap;
4 |
5 | import com.cry.zero_common.opengl.GLESUtils;
6 | import com.cry.zero_common.opengl.MatrixUtils;
7 |
8 | public class PhotoFilter2 extends I2DFilter {
9 | private Bitmap mBitmap;
10 |
11 | public PhotoFilter2() {
12 | }
13 |
14 | @Override
15 | public String obtainVertex() {
16 | return "attribute vec4 aPosition;\n" +
17 | "attribute vec2 aCoordinate;\n" +
18 | "uniform mat4 uMatrix;\n" +
19 | "varying vec2 vTextureCoordinate;\n" +
20 | "\n" +
21 | "void main(){\n" +
22 | " gl_Position = uMatrix*aPosition;\n" +
23 | " vTextureCoordinate = aCoordinate;\n" +
24 | "}";
25 | }
26 |
27 | @Override
28 | public String obtainFragment() {
29 | return "precision mediump float;\n" +
30 | "\n" +
31 | "varying vec2 vTextureCoordinate;\n" +
32 | "uniform sampler2D uTexture;\n" +
33 | "void main() {\n" +
34 | " gl_FragColor = texture2D(uTexture,vTextureCoordinate);\n" +
35 | "}";
36 | }
37 |
38 | @Override
39 | public void onExtraCreated(int mProgram) {
40 |
41 | }
42 |
43 | public Bitmap getBitmap() {
44 | return mBitmap;
45 | }
46 |
47 | public void setBitmap(Bitmap bitmap) {
48 | if (mBitmap == bitmap) {
49 | return;
50 | }
51 | if (this.mBitmap != null) {
52 | releaseBitmap();
53 | }
54 | this.mBitmap = bitmap;
55 | setTextureId(GLESUtils.createTexture(bitmap));
56 | setMVPMatrix(MatrixUtils.calculateMatrixForBitmap(bitmap, width, height));
57 | }
58 |
59 | @Override
60 | public void release() {
61 | releaseBitmap();
62 | super.release();
63 | }
64 |
65 | @Override
66 | protected void onClear() {
67 |
68 | }
69 |
70 | private void releaseBitmap() {
71 | if (this.mBitmap != null) {
72 | mBitmap.recycle();
73 | mBitmap = null;
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/zero-camera/src/main/java/com/cry/zero_camera/render/fliter/Show2DFilter.java:
--------------------------------------------------------------------------------
1 | package com.cry.zero_camera.render.fliter;
2 |
3 | import android.opengl.Matrix;
4 |
5 | import com.cry.zero_common.opengl.MatrixUtils;
6 |
7 | public class Show2DFilter extends I2DFilter {
8 | @Override
9 | public String obtainVertex() {
10 | return "attribute vec4 aPosition;\n" +
11 | "attribute vec2 aCoordinate;\n" +
12 | "uniform mat4 uMatrix;\n" +
13 | "varying vec2 vTextureCoordinate;\n" +
14 | "\n" +
15 | "void main(){\n" +
16 | " gl_Position = uMatrix*aPosition;\n" +
17 | " vTextureCoordinate = aCoordinate;\n" +
18 | "}";
19 | }
20 |
21 | @Override
22 | public String obtainFragment() {
23 | return "precision mediump float;\n" +
24 | "\n" +
25 | "varying vec2 vTextureCoordinate;\n" +
26 | "uniform sampler2D uTexture;\n" +
27 | "void main() {\n" +
28 | " gl_FragColor = texture2D(uTexture,vTextureCoordinate);\n" +
29 | "}";
30 | }
31 |
32 |
33 | @Override
34 | protected void onExtraCreated(int mProgram) {
35 |
36 | }
37 |
38 | @Override
39 | public void onSizeChange(int width, int height) {
40 | this.width = width;
41 | this.height = height;
42 | Matrix.setIdentityM(mMVPMatrix, 0);
43 | MatrixUtils.flip(mMVPMatrix, false, true);
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/zero-camera/src/main/res/layout/activity_camera.xml:
--------------------------------------------------------------------------------
1 |
2 |