Skip to content

Commit

Permalink
Fix issues and tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
nekocode committed Sep 29, 2016
1 parent 786a0a3 commit a7818cb
Show file tree
Hide file tree
Showing 29 changed files with 71 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

Realtime camera filters. Process frames by OpenGL shaders.

**[Download the apk](https://github.com/WeLikeVis/CameraFilter/releases/download/2.1.1/app-debug.apk)** to have a try.
**[Download the apk](https://github.com/WeLikeVis/CameraFilter/releases/download/2.2/CameraFilter.apk)** to have a try.

## Filters

Expand Down
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "24.0.0"
compileSdkVersion 24
buildToolsVersion "24.0.2"

defaultConfig {
applicationId "cn.nekocode.camerafilter"
minSdkVersion 15
targetSdkVersion 23
targetSdkVersion 24
versionCode 211
versionName "2.1.1"
versionName "2.2"
}
buildTypes {
release {
Expand All @@ -21,5 +21,5 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:appcompat-v7:24.2.1'
}
47 changes: 32 additions & 15 deletions app/src/main/java/cn/nekocode/camerafilter/CameraRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@
import cn.nekocode.camerafilter.filter.TrianglesMosaicFilter;

/**
* Created by nekocode on 16/8/5.
* @author nekocode (nekocode.cn@gmail.com)
*/
public class CameraRenderer extends Thread implements TextureView.SurfaceTextureListener {
public class CameraRenderer implements Runnable, TextureView.SurfaceTextureListener {
private static final String TAG = "CameraRenderer";
private static final int EGL_OPENGL_ES2_BIT = 4;
private static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
private static final int DRAW_INTERVAL = 1000 / 30;

private Thread renderThread;
private Context context;
private SurfaceTexture surfaceTexture;
private int gwidth, gheight;
Expand All @@ -78,6 +79,7 @@ public class CameraRenderer extends Thread implements TextureView.SurfaceTexture
private SurfaceTexture cameraSurfaceTexture;
private int cameraTextureId;
private CameraFilter selectedFilter;
private int selectedFilterId = R.id.filter0;
private SparseArray<CameraFilter> cameraFilterMap = new SparseArray<>();

public CameraRenderer(Context context) {
Expand All @@ -90,7 +92,8 @@ public void onSurfaceTextureUpdated(SurfaceTexture surface) {

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
GLES20.glViewport(0, 0, gwidth = width, gheight = height);
gwidth = -width;
gheight = -height;
}

@Override
Expand All @@ -99,33 +102,39 @@ public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
camera.stopPreview();
camera.release();
}
interrupt();
if (renderThread != null && renderThread.isAlive()) {
renderThread.interrupt();
}
CameraFilter.release();

return true;
}

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
if (isAlive()) {
interrupt();
if (renderThread != null && renderThread.isAlive()) {
renderThread.interrupt();
}
renderThread = new Thread(this);

surfaceTexture = surface;
GLES20.glViewport(0, 0, gwidth = width, gheight = height);
gwidth = -width;
gheight = -height;

// Open camera
Pair<Camera.CameraInfo, Integer> backCamera = getBackCamera();
final int backCameraId = backCamera.second;
camera = Camera.open(backCameraId);

// Start rendering
start();
renderThread.start();
}

public void setSelectedFilter(int id) {
selectedFilterId = id;
selectedFilter = cameraFilterMap.get(id);
selectedFilter.onAttach();
if (selectedFilter != null)
selectedFilter.onAttach();
}

@Override
Expand Down Expand Up @@ -154,7 +163,7 @@ public void run() {
cameraFilterMap.append(R.id.filter18, new CrackedFilter(context));
cameraFilterMap.append(R.id.filter19, new PolygonizationFilter(context));
cameraFilterMap.append(R.id.filter20, new JFAVoronoiFilter(context));
setSelectedFilter(R.id.filter0);
setSelectedFilter(selectedFilterId);

// Create texture for camera preview
cameraTextureId = MyGLUtils.genTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES);
Expand All @@ -171,6 +180,9 @@ public void run() {
// Render loop
while (!Thread.currentThread().isInterrupted()) {
try {
if (gwidth < 0 && gheight < 0)
GLES20.glViewport(0, 0, gwidth = -gwidth, gheight = -gheight);

GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

// Update the camera preview texture
Expand Down Expand Up @@ -201,12 +213,14 @@ private void initGL(SurfaceTexture texture) {

eglDisplay = egl10.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
if (eglDisplay == EGL10.EGL_NO_DISPLAY) {
throw new RuntimeException("eglGetDisplay failed " + android.opengl.GLUtils.getEGLErrorString(egl10.eglGetError()));
throw new RuntimeException("eglGetDisplay failed " +
android.opengl.GLUtils.getEGLErrorString(egl10.eglGetError()));
}

int[] version = new int[2];
if (!egl10.eglInitialize(eglDisplay, version)) {
throw new RuntimeException("eglInitialize failed " + android.opengl.GLUtils.getEGLErrorString(egl10.eglGetError()));
throw new RuntimeException("eglInitialize failed " +
android.opengl.GLUtils.getEGLErrorString(egl10.eglGetError()));
}

int[] configsCount = new int[1];
Expand All @@ -225,7 +239,8 @@ private void initGL(SurfaceTexture texture) {

EGLConfig eglConfig = null;
if (!egl10.eglChooseConfig(eglDisplay, configSpec, configs, 1, configsCount)) {
throw new IllegalArgumentException("eglChooseConfig failed " + android.opengl.GLUtils.getEGLErrorString(egl10.eglGetError()));
throw new IllegalArgumentException("eglChooseConfig failed " +
android.opengl.GLUtils.getEGLErrorString(egl10.eglGetError()));
} else if (configsCount[0] > 0) {
eglConfig = configs[0];
}
Expand All @@ -243,11 +258,13 @@ private void initGL(SurfaceTexture texture) {
Log.e(TAG, "eglCreateWindowSurface returned EGL10.EGL_BAD_NATIVE_WINDOW");
return;
}
throw new RuntimeException("eglCreateWindowSurface failed " + android.opengl.GLUtils.getEGLErrorString(error));
throw new RuntimeException("eglCreateWindowSurface failed " +
android.opengl.GLUtils.getEGLErrorString(error));
}

if (!egl10.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext)) {
throw new RuntimeException("eglMakeCurrent failed " + android.opengl.GLUtils.getEGLErrorString(egl10.eglGetError()));
throw new RuntimeException("eglMakeCurrent failed " +
android.opengl.GLUtils.getEGLErrorString(egl10.eglGetError()));
}
}

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/cn/nekocode/camerafilter/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author nekocode (nekocode.cn@gmail.com)
*/
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CAMERA_PERMISSION = 101;
private CameraRenderer renderer;
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/cn/nekocode/camerafilter/MyGLUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import javax.microedition.khronos.opengles.GL10;

/**
* Created by nekocode on 16/8/6.
* @author nekocode (nekocode.cn@gmail.com)
*/
public class MyGLUtils {
private static final String TAG = "MyGLUtils";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import javax.microedition.khronos.opengles.GL10;

/**
* Created by nekocode on 16/8/12.
* @author nekocode (nekocode.cn@gmail.com)
*/
public class RenderBuffer {
private int texId = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import cn.nekocode.camerafilter.R;

/**
* Created by nekocode on 16/8/6.
* @author nekocode (nekocode.cn@gmail.com)
*/
public class AsciiArtFilter extends CameraFilter {
private int program;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import cn.nekocode.camerafilter.R;

/**
* Created by nekocode on 16/8/6.
* @author nekocode (nekocode.cn@gmail.com)
*/
public class BasicDeformFilter extends CameraFilter {
private int program;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import cn.nekocode.camerafilter.R;

/**
* Created by nekocode on 16/8/6.
* @author nekocode (nekocode.cn@gmail.com)
*/
public class BlueorangeFilter extends CameraFilter {
private int program;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import cn.nekocode.camerafilter.RenderBuffer;

/**
* Created by nekocode on 16/8/6.
* @author nekocode (nekocode.cn@gmail.com)
*/
public abstract class CameraFilter {
static final float SQUARE_COORDS[] = {
Expand Down Expand Up @@ -128,7 +128,7 @@ final public void draw(int cameraTexId, int canvasWidth, int canvasHeight) {

onDraw(CAMERA_RENDER_BUF.getTexId(), canvasWidth, canvasHeight);

iFrame ++;
iFrame++;
}

abstract void onDraw(int cameraTexId, int canvasWidth, int canvasHeight);
Expand Down Expand Up @@ -159,7 +159,7 @@ void setupShaderInputs(int program, FloatBuffer vertex, FloatBuffer textureCoord
GLES20.glEnableVertexAttribArray(vTexCoordLocation);
GLES20.glVertexAttribPointer(vTexCoordLocation, 2, GLES20.GL_FLOAT, false, 4 * 2, textureCoord);

for (int i = 0; i < iChannels.length; i ++) {
for (int i = 0; i < iChannels.length; i++) {
int sTextureLocation = GLES20.glGetUniformLocation(program, "iChannel" + i);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, iChannels[i]);
Expand All @@ -168,9 +168,9 @@ void setupShaderInputs(int program, FloatBuffer vertex, FloatBuffer textureCoord

float _iChannelResolutions[] = new float[iChannelResolutions.length * 3];
for (int i = 0; i < iChannelResolutions.length; i++) {
_iChannelResolutions[i*3] = iChannelResolutions[i][0];
_iChannelResolutions[i*3 + 1] = iChannelResolutions[i][1];
_iChannelResolutions[i*3 + 2] = 1.0f;
_iChannelResolutions[i * 3] = iChannelResolutions[i][0];
_iChannelResolutions[i * 3 + 1] = iChannelResolutions[i][1];
_iChannelResolutions[i * 3 + 2] = 1.0f;
}

int iChannelResolutionLocation = GLES20.glGetUniformLocation(program, "iChannelResolution");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import cn.nekocode.camerafilter.R;

/**
* Created by nekocode on 16/8/6.
* @author nekocode (nekocode.cn@gmail.com)
*/
public class ChromaticAberrationFilter extends CameraFilter {
private int program;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import cn.nekocode.camerafilter.R;

/**
* Created by nekocode on 16/8/6.
* @author nekocode (nekocode.cn@gmail.com)
*/
public class ContrastFilter extends CameraFilter {
private int program;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import cn.nekocode.camerafilter.R;

/**
* Created by nekocode on 16/8/6.
* @author nekocode (nekocode.cn@gmail.com)
*/
public class CrackedFilter extends CameraFilter {
private int program;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import cn.nekocode.camerafilter.R;

/**
* Created by nekocode on 16/8/6.
* @author nekocode (nekocode.cn@gmail.com)
*/
public class CrosshatchFilter extends CameraFilter {
private int program;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import cn.nekocode.camerafilter.R;

/**
* Created by nekocode on 16/8/6.
* @author nekocode (nekocode.cn@gmail.com)
*/
public class EMInterferenceFilter extends CameraFilter {
private int program;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import cn.nekocode.camerafilter.R;

/**
* Created by nekocode on 16/8/6.
* @author nekocode (nekocode.cn@gmail.com)
*/
public class EdgeDetectionFilter extends CameraFilter {
private int program;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
import cn.nekocode.camerafilter.RenderBuffer;

/**
* Created by nekocode on 16/8/6.
* TODO Not working well
* @author nekocode (nekocode.cn@gmail.com)
*/
public class JFAVoronoiFilter extends CameraFilter {
private int programImg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import cn.nekocode.camerafilter.R;

/**
* Created by nekocode on 16/8/6.
* @author nekocode (nekocode.cn@gmail.com)
*/
public class LegofiedFilter extends CameraFilter {
private int program;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import cn.nekocode.camerafilter.R;

/**
* Created by nekocode on 16/8/6.
* @author nekocode (nekocode.cn@gmail.com)
*/
public class LichtensteinEsqueFilter extends CameraFilter {
private int program;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import cn.nekocode.camerafilter.R;

/**
* Created by nekocode on 16/8/6.
* @author nekocode (nekocode.cn@gmail.com)
*/
public class MappingFilter extends CameraFilter {
private int program;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import cn.nekocode.camerafilter.R;

/**
* Created by nekocode on 16/8/6.
* @author nekocode (nekocode.cn@gmail.com)
*/
public class MoneyFilter extends CameraFilter {
private int program;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import cn.nekocode.camerafilter.R;

/**
* Created by nekocode on 16/8/6.
* @author nekocode (nekocode.cn@gmail.com)
*/
public class NoiseWarpFilter extends CameraFilter {
private int program;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import cn.nekocode.camerafilter.R;

/**
* Created by nekocode on 16/8/6.
* @author nekocode (nekocode.cn@gmail.com)
*/
public class OriginalFilter extends CameraFilter {
private int program;
Expand Down
Loading

0 comments on commit a7818cb

Please sign in to comment.