-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathGPUImageView.java
More file actions
604 lines (533 loc) · 19.8 KB
/
Copy pathGPUImageView.java
File metadata and controls
604 lines (533 loc) · 19.8 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*
* Copyright (C) 2018 CyberAgent, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jp.co.cyberagent.android.gpuimage;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.hardware.Camera;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.opengl.GLSurfaceView;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.concurrent.Semaphore;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageFilter;
import jp.co.cyberagent.android.gpuimage.util.Rotation;
import jp.co.cyberagent.android.gpuimage.view.GLRenderView;
import static jp.co.cyberagent.android.gpuimage.GPUImage.SURFACE_TYPE_SURFACE_VIEW;
import static jp.co.cyberagent.android.gpuimage.GPUImage.SURFACE_TYPE_TEXTURE_VIEW;
public class GPUImageView extends FrameLayout {
private int surfaceType = SURFACE_TYPE_SURFACE_VIEW;
private GLRenderView surfaceView;
private GPUImage gpuImage;
private boolean isShowLoading = true;
private GPUImageFilter filter;
public Size forceSize = null;
private float ratio = 0.0f;
public final static int RENDERMODE_WHEN_DIRTY = 0;
public final static int RENDERMODE_CONTINUOUSLY = 1;
public GPUImageView(Context context) {
super(context);
init(context, null);
}
public GPUImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
if (attrs != null) {
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.GPUImageView, 0, 0);
try {
surfaceType = a.getInt(R.styleable.GPUImageView_gpuimage_surface_type, surfaceType);
isShowLoading = a.getBoolean(R.styleable.GPUImageView_gpuimage_show_loading, isShowLoading);
} finally {
a.recycle();
}
}
gpuImage = new GPUImage(context);
if (surfaceType == SURFACE_TYPE_TEXTURE_VIEW) {
surfaceView = new GPUImageGLTextureView(context, attrs);
} else {
surfaceView = new GPUImageGLSurfaceView(context, attrs);
}
gpuImage.setGLRenderView(surfaceView);
if (surfaceView instanceof View) {
addView((View) surfaceView);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (ratio != 0.0f) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int newHeight;
int newWidth;
if (width / ratio < height) {
newWidth = width;
newHeight = Math.round(width / ratio);
} else {
newHeight = height;
newWidth = Math.round(height * ratio);
}
int newWidthSpec = MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY);
int newHeightSpec = MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY);
super.onMeasure(newWidthSpec, newHeightSpec);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
/**
* Retrieve the GPUImage instance used by this view.
*
* @return used GPUImage instance
*/
public GPUImage getGPUImage() {
return gpuImage;
}
/**
* Deprecated: Please call
* {@link GPUImageView#updatePreviewFrame(byte[], int, int)} frame by frame
* <p>
* Sets the up camera to be connected to GPUImage to get a filtered preview.
*
* @param camera the camera
*/
@Deprecated
public void setUpCamera(final Camera camera) {
gpuImage.setUpCamera(camera);
}
/**
* Deprecated: Please call
* {@link GPUImageView#updatePreviewFrame(byte[], int, int)} frame by frame
* <p>
* Sets the up camera to be connected to GPUImage to get a filtered preview.
*
* @param camera the camera
* @param degrees by how many degrees the image should be rotated
* @param flipHorizontal if the image should be flipped horizontally
* @param flipVertical if the image should be flipped vertically
*/
@Deprecated
public void setUpCamera(final Camera camera, final int degrees, final boolean flipHorizontal,
final boolean flipVertical) {
gpuImage.setUpCamera(camera, degrees, flipHorizontal, flipVertical);
}
/**
* Update camera preview frame with YUV format data.
*
* @param data Camera preview YUV data for frame.
* @param width width of camera preview
* @param height height of camera preview
*/
public void updatePreviewFrame(byte[] data, int width, int height) {
gpuImage.updatePreviewFrame(data, width, height);
}
/**
* Sets the background color
*
* @param red red color value
* @param green green color value
* @param blue red color value
*/
public void setBackgroundColor(float red, float green, float blue) {
gpuImage.setBackgroundColor(red, green, blue);
}
/**
* Set the rendering mode. When renderMode is
* RENDERMODE_CONTINUOUSLY, the renderer is called
* repeatedly to re-render the scene. When renderMode
* is RENDERMODE_WHEN_DIRTY, the renderer only rendered when the surface
* is created, or when {@link #requestRender} is called. Defaults to RENDERMODE_CONTINUOUSLY.
*
* @param renderMode one of the RENDERMODE_X constants
* @see #RENDERMODE_CONTINUOUSLY
* @see #RENDERMODE_WHEN_DIRTY
* @see GLSurfaceView#setRenderMode(int)
* @see GLTextureView#setRenderMode(int)
*/
public void setRenderMode(int renderMode) {
if (surfaceView instanceof GLSurfaceView) {
((GLSurfaceView) surfaceView).setRenderMode(renderMode);
} else if (surfaceView instanceof GLTextureView) {
((GLTextureView) surfaceView).setRenderMode(renderMode);
}
}
// TODO Should be an xml attribute. But then GPUImage can not be distributed as .jar anymore.
public void setRatio(float ratio) {
this.ratio = ratio;
surfaceView.requestLayout();
gpuImage.deleteImage();
}
/**
* Set the scale type of GPUImage.
*
* @param scaleType the new ScaleType
*/
public void setScaleType(GPUImage.ScaleType scaleType) {
gpuImage.setScaleType(scaleType);
}
/**
* Sets the rotation of the displayed image.
*
* @param rotation new rotation
*/
public void setRotation(Rotation rotation) {
gpuImage.setRotation(rotation);
requestRender();
}
/**
* Set the filter to be applied on the image.
*
* @param filter Filter that should be applied on the image.
*/
public void setFilter(GPUImageFilter filter) {
this.filter = filter;
gpuImage.setFilter(filter);
requestRender();
}
/**
* Get the current applied filter.
*
* @return the current filter
*/
public GPUImageFilter getFilter() {
return filter;
}
/**
* Sets the image on which the filter should be applied.
*
* @param bitmap the new image
*/
public void setImage(final Bitmap bitmap) {
gpuImage.setImage(bitmap);
}
/**
* Sets the image on which the filter should be applied from a Uri.
*
* @param uri the uri of the new image
*/
public void setImage(final Uri uri) {
gpuImage.setImage(uri);
}
/**
* Sets the image on which the filter should be applied from a File.
*
* @param file the file of the new image
*/
public void setImage(final File file) {
gpuImage.setImage(file);
}
public void requestRender() {
if (surfaceView instanceof GLSurfaceView) {
((GLSurfaceView) surfaceView).requestRender();
} else if (surfaceView instanceof GLTextureView) {
((GLTextureView) surfaceView).requestRender();
}
}
/**
* Save current image with applied filter to Pictures. It will be stored on
* the default Picture folder on the phone below the given folderName and
* fileName. <br>
* This method is async and will notify when the image was saved through the
* listener.
*
* @param folderName the folder name
* @param fileName the file name
* @param listener the listener
*/
public void saveToPictures(final String folderName, final String fileName,
final OnPictureSavedListener listener) {
new SaveTask(folderName, fileName, listener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
/**
* Save current image with applied filter to Pictures. It will be stored on
* the default Picture folder on the phone below the given folderName and
* fileName. <br>
* This method is async and will notify when the image was saved through the
* listener.
*
* @param folderName the folder name
* @param fileName the file name
* @param width requested output width
* @param height requested output height
* @param listener the listener
*/
public void saveToPictures(final String folderName, final String fileName,
int width, int height,
final OnPictureSavedListener listener) {
new SaveTask(folderName, fileName, width, height, listener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
/**
* Retrieve current image with filter applied and given size as Bitmap.
*
* @param width requested Bitmap width
* @param height requested Bitmap height
* @return Bitmap of picture with given size
* @throws InterruptedException
*/
public Bitmap capture(final int width, final int height) throws InterruptedException {
// This method needs to run on a background thread because it will take a longer time
if (Looper.myLooper() == Looper.getMainLooper()) {
throw new IllegalStateException("Do not call this method from the UI thread!");
}
forceSize = new Size(width, height);
final Semaphore waiter = new Semaphore(0);
// Layout with new size
getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
waiter.release();
}
});
post(new Runnable() {
@Override
public void run() {
// Optionally, show loading view:
if (isShowLoading) {
addView(new LoadingView(getContext()));
}
// Request layout to release waiter:
surfaceView.requestLayout();
}
});
waiter.acquire();
// Run one render pass
gpuImage.runOnGLThread(new Runnable() {
@Override
public void run() {
waiter.release();
}
});
requestRender();
waiter.acquire();
Bitmap bitmap = capture();
forceSize = null;
post(new Runnable() {
@Override
public void run() {
surfaceView.requestLayout();
}
});
requestRender();
if (isShowLoading) {
postDelayed(new Runnable() {
@Override
public void run() {
// Remove loading view
removeViewAt(1);
}
}, 300);
}
return bitmap;
}
/**
* Capture the current image with the size as it is displayed and retrieve it as Bitmap.
*
* @return current output as Bitmap
* @throws InterruptedException
*/
public Bitmap capture() throws InterruptedException {
final Semaphore waiter = new Semaphore(0);
final int width = surfaceView.getMeasuredWidth();
final int height = surfaceView.getMeasuredHeight();
// Take picture on OpenGL thread
final Bitmap resultBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
gpuImage.runOnGLThread(new Runnable() {
@Override
public void run() {
GPUImageNativeLibrary.adjustBitmap(resultBitmap);
waiter.release();
}
});
requestRender();
waiter.acquire();
return resultBitmap;
}
/**
* Pauses the Surface.
*/
public void onPause() {
if (surfaceView instanceof GLSurfaceView) {
((GLSurfaceView) surfaceView).onPause();
} else if (surfaceView instanceof GLTextureView) {
((GLTextureView) surfaceView).onPause();
}
}
/**
* Resumes the Surface.
*/
public void onResume() {
if (surfaceView instanceof GLSurfaceView) {
((GLSurfaceView) surfaceView).onResume();
} else if (surfaceView instanceof GLTextureView) {
((GLTextureView) surfaceView).onResume();
}
}
public static class Size {
int width;
int height;
public Size(int width, int height) {
this.width = width;
this.height = height;
}
}
private class GPUImageGLSurfaceView extends GLSurfaceView implements GLRenderView {
public GPUImageGLSurfaceView(Context context) {
super(context);
}
public GPUImageGLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (forceSize != null) {
super.onMeasure(MeasureSpec.makeMeasureSpec(forceSize.width, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(forceSize.height, MeasureSpec.EXACTLY));
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
@Override
public void setRender(GLRenderView.Renderer render) {
if (render instanceof GLSurfaceView.Renderer) {
setRenderer((GLSurfaceView.Renderer) render);
}
}
}
private class GPUImageGLTextureView extends GLTextureView implements GLRenderView {
public GPUImageGLTextureView(Context context) {
super(context);
}
public GPUImageGLTextureView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (forceSize != null) {
super.onMeasure(MeasureSpec.makeMeasureSpec(forceSize.width, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(forceSize.height, MeasureSpec.EXACTLY));
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
@Override
public void setRender(GLRenderView.Renderer render) {
if (render instanceof GLTextureView.Renderer) {
setRenderer((GLTextureView.Renderer) render);
}
}
}
private class LoadingView extends FrameLayout {
public LoadingView(Context context) {
super(context);
init();
}
public LoadingView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public LoadingView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
ProgressBar view = new ProgressBar(getContext());
view.setLayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER));
addView(view);
setBackgroundColor(Color.BLACK);
}
}
private class SaveTask extends AsyncTask<Void, Void, Void> {
private final String folderName;
private final String fileName;
private final int width;
private final int height;
private final OnPictureSavedListener listener;
private final Handler handler;
public SaveTask(final String folderName, final String fileName,
final OnPictureSavedListener listener) {
this(folderName, fileName, 0, 0, listener);
}
public SaveTask(final String folderName, final String fileName, int width, int height,
final OnPictureSavedListener listener) {
this.folderName = folderName;
this.fileName = fileName;
this.width = width;
this.height = height;
this.listener = listener;
handler = new Handler();
}
@Override
protected Void doInBackground(final Void... params) {
try {
Bitmap result = width != 0 ? capture(width, height) : capture();
saveImage(folderName, fileName, result);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
private void saveImage(final String folderName, final String fileName, final Bitmap image) {
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file = new File(path, folderName + "/" + fileName);
try {
file.getParentFile().mkdirs();
image.compress(Bitmap.CompressFormat.JPEG, 80, new FileOutputStream(file));
MediaScannerConnection.scanFile(getContext(),
new String[]{
file.toString()
}, null,
new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(final String path, final Uri uri) {
if (listener != null) {
handler.post(new Runnable() {
@Override
public void run() {
listener.onPictureSaved(uri);
}
});
}
}
});
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
public interface OnPictureSavedListener {
void onPictureSaved(Uri uri);
}
}