├── .gitignore ├── .idea ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .editorconfig ├── .gitignore ├── CMakeLists.txt ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── huangqi │ │ └── offscreentest │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── cpp │ │ └── native-lib.cpp │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── huangqi │ │ │ └── offscreentest │ │ │ ├── CameraInstance.java │ │ │ ├── GLRenderer.java │ │ │ ├── GLSurface.java │ │ │ ├── GLThread.java │ │ │ ├── MainActivity.java │ │ │ └── TestGLSurface.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.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 │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── example │ └── huangqi │ └── offscreentest │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.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 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GLMultiCameraPreviewDemo 2 | -------------------------------------------------------------------------------- /app/.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | max_line_length = off 13 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # For more information about using CMake with Android Studio, read the 2 | # documentation: https://d.android.com/studio/projects/add-native-code.html 3 | 4 | # Sets the minimum version of CMake required to build the native library. 5 | 6 | cmake_minimum_required(VERSION 3.4.1) 7 | 8 | # Creates and names a library, sets it as either STATIC 9 | # or SHARED, and provides the relative paths to its source code. 10 | # You can define multiple libraries, and CMake builds them for you. 11 | # Gradle automatically packages shared libraries with your APK. 12 | 13 | add_library( # Sets the name of the library. 14 | native-lib 15 | 16 | # Sets the library as a shared library. 17 | SHARED 18 | 19 | # Provides a relative path to your source file(s). 20 | src/main/cpp/native-lib.cpp ) 21 | 22 | # Searches for a specified prebuilt library and stores the path as a 23 | # variable. Because CMake includes system libraries in the search path by 24 | # default, you only need to specify the name of the public NDK library 25 | # you want to add. CMake verifies that the library exists before 26 | # completing its build. 27 | 28 | find_library( # Sets the name of the path variable. 29 | log-lib 30 | 31 | # Specifies the name of the NDK library that 32 | # you want CMake to locate. 33 | log ) 34 | 35 | # Specifies libraries CMake should link to your target library. You 36 | # can link multiple libraries, such as libraries you define in this 37 | # build script, prebuilt third-party libraries, or system libraries. 38 | 39 | target_link_libraries( # Specifies the target library. 40 | native-lib 41 | 42 | # Links the target library to the log library 43 | # included in the NDK. 44 | ${log-lib} ) -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 26 5 | defaultConfig { 6 | applicationId "com.example.huangqi.offscreentest" 7 | minSdkVersion 17 8 | targetSdkVersion 26 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | externalNativeBuild { 13 | cmake { 14 | cppFlags "-std=c++11" 15 | } 16 | } 17 | } 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | externalNativeBuild { 25 | cmake { 26 | path "CMakeLists.txt" 27 | } 28 | } 29 | } 30 | 31 | dependencies { 32 | implementation fileTree(dir: 'libs', include: ['*.jar']) 33 | implementation 'com.android.support:appcompat-v7:26.1.0' 34 | implementation 'com.android.support.constraint:constraint-layout:1.0.2' 35 | 36 | implementation 'org.wysaid:gpuimage-plus:2.4.6-min' 37 | 38 | testImplementation 'junit:junit:4.12' 39 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 40 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 41 | } 42 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 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 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/example/huangqi/offscreentest/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.example.huangqi.offscreentest; 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 | * Instrumented 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.example.huangqi.offscreentest", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /app/src/main/cpp/native-lib.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | extern "C" 5 | JNIEXPORT jstring 6 | 7 | JNICALL 8 | Java_com_example_huangqi_offscreentest_MainActivity_stringFromJNI( 9 | JNIEnv *env, 10 | jobject /* this */) { 11 | std::string hello = "Hello from C++"; 12 | return env->NewStringUTF(hello.c_str()); 13 | } 14 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/huangqi/offscreentest/CameraInstance.java: -------------------------------------------------------------------------------- 1 | package com.example.huangqi.offscreentest; 2 | 3 | import android.graphics.PixelFormat; 4 | import android.graphics.Rect; 5 | import android.graphics.SurfaceTexture; 6 | import android.hardware.Camera; 7 | import android.os.Build; 8 | import android.util.Log; 9 | 10 | import java.io.IOException; 11 | import java.util.ArrayList; 12 | import java.util.Collections; 13 | import java.util.Comparator; 14 | import java.util.List; 15 | 16 | /** 17 | * Created by huangqi on 2017/12/12. 18 | */ 19 | 20 | 21 | // Camera 仅适用单例 22 | public class CameraInstance { 23 | public static final String LOG_TAG = "CameraInstance"; 24 | 25 | private static final String ASSERT_MSG = "检测到CameraDevice 为 null! 请检查"; 26 | 27 | private Camera mCameraDevice; 28 | private Camera.Parameters mParams; 29 | 30 | public static final int DEFAULT_PREVIEW_RATE = 30; 31 | 32 | 33 | private boolean mIsPreviewing = false; 34 | 35 | private int mDefaultCameraID = -1; 36 | 37 | private static CameraInstance mThisInstance; 38 | private int mPreviewWidth; 39 | private int mPreviewHeight; 40 | 41 | private int mPictureWidth = 1000; 42 | private int mPictureHeight = 1000; 43 | 44 | private int mPreferPreviewWidth = 640; 45 | private int mPreferPreviewHeight = 640; 46 | 47 | private int mFacing = 0; 48 | 49 | private CameraInstance() {} 50 | 51 | public static synchronized CameraInstance getInstance() { 52 | if(mThisInstance == null) { 53 | mThisInstance = new CameraInstance(); 54 | } 55 | return mThisInstance; 56 | } 57 | 58 | public boolean isPreviewing() { return mIsPreviewing; } 59 | 60 | public int previewWidth() { return mPreviewWidth; } 61 | public int previewHeight() { return mPreviewHeight; } 62 | public int pictureWidth() { return mPictureWidth; } 63 | public int pictureHeight() { return mPictureHeight; } 64 | 65 | public void setPreferPreviewSize(int w, int h) { 66 | mPreferPreviewHeight = w; 67 | mPreferPreviewWidth = h; 68 | } 69 | 70 | public interface CameraOpenCallback { 71 | void cameraReady(); 72 | } 73 | 74 | public boolean tryOpenCamera(CameraOpenCallback callback) { 75 | return tryOpenCamera(callback, Camera.CameraInfo.CAMERA_FACING_BACK); 76 | } 77 | 78 | public int getFacing() { 79 | return mFacing; 80 | } 81 | 82 | public synchronized boolean tryOpenCamera(CameraOpenCallback callback, int facing) { 83 | Log.i(LOG_TAG, "try open camera..."); 84 | 85 | try 86 | { 87 | if(Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) 88 | { 89 | int numberOfCameras = Camera.getNumberOfCameras(); 90 | 91 | Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); 92 | for (int i = 0; i < numberOfCameras; i++) { 93 | Camera.getCameraInfo(i, cameraInfo); 94 | if (cameraInfo.facing == facing) { 95 | mDefaultCameraID = i; 96 | mFacing = facing; 97 | } 98 | } 99 | } 100 | stopPreview(); 101 | if(mCameraDevice != null) 102 | mCameraDevice.release(); 103 | 104 | if(mDefaultCameraID >= 0) { 105 | mCameraDevice = Camera.open(mDefaultCameraID); 106 | } 107 | else { 108 | mCameraDevice = Camera.open(); 109 | mFacing = Camera.CameraInfo.CAMERA_FACING_BACK; //default: back facing 110 | } 111 | } 112 | catch(Exception e) 113 | { 114 | Log.e(LOG_TAG, "Open Camera Failed!"); 115 | e.printStackTrace(); 116 | mCameraDevice = null; 117 | return false; 118 | } 119 | 120 | if(mCameraDevice != null) { 121 | Log.i(LOG_TAG, "Camera opened!"); 122 | 123 | try { 124 | initCamera(DEFAULT_PREVIEW_RATE); 125 | } catch (Exception e) { 126 | mCameraDevice.release(); 127 | mCameraDevice = null; 128 | return false; 129 | } 130 | 131 | if (callback != null) { 132 | callback.cameraReady(); 133 | } 134 | 135 | return true; 136 | } 137 | 138 | return false; 139 | } 140 | 141 | public synchronized void stopCamera() { 142 | if(mCameraDevice != null) { 143 | mIsPreviewing = false; 144 | mCameraDevice.stopPreview(); 145 | mCameraDevice.setPreviewCallback(null); 146 | mCameraDevice.release(); 147 | mCameraDevice = null; 148 | } 149 | } 150 | 151 | public boolean isCameraOpened() { 152 | return mCameraDevice != null; 153 | } 154 | 155 | public synchronized void startPreview(SurfaceTexture texture) { 156 | Log.i(LOG_TAG, "Camera startPreview..."); 157 | if(mIsPreviewing) { 158 | Log.e(LOG_TAG, "Err: camera is previewing..."); 159 | // stopPreview(); 160 | return ; 161 | } 162 | 163 | if(mCameraDevice != null) { 164 | try { 165 | mCameraDevice.setPreviewTexture(texture); 166 | } catch (IOException e) { 167 | e.printStackTrace(); 168 | } 169 | 170 | mCameraDevice.startPreview(); 171 | mIsPreviewing = true; 172 | } 173 | } 174 | 175 | public synchronized void stopPreview() { 176 | if(mIsPreviewing && mCameraDevice != null) { 177 | Log.i(LOG_TAG, "Camera stopPreview..."); 178 | mIsPreviewing = false; 179 | mCameraDevice.stopPreview(); 180 | } 181 | } 182 | 183 | public synchronized Camera.Parameters getParams() { 184 | if(mCameraDevice != null) 185 | return mCameraDevice.getParameters(); 186 | assert mCameraDevice != null : ASSERT_MSG; 187 | return null; 188 | } 189 | 190 | public synchronized void setParams(Camera.Parameters param) { 191 | if(mCameraDevice != null) { 192 | mParams = param; 193 | mCameraDevice.setParameters(mParams); 194 | } 195 | assert mCameraDevice != null : ASSERT_MSG; 196 | } 197 | 198 | public Camera getCameraDevice() { 199 | return mCameraDevice; 200 | } 201 | 202 | //保证从大到小排列 203 | private Comparator comparatorBigger = new Comparator() { 204 | @Override 205 | public int compare(Camera.Size lhs, Camera.Size rhs) { 206 | int w = rhs.width - lhs.width; 207 | if(w == 0) 208 | return rhs.height - lhs.height; 209 | return w; 210 | } 211 | }; 212 | 213 | //保证从小到大排列 214 | private Comparator comparatorSmaller= new Comparator() { 215 | @Override 216 | public int compare(Camera.Size lhs, Camera.Size rhs) { 217 | int w = lhs.width - rhs.width; 218 | if(w == 0) 219 | return lhs.height - rhs.height; 220 | return w; 221 | } 222 | }; 223 | 224 | public void initCamera(int previewRate) { 225 | if(mCameraDevice == null) { 226 | Log.e(LOG_TAG, "initCamera: Camera is not opened!"); 227 | return; 228 | } 229 | 230 | mParams = mCameraDevice.getParameters(); 231 | List supportedPictureFormats = mParams.getSupportedPictureFormats(); 232 | 233 | for(int fmt : supportedPictureFormats) { 234 | Log.i(LOG_TAG, String.format("Picture Format: %x", fmt)); 235 | } 236 | 237 | mParams.setPictureFormat(PixelFormat.JPEG); 238 | 239 | List picSizes = mParams.getSupportedPictureSizes(); 240 | Camera.Size picSz = null; 241 | 242 | Collections.sort(picSizes, comparatorBigger); 243 | 244 | for(Camera.Size sz : picSizes) { 245 | Log.i(LOG_TAG, String.format("Supported picture size: %d x %d", sz.width, sz.height)); 246 | if(picSz == null || (sz.width >= mPictureWidth && sz.height >= mPictureHeight)) { 247 | picSz = sz; 248 | } 249 | } 250 | 251 | List prevSizes = mParams.getSupportedPreviewSizes(); 252 | Camera.Size prevSz = null; 253 | 254 | Collections.sort(prevSizes, comparatorBigger); 255 | 256 | for(Camera.Size sz : prevSizes) { 257 | Log.i(LOG_TAG, String.format("Supported preview size: %d x %d", sz.width, sz.height)); 258 | if(prevSz == null || (sz.width >= mPreferPreviewWidth && sz.height >= mPreferPreviewHeight)) { 259 | prevSz = sz; 260 | } 261 | } 262 | 263 | List frameRates = mParams.getSupportedPreviewFrameRates(); 264 | 265 | int fpsMax = 0; 266 | 267 | for(Integer n : frameRates) { 268 | Log.i(LOG_TAG, "Supported frame rate: " + n); 269 | if(fpsMax < n) { 270 | fpsMax = n; 271 | } 272 | } 273 | 274 | mParams.setPreviewSize(prevSz.width, prevSz.height); 275 | mParams.setPictureSize(picSz.width, picSz.height); 276 | 277 | List focusModes = mParams.getSupportedFocusModes(); 278 | if(focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)){ 279 | mParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); 280 | } 281 | 282 | previewRate = fpsMax; 283 | mParams.setPreviewFrameRate(previewRate); //设置相机预览帧率 284 | // mParams.setPreviewFpsRange(20, 60); 285 | 286 | try { 287 | mCameraDevice.setParameters(mParams); 288 | }catch (Exception e) { 289 | e.printStackTrace(); 290 | } 291 | 292 | 293 | mParams = mCameraDevice.getParameters(); 294 | 295 | Camera.Size szPic = mParams.getPictureSize(); 296 | Camera.Size szPrev = mParams.getPreviewSize(); 297 | 298 | mPreviewWidth = szPrev.width; 299 | mPreviewHeight = szPrev.height; 300 | 301 | mPictureWidth = szPic.width; 302 | mPictureHeight = szPic.height; 303 | 304 | Log.i(LOG_TAG, String.format("Camera Picture Size: %d x %d", szPic.width, szPic.height)); 305 | Log.i(LOG_TAG, String.format("Camera Preview Size: %d x %d", szPrev.width, szPrev.height)); 306 | } 307 | 308 | public synchronized void setFocusMode(String focusMode) { 309 | 310 | if(mCameraDevice == null) 311 | return; 312 | 313 | mParams = mCameraDevice.getParameters(); 314 | List focusModes = mParams.getSupportedFocusModes(); 315 | if(focusModes.contains(focusMode)){ 316 | mParams.setFocusMode(focusMode); 317 | } 318 | } 319 | 320 | public synchronized void setPictureSize(int width, int height, boolean isBigger) { 321 | 322 | if(mCameraDevice == null) { 323 | mPictureWidth = width; 324 | mPictureHeight = height; 325 | return; 326 | } 327 | 328 | mParams = mCameraDevice.getParameters(); 329 | 330 | 331 | List picSizes = mParams.getSupportedPictureSizes(); 332 | Camera.Size picSz = null; 333 | 334 | if(isBigger) { 335 | Collections.sort(picSizes, comparatorBigger); 336 | for(Camera.Size sz : picSizes) { 337 | if(picSz == null || (sz.width >= width && sz.height >= height)) { 338 | picSz = sz; 339 | } 340 | } 341 | } else { 342 | Collections.sort(picSizes, comparatorSmaller); 343 | for(Camera.Size sz : picSizes) { 344 | if(picSz == null || (sz.width <= width && sz.height <= height)) { 345 | picSz = sz; 346 | } 347 | } 348 | } 349 | 350 | mPictureWidth = picSz.width; 351 | mPictureHeight= picSz.height; 352 | 353 | try { 354 | mParams.setPictureSize(mPictureWidth, mPictureHeight); 355 | mCameraDevice.setParameters(mParams); 356 | } catch (Exception e) { 357 | e.printStackTrace(); 358 | } 359 | } 360 | 361 | public void focusAtPoint(float x, float y, final Camera.AutoFocusCallback callback) { 362 | focusAtPoint(x, y, 0.2f, callback); 363 | } 364 | 365 | public synchronized void focusAtPoint(float x, float y, float radius, final Camera.AutoFocusCallback callback) { 366 | if(mCameraDevice == null) { 367 | Log.e(LOG_TAG, "Error: focus after release."); 368 | return; 369 | } 370 | 371 | mParams = mCameraDevice.getParameters(); 372 | 373 | if(mParams.getMaxNumMeteringAreas() > 0) { 374 | 375 | int focusRadius = (int) (radius * 1000.0f); 376 | int left = (int) (x * 2000.0f - 1000.0f) - focusRadius; 377 | int top = (int) (y * 2000.0f - 1000.0f) - focusRadius; 378 | 379 | Rect focusArea = new Rect(); 380 | focusArea.left = Math.max(left, -1000); 381 | focusArea.top = Math.max(top, -1000); 382 | focusArea.right = Math.min(left + focusRadius, 1000); 383 | focusArea.bottom = Math.min(top + focusRadius, 1000); 384 | List meteringAreas = new ArrayList(); 385 | meteringAreas.add(new Camera.Area(focusArea, 800)); 386 | 387 | try { 388 | mCameraDevice.cancelAutoFocus(); 389 | mParams.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); 390 | mParams.setFocusAreas(meteringAreas); 391 | mCameraDevice.setParameters(mParams); 392 | mCameraDevice.autoFocus(callback); 393 | } catch (Exception e) { 394 | Log.e(LOG_TAG, "Error: focusAtPoint failed: " + e.toString()); 395 | } 396 | } else { 397 | Log.i(LOG_TAG, "The device does not support metering areas..."); 398 | try { 399 | mCameraDevice.autoFocus(callback); 400 | } catch (Exception e) { 401 | Log.e(LOG_TAG, "Error: focusAtPoint failed: " + e.toString()); 402 | } 403 | } 404 | 405 | } 406 | } -------------------------------------------------------------------------------- /app/src/main/java/com/example/huangqi/offscreentest/GLRenderer.java: -------------------------------------------------------------------------------- 1 | package com.example.huangqi.offscreentest; 2 | 3 | import android.graphics.SurfaceTexture; 4 | import android.hardware.Camera; 5 | import android.opengl.EGL14; 6 | import android.opengl.GLES11Ext; 7 | import android.opengl.GLES20; 8 | import android.util.Log; 9 | import android.view.Surface; 10 | import android.view.SurfaceView; 11 | 12 | import org.wysaid.common.Common; 13 | import org.wysaid.nativePort.CGEFrameRenderer; 14 | import org.wysaid.texUtils.TextureRenderer; 15 | import org.wysaid.texUtils.TextureRendererDrawOrigin; 16 | 17 | import java.nio.IntBuffer; 18 | 19 | /** 20 | * Created by huangqi on 2017/12/12. 21 | */ 22 | 23 | public class GLRenderer extends GLThread implements SurfaceTexture.OnFrameAvailableListener { 24 | public static final String LOG_TAG = "GLRenderer"; 25 | 26 | private int mTextureID; 27 | private SurfaceTexture mSurfaceTexture; 28 | private float[] mTransformMatrix = new float[16]; 29 | 30 | private CGEFrameRenderer mRenderer; 31 | 32 | public CameraInstance cameraInstance() { 33 | return CameraInstance.getInstance(); 34 | } 35 | 36 | public void startPreview() { 37 | postRunnable(new Runnable() { 38 | @Override 39 | public void run() { 40 | if (!cameraInstance().isCameraOpened()) { 41 | 42 | int facing = Camera.CameraInfo.CAMERA_FACING_FRONT; 43 | 44 | cameraInstance().tryOpenCamera(new CameraInstance.CameraOpenCallback() { 45 | @Override 46 | public void cameraReady() { 47 | Log.i(LOG_TAG, "tryOpenCamera OK..."); 48 | } 49 | }, facing); 50 | } 51 | 52 | if (!cameraInstance().isPreviewing()) { 53 | cameraInstance().startPreview(mSurfaceTexture); 54 | mRenderer.srcResize(cameraInstance().previewHeight(), cameraInstance().previewWidth()); 55 | } 56 | 57 | requestRender(); 58 | } 59 | }); 60 | } 61 | 62 | public void stopPreview() { 63 | cameraInstance().stopPreview(); 64 | requestRender(); 65 | } 66 | 67 | public void setFilter(final String config) { 68 | postRunnable(new Runnable() { 69 | @Override 70 | public void run() { 71 | if (mRenderer != null) { 72 | mRenderer.setFilterWidthConfig(config); 73 | } 74 | } 75 | }); 76 | } 77 | 78 | @Override 79 | public void onCreated() { 80 | GLES20.glDisable(GLES20.GL_DEPTH_TEST); 81 | GLES20.glDisable(GLES20.GL_STENCIL_TEST); 82 | GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); 83 | 84 | int texSize[] = new int[1]; 85 | 86 | GLES20.glGetIntegerv(GLES20.GL_MAX_TEXTURE_SIZE, texSize, 0); 87 | 88 | mTextureID = Common.genSurfaceTextureID(); 89 | mSurfaceTexture = new SurfaceTexture(mTextureID); 90 | mSurfaceTexture.setOnFrameAvailableListener(this); 91 | 92 | // FIXME: make temp offscreen surface 93 | GLSurface temp = new GLSurface(500, 500); 94 | makeOutputSurface(temp); 95 | EGL14.eglMakeCurrent(eglDisplay, temp.eglSurface, temp.eglSurface, eglContext); 96 | 97 | mRenderer = new CGEFrameRenderer(); 98 | mRenderer.init(500, 500, 500, 500); 99 | 100 | mRenderer.setSrcRotation((float) (Math.PI / 2.0)); 101 | mRenderer.setSrcFlipScale(1.0f, -1.0f); 102 | mRenderer.setRenderFlipScale(1.0f, -1.0f); 103 | 104 | } 105 | 106 | @Override 107 | public void onUpdate() { 108 | // 将相机图像流转为GL外部纹理 109 | mSurfaceTexture.updateTexImage(); 110 | mSurfaceTexture.getTransformMatrix(mTransformMatrix); 111 | 112 | mRenderer.update(mTextureID, mTransformMatrix); 113 | mRenderer.runProc(); 114 | 115 | GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0); 116 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); 117 | } 118 | 119 | @Override 120 | public void onDestroy() { 121 | mRenderer.release(); 122 | mRenderer = null; 123 | GLES20.glDeleteTextures(1, new int[]{mTextureID}, 0); 124 | mTextureID = 0; 125 | mSurfaceTexture.release(); 126 | mSurfaceTexture = null; 127 | } 128 | 129 | @Override 130 | public void onDrawFrame(GLSurface outputSurface) { 131 | GLSurface.Viewport viewport = outputSurface.getViewport(); 132 | mRenderer.render(viewport.x, viewport.y, viewport.width, viewport.height); 133 | // TODO: draw to view 134 | // TODO: add filter 135 | } 136 | 137 | @Override 138 | public void onFrameAvailable(SurfaceTexture surfaceTexture) { 139 | this.requestRender(); 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/huangqi/offscreentest/GLSurface.java: -------------------------------------------------------------------------------- 1 | package com.example.huangqi.offscreentest; 2 | 3 | import android.opengl.EGL14; 4 | import android.opengl.EGLSurface; 5 | import android.view.Surface; 6 | 7 | /** 8 | * Created by huangqi on 2017/12/12. 9 | */ 10 | 11 | public class GLSurface { 12 | public static final int TYPE_WINDOW_SURFACE = 0; 13 | public static final int TYPE_PBUFFER_SURFACE = 1; 14 | public static final int TYPE_PIXMAP_SURFACE = 2; 15 | 16 | protected final int type; 17 | protected Object surface; // 显示控件(支持SurfaceView、SurfaceHolder、Surface和SurfaceTexture) 18 | protected EGLSurface eglSurface = EGL14.EGL_NO_SURFACE; 19 | protected Viewport viewport = new Viewport(); 20 | 21 | 22 | public GLSurface(int width, int height) { 23 | setViewport(0, 0, width, height); 24 | surface = null; 25 | type = TYPE_PBUFFER_SURFACE; 26 | } 27 | 28 | public GLSurface(Surface surface, int width, int height) { 29 | this(surface, 0, 0, width, height); 30 | } 31 | 32 | public GLSurface(Surface surface, int x, int y, int width, int height) { 33 | setViewport(x, y, width, height); 34 | this.surface = surface; 35 | type = TYPE_WINDOW_SURFACE; 36 | } 37 | 38 | public void setViewport(int x, int y, int width, int height) { 39 | viewport.x = x; 40 | viewport.y = y; 41 | viewport.width = width; 42 | viewport.height = height; 43 | } 44 | 45 | public void setViewport(Viewport viewport) { 46 | this.viewport = viewport; 47 | } 48 | 49 | public Viewport getViewport() { 50 | return viewport; 51 | } 52 | 53 | public static class Viewport { 54 | public int x; 55 | public int y; 56 | public int width; 57 | public int height; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/huangqi/offscreentest/GLThread.java: -------------------------------------------------------------------------------- 1 | package com.example.huangqi.offscreentest; 2 | 3 | import android.opengl.EGL14; 4 | import android.opengl.EGLConfig; 5 | import android.opengl.EGLContext; 6 | import android.opengl.EGLDisplay; 7 | import android.opengl.GLES20; 8 | import android.opengl.GLUtils; 9 | import android.support.annotation.NonNull; 10 | import android.util.Log; 11 | 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | import java.util.concurrent.ArrayBlockingQueue; 15 | 16 | import static android.opengl.EGL14.EGL_NO_SURFACE; 17 | 18 | /** 19 | * Created by huangqi on 2017/12/11. 20 | */ 21 | 22 | 23 | public abstract class GLThread extends Thread { 24 | private static final String TAG = "GLThread"; 25 | private EGLConfig eglConfig = null; 26 | protected EGLDisplay eglDisplay = EGL14.EGL_NO_DISPLAY; 27 | protected EGLContext eglContext = EGL14.EGL_NO_CONTEXT; 28 | 29 | private ArrayBlockingQueue eventQueue; 30 | private final List outputSurfaces; 31 | private boolean rendering; 32 | private boolean isRelease; 33 | 34 | public GLThread() { 35 | setName("GLRenderer-" + getId()); 36 | outputSurfaces = new ArrayList<>(); 37 | rendering = false; 38 | isRelease = false; 39 | 40 | eventQueue = new ArrayBlockingQueue<>(100); 41 | } 42 | 43 | protected boolean makeOutputSurface(GLSurface surface) { 44 | // 创建Surface缓存 45 | try { 46 | switch (surface.type) { 47 | case GLSurface.TYPE_WINDOW_SURFACE: { 48 | final int[] attributes = {EGL14.EGL_NONE}; 49 | // 创建失败时返回EGL14.EGL_NO_SURFACE 50 | surface.eglSurface = EGL14.eglCreateWindowSurface(eglDisplay, eglConfig, surface.surface, attributes, 0); 51 | break; 52 | } 53 | case GLSurface.TYPE_PBUFFER_SURFACE: { 54 | final int[] attributes = { 55 | EGL14.EGL_WIDTH, surface.viewport.width, 56 | EGL14.EGL_HEIGHT, surface.viewport.height, 57 | EGL14.EGL_NONE}; 58 | // 创建失败时返回EGL14.EGL_NO_SURFACE 59 | surface.eglSurface = EGL14.eglCreatePbufferSurface(eglDisplay, eglConfig, attributes, 0); 60 | break; 61 | } 62 | case GLSurface.TYPE_PIXMAP_SURFACE: { 63 | Log.w(TAG, "nonsupport pixmap surface"); 64 | return false; 65 | } 66 | default: 67 | Log.w(TAG, "surface type error " + surface.type); 68 | return false; 69 | } 70 | } catch (Exception e) { 71 | Log.w(TAG, "can't create eglSurface"); 72 | surface.eglSurface = EGL_NO_SURFACE; 73 | return false; 74 | } 75 | 76 | return true; 77 | } 78 | 79 | public void addSurface(@NonNull final GLSurface surface) { 80 | Event event = new Event(Event.ADD_SURFACE); 81 | event.param = surface; 82 | if (!eventQueue.offer(event)) 83 | Log.e(TAG, "queue full"); 84 | } 85 | 86 | public void removeSurface(@NonNull final GLSurface surface) { 87 | Event event = new Event(Event.REMOVE_SURFACE); 88 | event.param = surface; 89 | if (!eventQueue.offer(event)) 90 | Log.e(TAG, "queue full"); 91 | } 92 | 93 | /** 94 | * 开始渲染 95 | * 启动线程并等待初始化完毕 96 | */ 97 | public void startRender() { 98 | if (!eventQueue.offer(new Event(Event.START_RENDER))) 99 | Log.e(TAG, "queue full"); 100 | if (getState() == State.NEW) { 101 | super.start(); // 启动渲染线程 102 | } 103 | } 104 | 105 | public void stopRender() { 106 | if (!eventQueue.offer(new Event(Event.STOP_RENDER))) 107 | Log.e(TAG, "queue full"); 108 | } 109 | 110 | public boolean postRunnable(@NonNull Runnable runnable) { 111 | Event event = new Event(Event.RUNNABLE); 112 | event.param = runnable; 113 | if (!eventQueue.offer(event)) { 114 | Log.e(TAG, "queue full"); 115 | return false; 116 | } 117 | 118 | return true; 119 | } 120 | 121 | /** 122 | * 123 | */ 124 | @Override 125 | public void start() { 126 | Log.w(TAG, "Don't call this function"); 127 | } 128 | 129 | public void requestRender() { 130 | eventQueue.offer(new Event(Event.REQ_RENDER)); 131 | } 132 | 133 | /** 134 | * 创建OpenGL环境 135 | */ 136 | private void createGL() { 137 | // 获取显示设备(默认的显示设备) 138 | eglDisplay = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY); 139 | // 初始化 140 | int[] version = new int[2]; 141 | if (!EGL14.eglInitialize(eglDisplay, version, 0, version, 1)) { 142 | throw new RuntimeException("EGL error " + EGL14.eglGetError()); 143 | } 144 | // 获取FrameBuffer格式和能力 145 | int[] configAttribs = { 146 | EGL14.EGL_BUFFER_SIZE, 32, 147 | EGL14.EGL_RED_SIZE, 8, 148 | EGL14.EGL_GREEN_SIZE, 8, 149 | EGL14.EGL_BLUE_SIZE, 8, 150 | EGL14.EGL_ALPHA_SIZE, 8, 151 | // EGL14.EGL_DEPTH_SIZE, 8, 152 | // EGL14.EGL_STENCIL_SIZE, 0, 153 | EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT, 154 | EGL14.EGL_SURFACE_TYPE, EGL14.EGL_WINDOW_BIT, 155 | EGL14.EGL_NONE 156 | }; 157 | int[] numConfigs = new int[1]; 158 | EGLConfig[] configs = new EGLConfig[1]; 159 | if (!EGL14.eglChooseConfig(eglDisplay, configAttribs, 0, configs, 0, configs.length, numConfigs, 0)) { 160 | throw new RuntimeException("EGL error " + EGL14.eglGetError()); 161 | } 162 | eglConfig = configs[0]; 163 | // 创建OpenGL上下文(可以先不设置EGLSurface,但EGLContext必须创建, 164 | // 因为后面调用GLES方法基本都要依赖于EGLContext) 165 | int[] contextAttribs = { 166 | EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, 167 | EGL14.EGL_NONE 168 | }; 169 | eglContext = EGL14.eglCreateContext(eglDisplay, eglConfig, EGL14.EGL_NO_CONTEXT, contextAttribs, 0); 170 | if (eglContext == EGL14.EGL_NO_CONTEXT) { 171 | throw new RuntimeException("EGL error " + EGL14.eglGetError()); 172 | } 173 | // 设置默认的上下文环境和输出缓冲区(小米4上如果不设置有效的eglSurface后面创建着色器会失败,可以先创建一个默认的eglSurface) 174 | // EGL14.eglMakeCurrent(eglDisplay, surface.eglSurface, surface.eglSurface, eglContext); 175 | EGL14.eglMakeCurrent(eglDisplay, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, eglContext); 176 | } 177 | 178 | /** 179 | * 销毁OpenGL环境 180 | */ 181 | private void destroyGL() { 182 | EGL14.eglDestroyContext(eglDisplay, eglContext); 183 | eglContext = EGL14.EGL_NO_CONTEXT; 184 | eglDisplay = EGL14.EGL_NO_DISPLAY; 185 | } 186 | 187 | /** 188 | * 渲染到各个eglSurface 189 | */ 190 | private void render() { 191 | // 渲染(绘制) 192 | for (GLSurface output : outputSurfaces) { 193 | if (output.eglSurface == EGL_NO_SURFACE) { 194 | if (!makeOutputSurface(output)) 195 | continue; 196 | } 197 | // 设置当前的上下文环境和输出缓冲区 198 | EGL14.eglMakeCurrent(eglDisplay, output.eglSurface, output.eglSurface, eglContext); 199 | // 设置视窗大小及位置 200 | GLES20.glViewport(output.viewport.x, output.viewport.y, output.viewport.width, output.viewport.height); 201 | // 绘制 202 | onDrawFrame(output); 203 | // 交换显存(将surface显存和显示器的显存交换) 204 | EGL14.eglSwapBuffers(eglDisplay, output.eglSurface); 205 | } 206 | } 207 | 208 | @Override 209 | public void run() { 210 | Event event; 211 | 212 | Log.d(TAG, getName() + ": render create"); 213 | createGL(); 214 | onCreated(); 215 | // 渲染 216 | while (!isRelease) { 217 | try { 218 | event = eventQueue.take(); 219 | switch (event.event) { 220 | case Event.ADD_SURFACE: { 221 | // 创建eglSurface 222 | GLSurface surface = (GLSurface) event.param; 223 | Log.d(TAG, "add:" + surface); 224 | makeOutputSurface(surface); 225 | outputSurfaces.add(surface); 226 | break; 227 | } 228 | case Event.REMOVE_SURFACE: { 229 | GLSurface surface = (GLSurface) event.param; 230 | Log.d(TAG, "remove:" + surface); 231 | EGL14.eglDestroySurface(eglDisplay, surface.eglSurface); 232 | outputSurfaces.remove(surface); 233 | 234 | break; 235 | } 236 | case Event.START_RENDER: 237 | rendering = true; 238 | break; 239 | case Event.REQ_RENDER: // 渲染 240 | if (rendering) { 241 | onUpdate(); 242 | render(); // 如果surface缓存没有释放(被消费)那么这里将卡住 243 | } 244 | break; 245 | case Event.STOP_RENDER: 246 | rendering = false; 247 | break; 248 | case Event.RUNNABLE: 249 | ((Runnable) event.param).run(); 250 | break; 251 | case Event.RELEASE: 252 | isRelease = true; 253 | break; 254 | default: 255 | Log.e(TAG, "event error: " + event); 256 | break; 257 | } 258 | } catch (InterruptedException e) { 259 | e.printStackTrace(); 260 | } 261 | } 262 | // 回调 263 | onDestroy(); 264 | // 销毁eglSurface 265 | for (GLSurface outputSurface : outputSurfaces) { 266 | EGL14.eglDestroySurface(eglDisplay, outputSurface.eglSurface); 267 | outputSurface.eglSurface = EGL_NO_SURFACE; 268 | } 269 | destroyGL(); 270 | eventQueue.clear(); 271 | Log.d(TAG, getName() + ": render release"); 272 | } 273 | 274 | /** 275 | * 退出OpenGL渲染并释放资源 276 | * 这里先将渲染器释放(renderer)再退出looper,因为renderer里面可能持有这个looper的handler, 277 | * 先退出looper再释放renderer可能会报一些警告信息(sending message to a Handler on a dead thread) 278 | */ 279 | public void release() { 280 | if (eventQueue.offer(new Event(Event.RELEASE))) { 281 | // 等待线程结束,如果不等待,在快速开关的时候可能会导致资源竞争(如竞争摄像头) 282 | // 但这样操作可能会引起界面卡顿,择优取舍 283 | while (isAlive()) { 284 | try { 285 | this.join(1000); 286 | } catch (InterruptedException e) { 287 | e.printStackTrace(); 288 | } 289 | } 290 | } 291 | } 292 | 293 | /** 294 | * 当创建完基本的OpenGL环境后调用此方法,可以在这里初始化纹理之类的东西 295 | */ 296 | public abstract void onCreated(); 297 | 298 | /** 299 | * 在渲染之前调用,用于更新纹理数据。渲染一帧调用一次 300 | */ 301 | public abstract void onUpdate(); 302 | 303 | /** 304 | * 绘制渲染,每次绘制都会调用,一帧数据可能调用多次(不同是输出缓存) 305 | * 306 | * @param outputSurface 输出缓存位置surface 307 | */ 308 | public abstract void onDrawFrame(GLSurface outputSurface); 309 | 310 | /** 311 | * 当渲染器销毁前调用,用户回收释放资源 312 | */ 313 | public abstract void onDestroy(); 314 | 315 | 316 | private static String getEGLErrorString() { 317 | return GLUtils.getEGLErrorString(EGL14.eglGetError()); 318 | } 319 | 320 | private static class Event { 321 | static final int ADD_SURFACE = 1; // 添加输出的surface 322 | static final int REMOVE_SURFACE = 2; // 移除输出的surface 323 | static final int START_RENDER = 3; // 开始渲染 324 | static final int REQ_RENDER = 4; // 请求渲染 325 | static final int STOP_RENDER = 5; // 结束渲染 326 | static final int RUNNABLE = 6; // 327 | static final int RELEASE = 7; // 释放渲染器 328 | 329 | final int event; 330 | Object param; 331 | 332 | Event(int event) { 333 | this.event = event; 334 | } 335 | } 336 | } 337 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/huangqi/offscreentest/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.huangqi.offscreentest; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | import android.util.Log; 6 | import android.view.SurfaceHolder; 7 | import android.view.SurfaceView; 8 | import android.widget.TextView; 9 | 10 | public class MainActivity extends AppCompatActivity { 11 | static final String TAG = "MainActivity"; 12 | 13 | // Used to load the 'native-lib' library on application startup. 14 | static { 15 | System.loadLibrary("native-lib"); 16 | } 17 | 18 | private GLRenderer render; 19 | 20 | private class HolderCallBack implements SurfaceHolder.Callback { 21 | private GLSurface mGLSurface; 22 | 23 | @Override 24 | public void surfaceCreated(SurfaceHolder holder) { 25 | mGLSurface = new GLSurface(holder.getSurface(), holder.getSurfaceFrame().width(), holder.getSurfaceFrame().height()); 26 | render.addSurface(mGLSurface); 27 | } 28 | 29 | @Override 30 | public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 31 | Log.d(TAG, String.format("surfaceChanged width: %d height: %d", width, height)); 32 | mGLSurface.setViewport(0, 0, width, height); 33 | render.requestRender(); 34 | } 35 | 36 | @Override 37 | public void surfaceDestroyed(SurfaceHolder holder) { 38 | render.removeSurface(mGLSurface); 39 | } 40 | } 41 | 42 | @Override 43 | protected void onCreate(Bundle savedInstanceState) { 44 | Log.d(TAG, "onCreate"); 45 | super.onCreate(savedInstanceState); 46 | setContentView(R.layout.activity_main); 47 | 48 | // Example of a call to a native method 49 | TextView tv = (TextView) findViewById(R.id.sample_text); 50 | this.setupGL(); 51 | 52 | tv.setText(stringFromJNI()); 53 | } 54 | 55 | private void setupGL() { 56 | // Example of a call to a native method 57 | SurfaceView sv = (SurfaceView) findViewById(R.id.camera_preview); 58 | SurfaceView sm = (SurfaceView) findViewById(R.id.camera_preview_small); 59 | this.render = new GLRenderer(); 60 | this.render.startRender(); 61 | 62 | sv.getHolder().addCallback(new HolderCallBack()); 63 | sm.getHolder().addCallback(new HolderCallBack()); 64 | 65 | this.render.startPreview(); 66 | this.render.setFilter("@beautify face 1 480 640 @curve R(0, 0)(71, 74)(164, 165)(255, 255) @pixblend screen 0.94118 0.29 0.29 1 20"); 67 | } 68 | 69 | @Override 70 | protected void onDestroy() { 71 | Log.d(TAG, "onDestroy"); 72 | this.render.stopPreview(); 73 | this.render.release(); 74 | this.render = null; 75 | super.onDestroy(); 76 | } 77 | 78 | /** 79 | * A native method that is implemented by the 'native-lib' native library, 80 | * which is packaged with this application. 81 | */ 82 | public native String stringFromJNI(); 83 | } 84 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/huangqi/offscreentest/TestGLSurface.java: -------------------------------------------------------------------------------- 1 | package com.example.huangqi.offscreentest; 2 | 3 | import android.content.Context; 4 | import android.opengl.GLSurfaceView; 5 | 6 | /** 7 | * Created by huangqi on 2017/12/12. 8 | */ 9 | 10 | public class TestGLSurface extends GLSurfaceView { 11 | public TestGLSurface(Context ctx) { 12 | super(ctx); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 18 | 19 | 28 | 29 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pinqy520/GLMultiCameraPreviewDemo/3274a6303c472f91aaec10eaaf78ea92bc47ef5f/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pinqy520/GLMultiCameraPreviewDemo/3274a6303c472f91aaec10eaaf78ea92bc47ef5f/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pinqy520/GLMultiCameraPreviewDemo/3274a6303c472f91aaec10eaaf78ea92bc47ef5f/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pinqy520/GLMultiCameraPreviewDemo/3274a6303c472f91aaec10eaaf78ea92bc47ef5f/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pinqy520/GLMultiCameraPreviewDemo/3274a6303c472f91aaec10eaaf78ea92bc47ef5f/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pinqy520/GLMultiCameraPreviewDemo/3274a6303c472f91aaec10eaaf78ea92bc47ef5f/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pinqy520/GLMultiCameraPreviewDemo/3274a6303c472f91aaec10eaaf78ea92bc47ef5f/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pinqy520/GLMultiCameraPreviewDemo/3274a6303c472f91aaec10eaaf78ea92bc47ef5f/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pinqy520/GLMultiCameraPreviewDemo/3274a6303c472f91aaec10eaaf78ea92bc47ef5f/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pinqy520/GLMultiCameraPreviewDemo/3274a6303c472f91aaec10eaaf78ea92bc47ef5f/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | OffScreenTest 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/example/huangqi/offscreentest/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.example.huangqi.offscreentest; 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 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | 5 | repositories { 6 | google() 7 | jcenter() 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.0.1' 11 | 12 | 13 | // NOTE: Do not place your application dependencies here; they belong 14 | // in the individual module build.gradle files 15 | } 16 | } 17 | 18 | allprojects { 19 | repositories { 20 | google() 21 | jcenter() 22 | } 23 | } 24 | 25 | task clean(type: Delete) { 26 | delete rootProject.buildDir 27 | } 28 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | ## Project-wide Gradle settings. 2 | # 3 | # For more details on how to configure your build environment visit 4 | # http://www.gradle.org/docs/current/userguide/build_environment.html 5 | # 6 | # Specifies the JVM arguments used for the daemon process. 7 | # The setting is particularly useful for tweaking memory settings. 8 | # Default value: -Xmx1024m -XX:MaxPermSize=256m 9 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 10 | # 11 | # When configured, Gradle will run in incubating parallel mode. 12 | # This option should only be used with decoupled projects. More details, visit 13 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 14 | # org.gradle.parallel=true 15 | #Sun Dec 10 18:48:10 CST 2017 16 | systemProp.http.proxyHost=127.0.0.1 17 | org.gradle.jvmargs=-Xmx1536m 18 | systemProp.http.proxyPort=49320 19 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pinqy520/GLMultiCameraPreviewDemo/3274a6303c472f91aaec10eaaf78ea92bc47ef5f/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Dec 10 18:48:08 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-4.1-all.zip 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | --------------------------------------------------------------------------------