Skip to content

Commit f3dd152

Browse files
author
xufuji456
committed
Feature: add CameraFilter with OpenGL
1 parent f6e7030 commit f3dd152

27 files changed

Lines changed: 2094 additions & 0 deletions

CameraFilter/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/build
2+
/.cxx

CameraFilter/build.gradle

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
plugins {
2+
id 'com.android.library'
3+
}
4+
5+
android {
6+
compileSdkVersion rootProject.ext.compileSdkVersion
7+
8+
defaultConfig {
9+
minSdkVersion rootProject.ext.minSdkVersion
10+
targetSdkVersion rootProject.ext.targetSdkVersion
11+
}
12+
13+
buildTypes {
14+
release {
15+
minifyEnabled false
16+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
17+
}
18+
}
19+
20+
}
21+
22+
dependencies {
23+
24+
implementation "androidx.appcompat:appcompat:$rootProject.appcompatVersion"
25+
26+
}

CameraFilter/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.frank.camerafilter">
4+
5+
<uses-feature android:glEsVersion="0x00030000" android:required="true"/>
6+
7+
</manifest>
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.frank.camerafilter.camera;
2+
3+
import android.graphics.SurfaceTexture;
4+
import android.hardware.Camera;
5+
import android.hardware.Camera.Parameters;
6+
7+
import java.io.IOException;
8+
import java.util.List;
9+
10+
/**
11+
* @author xufulong
12+
* @date 2022/6/17 5:14 下午
13+
* @desc
14+
*/
15+
public class CameraManager {
16+
17+
private Camera mCamera;
18+
private int mCameraId = 0;
19+
private SurfaceTexture mSurfaceTexture;
20+
21+
public Camera getCamera() {
22+
return mCamera;
23+
}
24+
25+
public boolean openCamera() {
26+
return openCamera(mCameraId);
27+
}
28+
29+
public boolean openCamera(int cameraId) {
30+
if (mCamera == null) {
31+
try {
32+
mCameraId = cameraId;
33+
mCamera = Camera.open(cameraId);
34+
setDefaultParams();
35+
return true;
36+
} catch (RuntimeException e) {
37+
return false;
38+
}
39+
}
40+
return false;
41+
}
42+
43+
public void releaseCamera() {
44+
if (mCamera == null)
45+
return;
46+
stopPreview();
47+
mCamera.release();
48+
mCamera = null;
49+
}
50+
51+
public void switchCamera() {
52+
if (mCameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
53+
mCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
54+
} else {
55+
mCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
56+
}
57+
releaseCamera();
58+
openCamera(mCameraId);
59+
startPreview(mSurfaceTexture);
60+
}
61+
62+
private static Camera.Size getLargePictureSize(Camera camera){
63+
if(camera != null){
64+
List<Camera.Size> sizes = camera.getParameters().getSupportedPictureSizes();
65+
Camera.Size temp = sizes.get(0);
66+
for(int i = 1;i < sizes.size();i ++){
67+
float scale = (float)(sizes.get(i).height) / sizes.get(i).width;
68+
if(temp.width < sizes.get(i).width && scale < 0.6f && scale > 0.5f)
69+
temp = sizes.get(i);
70+
}
71+
return temp;
72+
}
73+
return null;
74+
}
75+
76+
private static Camera.Size getLargePreviewSize(Camera camera){
77+
if(camera != null){
78+
List<Camera.Size> sizes = camera.getParameters().getSupportedPreviewSizes();
79+
Camera.Size temp = sizes.get(0);
80+
for(int i = 1;i < sizes.size();i ++){
81+
if(temp.width < sizes.get(i).width)
82+
temp = sizes.get(i);
83+
}
84+
return temp;
85+
}
86+
return null;
87+
}
88+
89+
public void setDefaultParams() {
90+
Parameters parameters = mCamera.getParameters();
91+
if (parameters.getSupportedFocusModes().contains(
92+
Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
93+
parameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
94+
}
95+
Camera.Size previewSize = getLargePreviewSize(mCamera);
96+
parameters.setPreviewSize(previewSize.width, previewSize.height);
97+
Camera.Size pictureSize = getLargePictureSize(mCamera);
98+
parameters.setPictureSize(pictureSize.width, pictureSize.height);
99+
parameters.setRotation(90);
100+
mCamera.setParameters(parameters);
101+
}
102+
103+
public void startPreview(SurfaceTexture surfaceTexture) {
104+
if (mCamera == null)
105+
return;
106+
try {
107+
mCamera.setPreviewTexture(surfaceTexture);
108+
mSurfaceTexture = surfaceTexture;
109+
mCamera.startPreview();
110+
} catch (IOException e) {
111+
e.printStackTrace();
112+
}
113+
}
114+
115+
public void stopPreview() {
116+
if (mCamera == null)
117+
return;
118+
mCamera.setPreviewCallback(null);
119+
mCamera.stopPreview();
120+
}
121+
122+
public int getOrientation() {
123+
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
124+
Camera.getCameraInfo(mCameraId, cameraInfo);
125+
return cameraInfo.orientation;
126+
}
127+
128+
public Camera.Size getPreviewSize() {
129+
return mCamera.getParameters().getPreviewSize();
130+
}
131+
132+
public boolean isFront() {
133+
return mCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT;
134+
}
135+
136+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.frank.camerafilter.factory;
2+
3+
import android.content.Context;
4+
5+
import com.frank.camerafilter.filter.advance.BeautyCrayonFilter;
6+
import com.frank.camerafilter.filter.advance.BeautySketchFilter;
7+
import com.frank.camerafilter.filter.BaseFilter;
8+
9+
public class BeautyFilterFactory {
10+
11+
private static BeautyFilterType filterType = BeautyFilterType.NONE;
12+
13+
public static BaseFilter getFilter(BeautyFilterType type, Context context) {
14+
filterType = type;
15+
switch (type) {
16+
case SKETCH:
17+
return new BeautySketchFilter(context);
18+
case CRAYON:
19+
return new BeautyCrayonFilter(context);
20+
default:
21+
return null;
22+
}
23+
}
24+
25+
public static BeautyFilterType getFilterType() {
26+
return filterType;
27+
}
28+
29+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.frank.camerafilter.factory;
2+
3+
public enum BeautyFilterType {
4+
NONE,
5+
CRAYON,
6+
SKETCH
7+
}

0 commit comments

Comments
 (0)