Skip to content

Commit c2d152a

Browse files
committed
1.Add black and white filter;
2.Add function: switch filter after swiping the view horizontally or vertically.
1 parent cae6921 commit c2d152a

File tree

5 files changed

+158
-16
lines changed

5 files changed

+158
-16
lines changed

app/src/main/java/cn/nekocode/camerafilter/CameraRenderer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import cn.nekocode.camerafilter.filter.AsciiArtFilter;
3737
import cn.nekocode.camerafilter.filter.BasicDeformFilter;
38+
import cn.nekocode.camerafilter.filter.BlackAndWhiteFilter;
3839
import cn.nekocode.camerafilter.filter.BlueorangeFilter;
3940
import cn.nekocode.camerafilter.filter.CameraFilter;
4041
import cn.nekocode.camerafilter.filter.ChromaticAberrationFilter;
@@ -163,6 +164,7 @@ public void run() {
163164
cameraFilterMap.append(R.id.filter18, new CrackedFilter(context));
164165
cameraFilterMap.append(R.id.filter19, new PolygonizationFilter(context));
165166
cameraFilterMap.append(R.id.filter20, new JFAVoronoiFilter(context));
167+
cameraFilterMap.append(R.id.filter21, new BlackAndWhiteFilter(context));
166168
setSelectedFilter(selectedFilterId);
167169

168170
// Create texture for camera preview

app/src/main/java/cn/nekocode/camerafilter/MainActivity.java

Lines changed: 89 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.support.v4.app.ActivityCompat;
2424
import android.support.v4.content.ContextCompat;
2525
import android.support.v7.app.AppCompatActivity;
26+
import android.view.GestureDetector;
2627
import android.view.Menu;
2728
import android.view.MenuItem;
2829
import android.view.MotionEvent;
@@ -37,23 +38,45 @@
3738
import java.io.IOException;
3839
import java.io.OutputStream;
3940
import java.text.SimpleDateFormat;
41+
import java.util.ArrayList;
42+
import java.util.Arrays;
4043
import java.util.Date;
4144

4245
/**
4346
* @author nekocode ([email protected])
4447
*/
45-
public class MainActivity extends AppCompatActivity {
48+
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {
4649
private static final int REQUEST_CAMERA_PERMISSION = 101;
4750
private FrameLayout container;
4851
private CameraRenderer renderer;
4952
private TextureView textureView;
5053
private int filterId = R.id.filter0;
54+
private int mCurrentFilterId = 0;
55+
56+
String[] TITLES = {"Original", "EdgeDectection", "Pixelize",
57+
"EMInterference", "TrianglesMosaic", "Legofied",
58+
"TileMosaic", "Blueorange", "ChromaticAberration",
59+
"BasicDeform", "Contrast", "NoiseWarp", "Refraction",
60+
"Mapping", "Crosshatch", "LichtensteinEsque",
61+
"AsciiArt", "MoneyFilter", "Cracked", "Polygonization",
62+
"JFAVoronoi", "BlackAndWhite"};
63+
64+
Integer[] FILTER_RES_IDS = {R.id.filter0, R.id.filter1, R.id.filter2, R.id.filter3, R.id.filter4,
65+
R.id.filter5, R.id.filter6, R.id.filter7, R.id.filter8, R.id.filter9, R.id.filter10,
66+
R.id.filter11, R.id.filter12, R.id.filter13, R.id.filter14, R.id.filter15, R.id.filter16,
67+
R.id.filter17, R.id.filter18, R.id.filter19, R.id.filter20,
68+
R.id.filter21};
69+
70+
ArrayList<Integer> mFilterArray = new ArrayList<>(Arrays.asList(FILTER_RES_IDS));
71+
72+
GestureDetector mGestureDetector;
5173

5274
@Override
5375
protected void onCreate(Bundle savedInstanceState) {
5476
super.onCreate(savedInstanceState);
5577
setContentView(container = new FrameLayout(this));
56-
setTitle("Original");
78+
setTitle(TITLES[mCurrentFilterId]);
79+
5780

5881
if (ContextCompat.checkSelfPermission(this,
5982
Manifest.permission.CAMERA)
@@ -71,6 +94,8 @@ protected void onCreate(Bundle savedInstanceState) {
7194
} else {
7295
setupCameraPreviewView();
7396
}
97+
98+
mGestureDetector = new GestureDetector(this, this);
7499
}
75100

76101
@Override
@@ -90,20 +115,11 @@ void setupCameraPreviewView() {
90115
container.addView(textureView);
91116
textureView.setSurfaceTextureListener(renderer);
92117

93-
// Show original frame when touch the view
118+
// textureView.setOnTouchListener(this);
94119
textureView.setOnTouchListener(new View.OnTouchListener() {
95120
@Override
96-
public boolean onTouch(View v, MotionEvent event) {
97-
switch (event.getAction()) {
98-
case MotionEvent.ACTION_DOWN:
99-
renderer.setSelectedFilter(R.id.filter0);
100-
break;
101-
102-
case MotionEvent.ACTION_UP:
103-
case MotionEvent.ACTION_CANCEL:
104-
renderer.setSelectedFilter(filterId);
105-
break;
106-
}
121+
public boolean onTouch(View view, MotionEvent motionEvent) {
122+
mGestureDetector.onTouchEvent(motionEvent);
107123
return true;
108124
}
109125
});
@@ -139,7 +155,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
139155

140156
if (renderer != null)
141157
renderer.setSelectedFilter(filterId);
142-
158+
mCurrentFilterId = mFilterArray.indexOf(filterId);
143159
return true;
144160
}
145161

@@ -178,4 +194,62 @@ private String genSaveFileName(String prefix, String suffix) {
178194
String externalPath = Environment.getExternalStorageDirectory().toString();
179195
return externalPath + "/" + prefix + timeString + suffix;
180196
}
197+
198+
@Override
199+
public boolean onDown(MotionEvent motionEvent) {
200+
201+
return false;
202+
}
203+
204+
@Override
205+
public void onShowPress(MotionEvent motionEvent) {
206+
207+
}
208+
209+
@Override
210+
public boolean onSingleTapUp(MotionEvent motionEvent) {
211+
return false;
212+
}
213+
214+
@Override
215+
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
216+
return false;
217+
}
218+
219+
@Override
220+
public void onLongPress(MotionEvent motionEvent) {
221+
222+
}
223+
224+
@Override
225+
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
226+
float velocity = Math.abs(velocityX) > Math.abs(velocityY) ? velocityX : velocityY;
227+
int step = velocity > 0 ? -1 : 1;
228+
mCurrentFilterId = circleLoop(TITLES.length, mCurrentFilterId, step);
229+
setTitle(TITLES[mCurrentFilterId]);
230+
if (renderer != null) {
231+
renderer.setSelectedFilter(FILTER_RES_IDS[mCurrentFilterId]);
232+
}
233+
return true;
234+
}
235+
236+
private int circleLoop(int size, int currentPos, int step) {
237+
if (step == 0) {
238+
return currentPos;
239+
}
240+
241+
if (step > 0) {
242+
if (currentPos + step >= size) {
243+
return (currentPos + step) % size;
244+
} else {
245+
return currentPos + step;
246+
}
247+
} else {
248+
if (currentPos + step < 0) {
249+
return currentPos + step + size;
250+
} else {
251+
return currentPos + step;
252+
}
253+
}
254+
}
181255
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2016 nekocode
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+
package cn.nekocode.camerafilter.filter;
17+
18+
import android.content.Context;
19+
import android.opengl.GLES20;
20+
21+
import cn.nekocode.camerafilter.MyGLUtils;
22+
import cn.nekocode.camerafilter.R;
23+
24+
/**
25+
* @author nekocode ([email protected])
26+
*/
27+
public class BlackAndWhiteFilter extends CameraFilter {
28+
private int program;
29+
30+
public BlackAndWhiteFilter(Context context) {
31+
super(context);
32+
program = MyGLUtils.buildProgram(context, R.raw.vertext, R.raw.black_and_white);
33+
}
34+
35+
@Override
36+
public void onDraw(int cameraTexId, int canvasWidth, int canvasHeight) {
37+
setupShaderInputs(program,
38+
new int[]{canvasWidth, canvasHeight},
39+
new int[]{cameraTexId},
40+
new int[][]{});
41+
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
42+
}
43+
}

app/src/main/res/menu/filter.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<menu xmlns:android="http://schemas.android.com/apk/res/android"
2-
xmlns:app="http://schemas.android.com/apk/res-auto"
2+
xmlns:app="http://schemas.android.com/apk/res-auto"
33
>
44

55
<item
@@ -134,4 +134,10 @@
134134
android:title="JFAVoronoi"
135135
app:showAsAction="never"
136136
/>
137+
138+
<item
139+
android:id="@+id/filter21"
140+
android:title="BlackAndWhite"
141+
app:showAsAction="never"
142+
/>
137143
</menu>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
precision highp float;
2+
3+
uniform vec3 iResolution;
4+
uniform sampler2D iChannel0;
5+
varying vec2 texCoord;
6+
7+
void mainImage( out vec4 fragColor, in vec2 fragCoord )
8+
{
9+
vec4 mask = texture2D(iChannel0, fragCoord);
10+
float color = (mask.r + mask.g + mask.b) /3.0;
11+
vec4 tempColor =vec4(color, color, color,1.0);
12+
fragColor = tempColor;
13+
}
14+
15+
void main() {
16+
mainImage(gl_FragColor, texCoord);
17+
}

0 commit comments

Comments
 (0)