├── BeautyFilterDemo
├── .gitignore
├── .idea
│ ├── compiler.xml
│ ├── copyright
│ │ └── profiles_settings.xml
│ ├── gradle.xml
│ ├── misc.xml
│ ├── modules.xml
│ └── runConfigurations.xml
├── app
│ ├── .gitignore
│ ├── build.gradle
│ ├── proguard-rules.pro
│ └── src
│ │ ├── androidTest
│ │ └── java
│ │ │ └── com
│ │ │ └── nevaryyy
│ │ │ └── beautyfilterdemo
│ │ │ └── ExampleInstrumentedTest.java
│ │ ├── main
│ │ ├── AndroidManifest.xml
│ │ ├── assets
│ │ │ └── permission_en_zh.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── nevaryyy
│ │ │ │ └── beautyfilterdemo
│ │ │ │ ├── MyApplication.java
│ │ │ │ ├── base
│ │ │ │ ├── BaseActivity.java
│ │ │ │ └── IBaseView.java
│ │ │ │ ├── main
│ │ │ │ ├── GPUImageBeautyFilter.java
│ │ │ │ └── MainActivity.java
│ │ │ │ ├── permission
│ │ │ │ ├── PermissionCheck.java
│ │ │ │ ├── PermissionEnZh.java
│ │ │ │ └── PermissionEnZhXMLParserHandler.java
│ │ │ │ └── util
│ │ │ │ ├── DialogUtil.java
│ │ │ │ ├── LogUtil.java
│ │ │ │ └── StringUtil.java
│ │ └── res
│ │ │ ├── layout
│ │ │ └── activity_main.xml
│ │ │ ├── mipmap-hdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ ├── ic_launcher_round.png
│ │ │ ├── magic.png
│ │ │ └── switch_camera.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ │ └── values
│ │ │ ├── colors.xml
│ │ │ ├── dimens.xml
│ │ │ ├── strings.xml
│ │ │ └── styles.xml
│ │ └── test
│ │ └── java
│ │ └── com
│ │ └── nevaryyy
│ │ └── beautyfilterdemo
│ │ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
│ └── wrapper
│ │ ├── gradle-wrapper.jar
│ │ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
├── GPUImageBeautyFilter.java
└── README.md
/BeautyFilterDemo/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
17 |
18 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/.idea/misc.xml:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 | defaultConfig {
7 | applicationId "com.nevaryyy.beautyfilterdemo"
8 | minSdkVersion 21
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25 | exclude group: 'com.android.support', module: 'support-annotations'
26 | })
27 | compile 'com.android.support:appcompat-v7:25.2.0'
28 | testCompile 'junit:junit:4.12'
29 | compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.4.1'
30 | }
31 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/yuejinyang/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/androidTest/java/com/nevaryyy/beautyfilterdemo/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.nevaryyy.beautyfilterdemo;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.nevaryyy.beautyfilterdemo", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/assets/permission_en_zh.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/java/com/nevaryyy/beautyfilterdemo/MyApplication.java:
--------------------------------------------------------------------------------
1 | package com.nevaryyy.beautyfilterdemo;
2 |
3 | import android.app.Application;
4 |
5 | import com.nevaryyy.beautyfilterdemo.permission.PermissionEnZhXMLParserHandler;
6 |
7 | import java.io.InputStream;
8 |
9 | import javax.xml.parsers.SAXParser;
10 | import javax.xml.parsers.SAXParserFactory;
11 |
12 | /**
13 | * @author yuejinyang
14 | */
15 | public class MyApplication extends Application {
16 |
17 | @Override
18 | public void onCreate() {
19 | super.onCreate();
20 |
21 | loadPermissionEnZh();
22 | }
23 |
24 | private void loadPermissionEnZh() {
25 | try {
26 | InputStream inputStream = getAssets().open("permission_en_zh.xml");
27 | SAXParserFactory parserFactory = SAXParserFactory.newInstance();
28 | SAXParser parser = parserFactory.newSAXParser();
29 | PermissionEnZhXMLParserHandler parserHandler = new PermissionEnZhXMLParserHandler();
30 |
31 | parser.parse(inputStream, parserHandler);
32 | }
33 | catch (Exception e) {
34 | e.printStackTrace();
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/java/com/nevaryyy/beautyfilterdemo/base/BaseActivity.java:
--------------------------------------------------------------------------------
1 | package com.nevaryyy.beautyfilterdemo.base;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.content.pm.PackageManager;
6 | import android.net.Uri;
7 | import android.provider.Settings;
8 | import android.support.annotation.NonNull;
9 | import android.support.v4.app.ActivityCompat;
10 | import android.support.v4.content.ContextCompat;
11 | import android.support.v7.app.AppCompatActivity;
12 |
13 | import com.nevaryyy.beautyfilterdemo.R;
14 | import com.nevaryyy.beautyfilterdemo.permission.PermissionCheck;
15 | import com.nevaryyy.beautyfilterdemo.permission.PermissionEnZh;
16 | import com.nevaryyy.beautyfilterdemo.util.DialogUtil;
17 | import com.nevaryyy.beautyfilterdemo.util.StringUtil;
18 |
19 | import java.util.ArrayList;
20 | import java.util.List;
21 |
22 | /**
23 | * @author yuejinyang
24 | */
25 | public abstract class BaseActivity extends AppCompatActivity implements IBaseView {
26 |
27 | public static final int REQUEST_CODE_PERMISSION_READ_CONTACT = 0x80;
28 | public static final int REQUEST_CODE_PERMISSION_STORAGE = 0x81;
29 | public static final int REQUEST_CODE_PERMISSION_CALL = 0x82;
30 | public static final int REQUEST_CODE_PERMISSION_CAMERA = 0x83;
31 | public static final int REQUEST_CODE_PERMISSION_READ_PHONE_STATE = 0x84;
32 | public static final int REQUEST_CODE_DETAIL_SETTINGS = 0x85;
33 |
34 | protected PermissionCheck permissionCheck;
35 | protected boolean permissionChecking;
36 |
37 | @Override
38 | public Context getContext() {
39 | return this;
40 | }
41 |
42 | @Override
43 | public void openDetailSettings() {
44 | Uri packageUri = Uri.parse("package:" + getPackageName());
45 | Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageUri);
46 | startActivityForResult(intent, REQUEST_CODE_DETAIL_SETTINGS);
47 | }
48 |
49 | public void checkPermission(final PermissionCheck permissionCheck) {
50 | if (permissionChecking || permissionCheck == null) {
51 | return;
52 | }
53 |
54 | this.permissionCheck = permissionCheck;
55 | permissionChecking = true;
56 |
57 | final List permissionsDenied = new ArrayList<>();
58 | List permissionsNeededShow = new ArrayList<>();
59 |
60 | for (String permission : permissionCheck.getPermissions()) {
61 | if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
62 | permissionsDenied.add(permission);
63 | if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
64 | permissionsNeededShow.add(permission);
65 | }
66 | }
67 | }
68 |
69 | if (permissionsDenied.size() > 0) {
70 | if (permissionsNeededShow.size() > 0) {
71 | String message = getString(R.string.dialog_need_permission_title);
72 | for (String permissionNeededShow : permissionsNeededShow) {
73 | String permission = PermissionEnZh.getPermissionZh(permissionNeededShow);
74 | if (!StringUtil.isEmpty(permission)) {
75 | message = message + permission + getString(R.string.comma);
76 | }
77 | }
78 |
79 | message = message.substring(0, message.length() - getString(R.string.comma).length());
80 | message += getString(R.string.period);
81 |
82 | DialogUtil.showSimpleDialog(this, null, message,
83 | getString(R.string.grant), new DialogUtil.DialogCallback() {
84 | @Override
85 | public void callback() {
86 | ActivityCompat.requestPermissions(BaseActivity.this,
87 | permissionsDenied.toArray(new String[permissionsDenied.size()]),
88 | permissionCheck.getRequestCode());
89 | }
90 | }, getString(R.string.deny), new DialogUtil.DialogCallback() {
91 | @Override
92 | public void callback() {
93 | permissionCheck.deniedCallback();
94 | permissionChecking = false;
95 | }
96 | });
97 | }
98 | else {
99 | ActivityCompat.requestPermissions(BaseActivity.this,
100 | permissionsDenied.toArray(new String[permissionsDenied.size()]),
101 | permissionCheck.getRequestCode());
102 | }
103 |
104 | return;
105 | }
106 |
107 | permissionCheck.grantedCallback();
108 | permissionChecking = false;
109 | }
110 |
111 | @Override
112 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
113 | if (requestCode == REQUEST_CODE_PERMISSION_STORAGE ||
114 | requestCode == REQUEST_CODE_PERMISSION_READ_CONTACT ||
115 | requestCode == REQUEST_CODE_PERMISSION_CALL ||
116 | requestCode == REQUEST_CODE_PERMISSION_CAMERA ||
117 | requestCode == REQUEST_CODE_PERMISSION_READ_PHONE_STATE) {
118 |
119 | boolean granted = true;
120 | for (int result : grantResults) {
121 | if (result != PackageManager.PERMISSION_GRANTED) {
122 | granted = false;
123 | break;
124 | }
125 | }
126 | if (granted) {
127 | permissionCheck.grantedCallback();
128 | }
129 | else {
130 | permissionCheck.deniedCallback();
131 | }
132 |
133 | permissionChecking = false;
134 | }
135 | else {
136 | super.onRequestPermissionsResult(requestCode, permissions, grantResults);
137 | }
138 | }
139 |
140 | @Override
141 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
142 | if (requestCode == REQUEST_CODE_DETAIL_SETTINGS) {
143 | checkPermission(permissionCheck);
144 | }
145 | else {
146 | super.onActivityResult(requestCode, resultCode, data);
147 | }
148 | }
149 | }
150 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/java/com/nevaryyy/beautyfilterdemo/base/IBaseView.java:
--------------------------------------------------------------------------------
1 | package com.nevaryyy.beautyfilterdemo.base;
2 |
3 | import android.content.Context;
4 |
5 | /**
6 | * @author yuejinyang
7 | */
8 | public interface IBaseView {
9 | Context getContext();
10 | void openDetailSettings();
11 | }
12 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/java/com/nevaryyy/beautyfilterdemo/main/GPUImageBeautyFilter.java:
--------------------------------------------------------------------------------
1 | package com.nevaryyy.beautyfilterdemo.main;
2 |
3 | import android.opengl.GLES20;
4 |
5 | import jp.co.cyberagent.android.gpuimage.GPUImageFilter;
6 |
7 | /**
8 | * @author nevaryyy
9 | */
10 | public class GPUImageBeautyFilter extends GPUImageFilter {
11 | public static final String BILATERAL_FRAGMENT_SHADER = "" +
12 | " varying highp vec2 textureCoordinate;\n" +
13 | "\n" +
14 | " uniform sampler2D inputImageTexture;\n" +
15 | "\n" +
16 | " uniform highp vec2 singleStepOffset;\n" +
17 | " uniform highp vec4 params;\n" +
18 | " uniform highp float brightness;\n" +
19 | "\n" +
20 | " const highp vec3 W = vec3(0.299, 0.587, 0.114);\n" +
21 | " const highp mat3 saturateMatrix = mat3(\n" +
22 | " 1.1102, -0.0598, -0.061,\n" +
23 | " -0.0774, 1.0826, -0.1186,\n" +
24 | " -0.0228, -0.0228, 1.1772);\n" +
25 | " highp vec2 blurCoordinates[24];\n" +
26 | "\n" +
27 | " highp float hardLight(highp float color) {\n" +
28 | " if (color <= 0.5)\n" +
29 | " color = color * color * 2.0;\n" +
30 | " else\n" +
31 | " color = 1.0 - ((1.0 - color)*(1.0 - color) * 2.0);\n" +
32 | " return color;\n" +
33 | "}\n" +
34 | "\n" +
35 | " void main(){\n" +
36 | " highp vec3 centralColor = texture2D(inputImageTexture, textureCoordinate).rgb;\n" +
37 | " blurCoordinates[0] = textureCoordinate.xy + singleStepOffset * vec2(0.0, -10.0);\n" +
38 | " blurCoordinates[1] = textureCoordinate.xy + singleStepOffset * vec2(0.0, 10.0);\n" +
39 | " blurCoordinates[2] = textureCoordinate.xy + singleStepOffset * vec2(-10.0, 0.0);\n" +
40 | " blurCoordinates[3] = textureCoordinate.xy + singleStepOffset * vec2(10.0, 0.0);\n" +
41 | " blurCoordinates[4] = textureCoordinate.xy + singleStepOffset * vec2(5.0, -8.0);\n" +
42 | " blurCoordinates[5] = textureCoordinate.xy + singleStepOffset * vec2(5.0, 8.0);\n" +
43 | " blurCoordinates[6] = textureCoordinate.xy + singleStepOffset * vec2(-5.0, 8.0);\n" +
44 | " blurCoordinates[7] = textureCoordinate.xy + singleStepOffset * vec2(-5.0, -8.0);\n" +
45 | " blurCoordinates[8] = textureCoordinate.xy + singleStepOffset * vec2(8.0, -5.0);\n" +
46 | " blurCoordinates[9] = textureCoordinate.xy + singleStepOffset * vec2(8.0, 5.0);\n" +
47 | " blurCoordinates[10] = textureCoordinate.xy + singleStepOffset * vec2(-8.0, 5.0);\n" +
48 | " blurCoordinates[11] = textureCoordinate.xy + singleStepOffset * vec2(-8.0, -5.0);\n" +
49 | " blurCoordinates[12] = textureCoordinate.xy + singleStepOffset * vec2(0.0, -6.0);\n" +
50 | " blurCoordinates[13] = textureCoordinate.xy + singleStepOffset * vec2(0.0, 6.0);\n" +
51 | " blurCoordinates[14] = textureCoordinate.xy + singleStepOffset * vec2(6.0, 0.0);\n" +
52 | " blurCoordinates[15] = textureCoordinate.xy + singleStepOffset * vec2(-6.0, 0.0);\n" +
53 | " blurCoordinates[16] = textureCoordinate.xy + singleStepOffset * vec2(-4.0, -4.0);\n" +
54 | " blurCoordinates[17] = textureCoordinate.xy + singleStepOffset * vec2(-4.0, 4.0);\n" +
55 | " blurCoordinates[18] = textureCoordinate.xy + singleStepOffset * vec2(4.0, -4.0);\n" +
56 | " blurCoordinates[19] = textureCoordinate.xy + singleStepOffset * vec2(4.0, 4.0);\n" +
57 | " blurCoordinates[20] = textureCoordinate.xy + singleStepOffset * vec2(-2.0, -2.0);\n" +
58 | " blurCoordinates[21] = textureCoordinate.xy + singleStepOffset * vec2(-2.0, 2.0);\n" +
59 | " blurCoordinates[22] = textureCoordinate.xy + singleStepOffset * vec2(2.0, -2.0);\n" +
60 | " blurCoordinates[23] = textureCoordinate.xy + singleStepOffset * vec2(2.0, 2.0);\n" +
61 | "\n" +
62 | " highp float sampleColor = centralColor.g * 22.0;\n" +
63 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[0]).g;\n" +
64 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[1]).g;\n" +
65 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[2]).g;\n" +
66 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[3]).g;\n" +
67 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[4]).g;\n" +
68 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[5]).g;\n" +
69 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[6]).g;\n" +
70 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[7]).g;\n" +
71 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[8]).g;\n" +
72 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[9]).g;\n" +
73 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[10]).g;\n" +
74 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[11]).g;\n" +
75 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[12]).g * 2.0;\n" +
76 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[13]).g * 2.0;\n" +
77 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[14]).g * 2.0;\n" +
78 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[15]).g * 2.0;\n" +
79 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[16]).g * 2.0;\n" +
80 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[17]).g * 2.0;\n" +
81 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[18]).g * 2.0;\n" +
82 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[19]).g * 2.0;\n" +
83 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[20]).g * 3.0;\n" +
84 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[21]).g * 3.0;\n" +
85 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[22]).g * 3.0;\n" +
86 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[23]).g * 3.0;\n" +
87 | "\n" +
88 | " sampleColor = sampleColor / 62.0;\n" +
89 | "\n" +
90 | " highp float highPass = centralColor.g - sampleColor + 0.5;\n" +
91 | "\n" +
92 | " for (int i = 0; i < 5; i++) {\n" +
93 | " highPass = hardLight(highPass);\n" +
94 | " }\n" +
95 | " highp float lumance = dot(centralColor, W);\n" +
96 | "\n" +
97 | " highp float alpha = pow(lumance, params.r);\n" +
98 | "\n" +
99 | " highp vec3 smoothColor = centralColor + (centralColor-vec3(highPass))*alpha*0.1;\n" +
100 | "\n" +
101 | " smoothColor.r = clamp(pow(smoothColor.r, params.g), 0.0, 1.0);\n" +
102 | " smoothColor.g = clamp(pow(smoothColor.g, params.g), 0.0, 1.0);\n" +
103 | " smoothColor.b = clamp(pow(smoothColor.b, params.g), 0.0, 1.0);\n" +
104 | "\n" +
105 | " highp vec3 lvse = vec3(1.0)-(vec3(1.0)-smoothColor)*(vec3(1.0)-centralColor);\n" +
106 | " highp vec3 bianliang = max(smoothColor, centralColor);\n" +
107 | " highp vec3 rouguang = 2.0*centralColor*smoothColor + centralColor*centralColor - 2.0*centralColor*centralColor*smoothColor;\n" +
108 | "\n" +
109 | " gl_FragColor = vec4(mix(centralColor, lvse, alpha), 1.0);\n" +
110 | " gl_FragColor.rgb = mix(gl_FragColor.rgb, bianliang, alpha);\n" +
111 | " gl_FragColor.rgb = mix(gl_FragColor.rgb, rouguang, params.b);\n" +
112 | "\n" +
113 | " highp vec3 satcolor = gl_FragColor.rgb * saturateMatrix;\n" +
114 | " gl_FragColor.rgb = mix(gl_FragColor.rgb, satcolor, params.a);\n" +
115 | " gl_FragColor.rgb = vec3(gl_FragColor.rgb + vec3(brightness));\n" +
116 | "}";
117 |
118 | private float toneLevel;
119 | private float beautyLevel;
120 | private float brightLevel;
121 |
122 | private int paramsLocation;
123 | private int brightnessLocation;
124 | private int singleStepOffsetLocation;
125 |
126 | public GPUImageBeautyFilter() {
127 | super(NO_FILTER_VERTEX_SHADER, BILATERAL_FRAGMENT_SHADER);
128 | }
129 |
130 | @Override
131 | public void onInit() {
132 | super.onInit();
133 |
134 | paramsLocation = GLES20.glGetUniformLocation(getProgram(), "params");
135 | brightnessLocation = GLES20.glGetUniformLocation(getProgram(), "brightness");
136 | singleStepOffsetLocation = GLES20.glGetUniformLocation(getProgram(), "singleStepOffset");
137 |
138 | toneLevel = 0.47f;
139 | beautyLevel = 0.42f;
140 | brightLevel = 0.34f;
141 |
142 | setParams(beautyLevel, toneLevel);
143 | setBrightLevel(brightLevel);
144 | }
145 |
146 | public void setBeautyLevel(float beautyLevel) {
147 | this.beautyLevel = beautyLevel;
148 | setParams(beautyLevel, toneLevel);
149 | }
150 |
151 | public void setBrightLevel(float brightLevel) {
152 | this.brightLevel = brightLevel;
153 | setFloat(brightnessLocation, 0.6f * (-0.5f + brightLevel));
154 | }
155 |
156 | public void setParams(float beauty, float tone) {
157 | float[] vector = new float[4];
158 | vector[0] = 1.0f - 0.6f * beauty;
159 | vector[1] = 1.0f - 0.3f * beauty;
160 | vector[2] = 0.1f + 0.3f * tone;
161 | vector[3] = 0.1f + 0.3f * tone;
162 | setFloatVec4(paramsLocation, vector);
163 | }
164 |
165 | private void setTexelSize(final float w, final float h) {
166 | setFloatVec2(singleStepOffsetLocation, new float[] {2.0f / w, 2.0f / h});
167 | }
168 |
169 | @Override
170 | public void onOutputSizeChanged(final int width, final int height) {
171 | super.onOutputSizeChanged(width, height);
172 | setTexelSize(width, height);
173 | }
174 | }
175 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/java/com/nevaryyy/beautyfilterdemo/main/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.nevaryyy.beautyfilterdemo.main;
2 |
3 | import android.graphics.PixelFormat;
4 | import android.hardware.Camera;
5 | import android.opengl.GLSurfaceView;
6 | import android.os.Bundle;
7 | import android.view.View;
8 | import android.widget.ImageButton;
9 |
10 | import com.nevaryyy.beautyfilterdemo.R;
11 | import com.nevaryyy.beautyfilterdemo.base.BaseActivity;
12 | import com.nevaryyy.beautyfilterdemo.permission.PermissionCheck;
13 | import com.nevaryyy.beautyfilterdemo.util.DialogUtil;
14 | import com.nevaryyy.beautyfilterdemo.util.LogUtil;
15 |
16 | import java.util.List;
17 |
18 | import jp.co.cyberagent.android.gpuimage.GPUImage;
19 | import jp.co.cyberagent.android.gpuimage.GPUImageFilter;
20 | import jp.co.cyberagent.android.gpuimage.GPUImageFilterGroup;
21 |
22 | /**
23 | * @author yuejinyang
24 | */
25 | public class MainActivity extends BaseActivity {
26 |
27 | private static final int PICTURE_WIDTH = 1280;
28 | private static final int PICTURE_HEIGHT = 720;
29 |
30 | private static final int PREVIEW_WIDTH = 1280;
31 | private static final int PREVIEW_HEIGHT = 720;
32 |
33 | private GLSurfaceView glSurfaceView;
34 | private ImageButton magicImageButton;
35 | private ImageButton switchImageButton;
36 |
37 | private GPUImage gpuImage;
38 | private GPUImageFilterGroup magicFilterGroup;
39 | private GPUImageFilterGroup noMagicFilterGroup;
40 |
41 | private boolean isInMagic;
42 |
43 | private boolean cameraDenied;
44 |
45 | private boolean isPreviewing;
46 |
47 | private int currentCameraId;
48 |
49 | private Camera camera;
50 |
51 | @Override
52 | protected void onCreate(Bundle savedInstanceState) {
53 | super.onCreate(savedInstanceState);
54 | setContentView(R.layout.activity_main);
55 |
56 | glSurfaceView = (GLSurfaceView) findViewById(R.id.glsv_main);
57 | magicImageButton = (ImageButton) findViewById(R.id.ib_main_magic);
58 | switchImageButton = (ImageButton) findViewById(R.id.ib_main_switch);
59 |
60 | magicImageButton.setOnClickListener(new View.OnClickListener() {
61 | @Override
62 | public void onClick(View v) {
63 | isInMagic = !isInMagic;
64 | gpuImage.setFilter(isInMagic ? magicFilterGroup : noMagicFilterGroup);
65 | }
66 | });
67 |
68 | switchImageButton.setOnClickListener(new View.OnClickListener() {
69 | @Override
70 | public void onClick(View v) {
71 | switchCamera();
72 | }
73 | });
74 |
75 | init();
76 | }
77 |
78 | @Override
79 | protected void onPause() {
80 | super.onPause();
81 |
82 | clearCamera();
83 | }
84 |
85 | @Override
86 | protected void onResume() {
87 | super.onResume();
88 |
89 | if (!cameraDenied) {
90 | checkPermission(PermissionCheck.getCameraPermissionCheck(
91 | new PermissionCheck.OnPermissionChecked() {
92 | @Override
93 | public void callback() {
94 | initCamera(currentCameraId);
95 | }
96 | },
97 | new PermissionCheck.OnPermissionChecked() {
98 | @Override
99 | public void callback() {
100 | cameraDenied = true;
101 | DialogUtil.showNoPermissionDialog(MainActivity.this,
102 | getString(R.string.dialog_no_camera_permission_message), null);
103 | }
104 | }
105 | ));
106 | }
107 | }
108 |
109 | private void init() {
110 | isInMagic = true;
111 | isPreviewing = false;
112 | currentCameraId = 0;
113 |
114 | gpuImage = new GPUImage(this);
115 | gpuImage.setGLSurfaceView(glSurfaceView);
116 |
117 | magicFilterGroup = new GPUImageFilterGroup();
118 | magicFilterGroup.addFilter(new GPUImageBeautyFilter());
119 |
120 | noMagicFilterGroup = new GPUImageFilterGroup();
121 | noMagicFilterGroup.addFilter(new GPUImageFilter());
122 |
123 | gpuImage.setFilter(magicFilterGroup);
124 | }
125 |
126 | private void initCamera(int cameraId) {
127 | try {
128 | if (camera == null) {
129 | currentCameraId = cameraId;
130 | LogUtil.d(currentCameraId + "!");
131 | camera = Camera.open(cameraId);
132 | //camera.setDisplayOrientation(90);
133 |
134 | Camera.Parameters parameters = camera.getParameters();
135 | parameters.setPictureFormat(PixelFormat.JPEG);
136 | List sizeList = parameters.getSupportedPictureSizes();
137 | int width = 0;
138 | int height = 0;
139 |
140 | for (Camera.Size size : sizeList) {
141 | if (size.width * size.height <= PICTURE_HEIGHT * PICTURE_WIDTH) {
142 | if (size.width * size.height > width * height) {
143 | width = size.width;
144 | height = size.height;
145 | }
146 | }
147 | }
148 | parameters.setPictureSize(width, height);
149 | LogUtil.d("picture: " + width + " " + height);
150 |
151 | sizeList = parameters.getSupportedPreviewSizes();
152 | width = 0;
153 | height = 0;
154 | for (Camera.Size size : sizeList) {
155 | if (size.width * size.height <= PREVIEW_WIDTH * PREVIEW_HEIGHT) {
156 | if (size.width * size.height > width * height) {
157 | width = size.width;
158 | height = size.height;
159 | }
160 | }
161 | }
162 | parameters.setPreviewSize(width, height);
163 | LogUtil.d("preview: " + width + " " + height);
164 |
165 | List stringList = parameters.getSupportedFocusModes();
166 | for (String s : stringList) {
167 | LogUtil.d(s);
168 | }
169 | if (cameraId == 0) {
170 | parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
171 | }
172 |
173 | camera.setParameters(parameters);
174 |
175 | // try {
176 | // camera.setPreviewDisplay(surfaceHolder);
177 | // } catch (IOException e) {
178 | // e.printStackTrace();
179 | // return;
180 | // }
181 |
182 | if (!isPreviewing) {
183 | isPreviewing = true;
184 | //camera.startPreview();
185 | gpuImage.setUpCamera(camera, cameraId == 0 ? 90 : 270, cameraId > 0, false);
186 | }
187 | }
188 | }
189 | catch (Exception e) {
190 | DialogUtil.showNoPermissionDialog(MainActivity.this,
191 | getString(R.string.dialog_no_camera_permission_message), null);
192 | }
193 | }
194 |
195 | private void switchCamera() {
196 | clearCamera();
197 | checkPermission(PermissionCheck.getCameraPermissionCheck(
198 | new PermissionCheck.OnPermissionChecked() {
199 | @Override
200 | public void callback() {
201 | initCamera(currentCameraId ^ 1);
202 | }
203 | },
204 | new PermissionCheck.OnPermissionChecked() {
205 | @Override
206 | public void callback() {
207 | cameraDenied = true;
208 | DialogUtil.showNoPermissionDialog(MainActivity.this,
209 | getString(R.string.dialog_no_camera_permission_message), null);
210 | }
211 | }
212 | ));
213 | }
214 |
215 | private void clearCamera() {
216 | if (camera != null) {
217 | gpuImage.deleteImage();
218 | camera.setPreviewCallback(null);
219 | camera.stopPreview();
220 | isPreviewing = false;
221 | camera.release();
222 | camera = null;
223 | }
224 | }
225 | }
226 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/java/com/nevaryyy/beautyfilterdemo/permission/PermissionCheck.java:
--------------------------------------------------------------------------------
1 | package com.nevaryyy.beautyfilterdemo.permission;
2 |
3 | import com.nevaryyy.beautyfilterdemo.base.BaseActivity;
4 |
5 | /**
6 | * @author yuejinyang
7 | */
8 | public class PermissionCheck {
9 |
10 | private String[] permissions;
11 | private int requestCode;
12 | private OnPermissionChecked permissionGranted;
13 | private OnPermissionChecked permissionDenied;
14 |
15 | public PermissionCheck(String[] permissions,
16 | int requestCode,
17 | OnPermissionChecked permissionGranted,
18 | OnPermissionChecked permissionDenied) {
19 | this.permissions = permissions;
20 | this.requestCode = requestCode;
21 | this.permissionGranted = permissionGranted;
22 | this.permissionDenied = permissionDenied;
23 | }
24 |
25 | public String[] getPermissions() {
26 | return permissions;
27 | }
28 |
29 | public int getRequestCode() {
30 | return requestCode;
31 | }
32 |
33 | public void grantedCallback() {
34 | if (permissionGranted != null) {
35 | permissionGranted.callback();
36 | }
37 | }
38 |
39 | public void deniedCallback() {
40 | if (permissionDenied != null) {
41 | permissionDenied.callback();
42 | }
43 | }
44 |
45 | public interface OnPermissionChecked {
46 | void callback();
47 | }
48 |
49 | public static PermissionCheck getCameraPermissionCheck
50 | (OnPermissionChecked permissionGranted, OnPermissionChecked permissionDenied) {
51 | return new PermissionCheck(
52 | new String[]{"android.permission.CAMERA"},
53 | BaseActivity.REQUEST_CODE_PERMISSION_CAMERA,
54 | permissionGranted, permissionDenied);
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/java/com/nevaryyy/beautyfilterdemo/permission/PermissionEnZh.java:
--------------------------------------------------------------------------------
1 | package com.nevaryyy.beautyfilterdemo.permission;
2 |
3 | import java.util.Map;
4 |
5 | /**
6 | * @author nevaryyy
7 | */
8 | public class PermissionEnZh {
9 | public static String VERSION;
10 | public static String AUTHOR;
11 | public static Map EN_ZH;
12 |
13 | public static String getPermissionZh(String permissionEn) {
14 | if (EN_ZH == null) {
15 | return permissionEn;
16 | }
17 | if (EN_ZH.containsKey(permissionEn)) {
18 | return EN_ZH.get(permissionEn);
19 | }
20 | else {
21 | return permissionEn;
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/java/com/nevaryyy/beautyfilterdemo/permission/PermissionEnZhXMLParserHandler.java:
--------------------------------------------------------------------------------
1 | package com.nevaryyy.beautyfilterdemo.permission;
2 |
3 | import org.xml.sax.Attributes;
4 | import org.xml.sax.SAXException;
5 | import org.xml.sax.helpers.DefaultHandler;
6 |
7 | import java.util.HashMap;
8 |
9 | /**
10 | * @author nevaryyy
11 | */
12 | public class PermissionEnZhXMLParserHandler extends DefaultHandler {
13 | @Override
14 | public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
15 | if (qName.equals("about")) {
16 | PermissionEnZh.VERSION = attributes.getValue(0);
17 | PermissionEnZh.AUTHOR = attributes.getValue(1);
18 | }
19 | else if (qName.equals("permissions")) {
20 | PermissionEnZh.EN_ZH = new HashMap<>();
21 | }
22 | else if (qName.equals("permission")) {
23 | PermissionEnZh.EN_ZH.put(attributes.getValue(0), attributes.getValue(1));
24 | }
25 | }
26 |
27 | @Override
28 | public void endElement(String uri, String localName, String qName) throws SAXException {
29 | super.endElement(uri, localName, qName);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/java/com/nevaryyy/beautyfilterdemo/util/DialogUtil.java:
--------------------------------------------------------------------------------
1 | package com.nevaryyy.beautyfilterdemo.util;
2 |
3 | import android.app.AlertDialog;
4 | import android.content.Context;
5 | import android.content.DialogInterface;
6 |
7 | import com.nevaryyy.beautyfilterdemo.R;
8 | import com.nevaryyy.beautyfilterdemo.base.IBaseView;
9 |
10 | /**
11 | * @author yuejinyang
12 | */
13 | public class DialogUtil {
14 |
15 | public static void showSimpleDialog(Context context, String title, String message,
16 | String positiveText, final DialogCallback positiveCallback,
17 | String negativeText, final DialogCallback negativeCallback) {
18 | new AlertDialog.Builder(context)
19 | .setTitle(title)
20 | .setMessage(message)
21 | .setCancelable(false)
22 | .setPositiveButton(positiveText, new DialogInterface.OnClickListener() {
23 | @Override
24 | public void onClick(DialogInterface dialog, int which) {
25 | if (positiveCallback != null) {
26 | positiveCallback.callback();
27 | }
28 | dialog.dismiss();
29 | }
30 | })
31 | .setNegativeButton(negativeText, new DialogInterface.OnClickListener() {
32 | @Override
33 | public void onClick(DialogInterface dialog, int which) {
34 | if (negativeCallback != null) {
35 | negativeCallback.callback();
36 | }
37 | dialog.dismiss();
38 | }
39 | })
40 | .create()
41 | .show();
42 | }
43 |
44 | public static void showNoPermissionDialog(final IBaseView baseView, String message, DialogCallback cancelCallback) {
45 | showSimpleDialog(baseView.getContext(),
46 | baseView.getContext().getString(R.string.dialog_no_permission_title),
47 | message,
48 | baseView.getContext().getString(R.string.goto_settings), new DialogCallback() {
49 | @Override
50 | public void callback() {
51 | baseView.openDetailSettings();
52 | }
53 | }, baseView.getContext().getString(R.string.cancel), cancelCallback);
54 | }
55 |
56 | public interface DialogCallback {
57 | void callback();
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/java/com/nevaryyy/beautyfilterdemo/util/LogUtil.java:
--------------------------------------------------------------------------------
1 | package com.nevaryyy.beautyfilterdemo.util;
2 |
3 | import android.util.Log;
4 |
5 | /**
6 | * @author nevaryyy
7 | */
8 | public class LogUtil {
9 |
10 | public static final int VERBOSE = 1;
11 | public static final int DEBUG = 2;
12 | public static final int INFO = 3;
13 | public static final int WARN = 4;
14 | public static final int ERROR = 5;
15 | public static final int NOTHING = 6;
16 |
17 | public static int LEVEL = VERBOSE;
18 |
19 | public static final String SEPARATOR = ",";
20 |
21 | public static void v(String tag, String message) {
22 | if (LEVEL <= VERBOSE) {
23 | StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
24 | if (StringUtil.isEmpty(tag)) {
25 | tag = getDefaultTag(stackTraceElement);
26 | }
27 | Log.v(tag, getLogInfo(stackTraceElement) + message);
28 | }
29 | }
30 |
31 | public static void d(String message) {
32 | if (LEVEL <= DEBUG) {
33 | StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
34 | String tag = getDefaultTag(stackTraceElement);
35 | Log.d(tag, getLogInfo(stackTraceElement) + message);
36 | }
37 | }
38 |
39 | public static void d(String tag, String message) {
40 | if (LEVEL <= DEBUG) {
41 | StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
42 | if (StringUtil.isEmpty(tag)) {
43 | tag = getDefaultTag(stackTraceElement);
44 | }
45 | Log.d(tag, getLogInfo(stackTraceElement) + message);
46 | }
47 | }
48 |
49 | public static void i(String message) {
50 | if (LEVEL <= INFO) {
51 | StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
52 | String tag = getDefaultTag(stackTraceElement);
53 | Log.i(tag, getLogInfo(stackTraceElement) + message);
54 | }
55 | }
56 |
57 | public static void i(String tag, String message) {
58 | if (LEVEL <= INFO) {
59 | StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
60 | if (StringUtil.isEmpty(tag)) {
61 | tag = getDefaultTag(stackTraceElement);
62 | }
63 | Log.i(tag, getLogInfo(stackTraceElement) + message);
64 | }
65 | }
66 |
67 | public static void w(String message) {
68 | if (LEVEL <= WARN) {
69 | StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
70 | String tag = getDefaultTag(stackTraceElement);
71 | Log.w(tag, getLogInfo(stackTraceElement) + message);
72 | }
73 | }
74 |
75 | public static void w(String tag, String message) {
76 | if (LEVEL <= WARN) {
77 | StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
78 | if (StringUtil.isEmpty(tag)) {
79 | tag = getDefaultTag(stackTraceElement);
80 | }
81 | Log.w(tag, getLogInfo(stackTraceElement) + message);
82 | }
83 | }
84 |
85 | public static void e(String message) {
86 | if (LEVEL <= WARN) {
87 | StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
88 | String tag = getDefaultTag(stackTraceElement);
89 | Log.e(tag, getLogInfo(stackTraceElement) + message);
90 | }
91 | }
92 |
93 | public static void e(String tag, String message) {
94 | if (LEVEL <= ERROR) {
95 | StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
96 | if (StringUtil.isEmpty(tag)) {
97 | tag = getDefaultTag(stackTraceElement);
98 | }
99 | Log.e(tag, getLogInfo(stackTraceElement) + message);
100 | }
101 | }
102 |
103 | public static String getDefaultTag(StackTraceElement stackTraceElement) {
104 | String fileName = stackTraceElement.getFileName();
105 | String stringArray[] = fileName.split("\\.");
106 | String tag = stringArray[0];
107 | return tag;
108 | }
109 |
110 | public static String getLogInfo(StackTraceElement stackTraceElement) {
111 | StringBuilder logInfoStringBuilder = new StringBuilder();
112 |
113 | String threadName = Thread.currentThread().getName();
114 | long threadID = Thread.currentThread().getId();
115 | String fileName = stackTraceElement.getFileName();
116 | String className = stackTraceElement.getClassName();
117 | String methodName = stackTraceElement.getMethodName();
118 | int lineNumber = stackTraceElement.getLineNumber();
119 |
120 | logInfoStringBuilder.append("[ ");
121 | logInfoStringBuilder.append("threadID=" + threadID).append(SEPARATOR);
122 | logInfoStringBuilder.append("threadName=" + threadName).append(SEPARATOR);
123 | logInfoStringBuilder.append("fileName=" + fileName).append(SEPARATOR);
124 | logInfoStringBuilder.append("className=" + className).append(SEPARATOR);
125 | logInfoStringBuilder.append("methodName=" + methodName).append(SEPARATOR);
126 | logInfoStringBuilder.append("lineNumber=" + lineNumber);
127 | logInfoStringBuilder.append(" ] ");
128 |
129 | return logInfoStringBuilder.toString();
130 | }
131 | }
132 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/java/com/nevaryyy/beautyfilterdemo/util/StringUtil.java:
--------------------------------------------------------------------------------
1 | package com.nevaryyy.beautyfilterdemo.util;
2 |
3 | /**
4 | * Created by yuejinyang on 2017/3/30.
5 | */
6 |
7 | public class StringUtil {
8 | public static boolean isEmpty(String string) {
9 | return string == null || string.equals("");
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
24 |
25 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nevaryyy/Android-GPUImageBeautyFilter/ff2850c762acffce19b2ab6ac14981ace0e82bc8/BeautyFilterDemo/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nevaryyy/Android-GPUImageBeautyFilter/ff2850c762acffce19b2ab6ac14981ace0e82bc8/BeautyFilterDemo/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nevaryyy/Android-GPUImageBeautyFilter/ff2850c762acffce19b2ab6ac14981ace0e82bc8/BeautyFilterDemo/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nevaryyy/Android-GPUImageBeautyFilter/ff2850c762acffce19b2ab6ac14981ace0e82bc8/BeautyFilterDemo/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nevaryyy/Android-GPUImageBeautyFilter/ff2850c762acffce19b2ab6ac14981ace0e82bc8/BeautyFilterDemo/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nevaryyy/Android-GPUImageBeautyFilter/ff2850c762acffce19b2ab6ac14981ace0e82bc8/BeautyFilterDemo/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nevaryyy/Android-GPUImageBeautyFilter/ff2850c762acffce19b2ab6ac14981ace0e82bc8/BeautyFilterDemo/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nevaryyy/Android-GPUImageBeautyFilter/ff2850c762acffce19b2ab6ac14981ace0e82bc8/BeautyFilterDemo/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/res/mipmap-xxhdpi/magic.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nevaryyy/Android-GPUImageBeautyFilter/ff2850c762acffce19b2ab6ac14981ace0e82bc8/BeautyFilterDemo/app/src/main/res/mipmap-xxhdpi/magic.png
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/res/mipmap-xxhdpi/switch_camera.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nevaryyy/Android-GPUImageBeautyFilter/ff2850c762acffce19b2ab6ac14981ace0e82bc8/BeautyFilterDemo/app/src/main/res/mipmap-xxhdpi/switch_camera.png
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nevaryyy/Android-GPUImageBeautyFilter/ff2850c762acffce19b2ab6ac14981ace0e82bc8/BeautyFilterDemo/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nevaryyy/Android-GPUImageBeautyFilter/ff2850c762acffce19b2ab6ac14981ace0e82bc8/BeautyFilterDemo/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 16dp
5 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | BeautyFilterDemo
3 |
4 | ,
5 | 。
6 |
7 | 无法获取您的摄像头数据。请到“设置-应用-权限”里开启摄像头权限,以正常使用摄像头功能。
8 | 无权限
9 | 您需要以下权限:
10 | 允许
11 | 拒绝
12 | 去设置
13 | 取消
14 |
15 |
16 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/app/src/test/java/com/nevaryyy/beautyfilterdemo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.nevaryyy.beautyfilterdemo;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/BeautyFilterDemo/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.3.0'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nevaryyy/Android-GPUImageBeautyFilter/ff2850c762acffce19b2ab6ac14981ace0e82bc8/BeautyFilterDemo/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/BeautyFilterDemo/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu Mar 30 16:37:44 CST 2017
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
7 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/BeautyFilterDemo/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/GPUImageBeautyFilter.java:
--------------------------------------------------------------------------------
1 | package gpuimage;
2 |
3 | import android.opengl.GLES20;
4 |
5 | import jp.co.cyberagent.android.gpuimage.GPUImageFilter;
6 |
7 | /**
8 | * @author nevaryyy
9 | */
10 | public class GPUImageBeautyFilter extends GPUImageFilter {
11 | public static final String BILATERAL_FRAGMENT_SHADER = "" +
12 | " varying highp vec2 textureCoordinate;\n" +
13 | "\n" +
14 | " uniform sampler2D inputImageTexture;\n" +
15 | "\n" +
16 | " uniform highp vec2 singleStepOffset;\n" +
17 | " uniform highp vec4 params;\n" +
18 | " uniform highp float brightness;\n" +
19 | "\n" +
20 | " const highp vec3 W = vec3(0.299, 0.587, 0.114);\n" +
21 | " const highp mat3 saturateMatrix = mat3(\n" +
22 | " 1.1102, -0.0598, -0.061,\n" +
23 | " -0.0774, 1.0826, -0.1186,\n" +
24 | " -0.0228, -0.0228, 1.1772);\n" +
25 | " highp vec2 blurCoordinates[24];\n" +
26 | "\n" +
27 | " highp float hardLight(highp float color) {\n" +
28 | " if (color <= 0.5)\n" +
29 | " color = color * color * 2.0;\n" +
30 | " else\n" +
31 | " color = 1.0 - ((1.0 - color)*(1.0 - color) * 2.0);\n" +
32 | " return color;\n" +
33 | "}\n" +
34 | "\n" +
35 | " void main(){\n" +
36 | " highp vec3 centralColor = texture2D(inputImageTexture, textureCoordinate).rgb;\n" +
37 | " blurCoordinates[0] = textureCoordinate.xy + singleStepOffset * vec2(0.0, -10.0);\n" +
38 | " blurCoordinates[1] = textureCoordinate.xy + singleStepOffset * vec2(0.0, 10.0);\n" +
39 | " blurCoordinates[2] = textureCoordinate.xy + singleStepOffset * vec2(-10.0, 0.0);\n" +
40 | " blurCoordinates[3] = textureCoordinate.xy + singleStepOffset * vec2(10.0, 0.0);\n" +
41 | " blurCoordinates[4] = textureCoordinate.xy + singleStepOffset * vec2(5.0, -8.0);\n" +
42 | " blurCoordinates[5] = textureCoordinate.xy + singleStepOffset * vec2(5.0, 8.0);\n" +
43 | " blurCoordinates[6] = textureCoordinate.xy + singleStepOffset * vec2(-5.0, 8.0);\n" +
44 | " blurCoordinates[7] = textureCoordinate.xy + singleStepOffset * vec2(-5.0, -8.0);\n" +
45 | " blurCoordinates[8] = textureCoordinate.xy + singleStepOffset * vec2(8.0, -5.0);\n" +
46 | " blurCoordinates[9] = textureCoordinate.xy + singleStepOffset * vec2(8.0, 5.0);\n" +
47 | " blurCoordinates[10] = textureCoordinate.xy + singleStepOffset * vec2(-8.0, 5.0);\n" +
48 | " blurCoordinates[11] = textureCoordinate.xy + singleStepOffset * vec2(-8.0, -5.0);\n" +
49 | " blurCoordinates[12] = textureCoordinate.xy + singleStepOffset * vec2(0.0, -6.0);\n" +
50 | " blurCoordinates[13] = textureCoordinate.xy + singleStepOffset * vec2(0.0, 6.0);\n" +
51 | " blurCoordinates[14] = textureCoordinate.xy + singleStepOffset * vec2(6.0, 0.0);\n" +
52 | " blurCoordinates[15] = textureCoordinate.xy + singleStepOffset * vec2(-6.0, 0.0);\n" +
53 | " blurCoordinates[16] = textureCoordinate.xy + singleStepOffset * vec2(-4.0, -4.0);\n" +
54 | " blurCoordinates[17] = textureCoordinate.xy + singleStepOffset * vec2(-4.0, 4.0);\n" +
55 | " blurCoordinates[18] = textureCoordinate.xy + singleStepOffset * vec2(4.0, -4.0);\n" +
56 | " blurCoordinates[19] = textureCoordinate.xy + singleStepOffset * vec2(4.0, 4.0);\n" +
57 | " blurCoordinates[20] = textureCoordinate.xy + singleStepOffset * vec2(-2.0, -2.0);\n" +
58 | " blurCoordinates[21] = textureCoordinate.xy + singleStepOffset * vec2(-2.0, 2.0);\n" +
59 | " blurCoordinates[22] = textureCoordinate.xy + singleStepOffset * vec2(2.0, -2.0);\n" +
60 | " blurCoordinates[23] = textureCoordinate.xy + singleStepOffset * vec2(2.0, 2.0);\n" +
61 | "\n" +
62 | " highp float sampleColor = centralColor.g * 22.0;\n" +
63 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[0]).g;\n" +
64 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[1]).g;\n" +
65 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[2]).g;\n" +
66 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[3]).g;\n" +
67 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[4]).g;\n" +
68 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[5]).g;\n" +
69 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[6]).g;\n" +
70 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[7]).g;\n" +
71 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[8]).g;\n" +
72 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[9]).g;\n" +
73 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[10]).g;\n" +
74 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[11]).g;\n" +
75 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[12]).g * 2.0;\n" +
76 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[13]).g * 2.0;\n" +
77 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[14]).g * 2.0;\n" +
78 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[15]).g * 2.0;\n" +
79 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[16]).g * 2.0;\n" +
80 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[17]).g * 2.0;\n" +
81 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[18]).g * 2.0;\n" +
82 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[19]).g * 2.0;\n" +
83 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[20]).g * 3.0;\n" +
84 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[21]).g * 3.0;\n" +
85 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[22]).g * 3.0;\n" +
86 | " sampleColor += texture2D(inputImageTexture, blurCoordinates[23]).g * 3.0;\n" +
87 | "\n" +
88 | " sampleColor = sampleColor / 62.0;\n" +
89 | "\n" +
90 | " highp float highPass = centralColor.g - sampleColor + 0.5;\n" +
91 | "\n" +
92 | " for (int i = 0; i < 5; i++) {\n" +
93 | " highPass = hardLight(highPass);\n" +
94 | " }\n" +
95 | " highp float lumance = dot(centralColor, W);\n" +
96 | "\n" +
97 | " highp float alpha = pow(lumance, params.r);\n" +
98 | "\n" +
99 | " highp vec3 smoothColor = centralColor + (centralColor-vec3(highPass))*alpha*0.1;\n" +
100 | "\n" +
101 | " smoothColor.r = clamp(pow(smoothColor.r, params.g), 0.0, 1.0);\n" +
102 | " smoothColor.g = clamp(pow(smoothColor.g, params.g), 0.0, 1.0);\n" +
103 | " smoothColor.b = clamp(pow(smoothColor.b, params.g), 0.0, 1.0);\n" +
104 | "\n" +
105 | " highp vec3 lvse = vec3(1.0)-(vec3(1.0)-smoothColor)*(vec3(1.0)-centralColor);\n" +
106 | " highp vec3 bianliang = max(smoothColor, centralColor);\n" +
107 | " highp vec3 rouguang = 2.0*centralColor*smoothColor + centralColor*centralColor - 2.0*centralColor*centralColor*smoothColor;\n" +
108 | "\n" +
109 | " gl_FragColor = vec4(mix(centralColor, lvse, alpha), 1.0);\n" +
110 | " gl_FragColor.rgb = mix(gl_FragColor.rgb, bianliang, alpha);\n" +
111 | " gl_FragColor.rgb = mix(gl_FragColor.rgb, rouguang, params.b);\n" +
112 | "\n" +
113 | " highp vec3 satcolor = gl_FragColor.rgb * saturateMatrix;\n" +
114 | " gl_FragColor.rgb = mix(gl_FragColor.rgb, satcolor, params.a);\n" +
115 | " gl_FragColor.rgb = vec3(gl_FragColor.rgb + vec3(brightness));\n" +
116 | "}";
117 |
118 | private float toneLevel;
119 | private float beautyLevel;
120 | private float brightLevel;
121 |
122 | private int paramsLocation;
123 | private int brightnessLocation;
124 | private int singleStepOffsetLocation;
125 |
126 | public GPUImageBeautyFilter() {
127 | super(NO_FILTER_VERTEX_SHADER, BILATERAL_FRAGMENT_SHADER);
128 | }
129 |
130 | @Override
131 | public void onInit() {
132 | super.onInit();
133 |
134 | paramsLocation = GLES20.glGetUniformLocation(getProgram(), "params");
135 | brightnessLocation = GLES20.glGetUniformLocation(getProgram(), "brightness");
136 | singleStepOffsetLocation = GLES20.glGetUniformLocation(getProgram(), "singleStepOffset");
137 |
138 | toneLevel = 0.47f;
139 | beautyLevel = 0.42f;
140 | brightLevel = 0.34f;
141 |
142 | setParams(beautyLevel, toneLevel);
143 | setBrightLevel(brightLevel);
144 | }
145 |
146 | public void setBeautyLevel(float beautyLevel) {
147 | this.beautyLevel = beautyLevel;
148 | setParams(beautyLevel, toneLevel);
149 | }
150 |
151 | public void setBrightLevel(float brightLevel) {
152 | this.brightLevel = brightLevel;
153 | setFloat(brightnessLocation, 0.6f * (-0.5f + brightLevel));
154 | }
155 |
156 | public void setParams(float beauty, float tone) {
157 | float[] vector = new float[4];
158 | vector[0] = 1.0f - 0.6f * beauty;
159 | vector[1] = 1.0f - 0.3f * beauty;
160 | vector[2] = 0.1f + 0.3f * tone;
161 | vector[3] = 0.1f + 0.3f * tone;
162 | setFloatVec4(paramsLocation, vector);
163 | }
164 |
165 | private void setTexelSize(final float w, final float h) {
166 | setFloatVec2(singleStepOffsetLocation, new float[] {2.0f / w, 2.0f / h});
167 | }
168 |
169 | @Override
170 | public void onOutputSizeChanged(final int width, final int height) {
171 | super.onOutputSizeChanged(width, height);
172 | setTexelSize(width, height);
173 | }
174 | }
175 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Android-GPUImageBeautyFilter
2 | 基于Android-GPUImage的美颜filter
3 | ## Demo
4 | Demo演示了如何通过GPUImage和BeautyFilter来实现美颜滤镜。主要实现代码在main包里,其余代码是做权限获取的,这部分你可以使用自己的代码。
5 | ## 感谢
6 | [OC版的美颜filter](https://github.com/Maru-zhang/MARFaceBeauty)
7 |
8 | [Android-GPUImage](https://github.com/CyberAgent/android-gpuimage)
9 |
--------------------------------------------------------------------------------