1
1
2
2
package jp .cyberagent .android .gpuimage .sample .activity ;
3
3
4
+ import java .io .File ;
5
+ import java .io .FileNotFoundException ;
6
+ import java .io .FileOutputStream ;
7
+ import java .io .IOException ;
8
+ import java .text .SimpleDateFormat ;
9
+ import java .util .Date ;
10
+
4
11
import jp .cyberagent .android .gpuimage .GPUImage ;
12
+ import jp .cyberagent .android .gpuimage .GPUImage .OnPictureSavedListener ;
5
13
import jp .cyberagent .android .gpuimage .GPUImageFilter ;
6
14
import jp .cyberagent .android .gpuimage .sample .GPUImageFilterTools ;
7
15
import jp .cyberagent .android .gpuimage .sample .GPUImageFilterTools .FilterAdjuster ;
8
16
import jp .cyberagent .android .gpuimage .sample .GPUImageFilterTools .OnGpuImageFilterChosenListener ;
9
17
import jp .cyberagent .android .gpuimage .sample .R ;
10
18
import android .app .Activity ;
19
+ import android .graphics .Bitmap ;
20
+ import android .graphics .BitmapFactory ;
11
21
import android .hardware .Camera ;
12
22
import android .hardware .Camera .Parameters ;
23
+ import android .net .Uri ;
13
24
import android .opengl .GLSurfaceView ;
14
25
import android .os .Bundle ;
26
+ import android .os .Environment ;
27
+ import android .util .Log ;
15
28
import android .view .View ;
16
29
import android .view .View .OnClickListener ;
17
30
import android .widget .SeekBar ;
@@ -31,6 +44,7 @@ public void onCreate(final Bundle savedInstanceState) {
31
44
setContentView (R .layout .activity_camera );
32
45
((SeekBar ) findViewById (R .id .seekBar )).setOnSeekBarChangeListener (this );
33
46
findViewById (R .id .button_choose_filter ).setOnClickListener (this );
47
+ findViewById (R .id .button_capture ).setOnClickListener (this );
34
48
35
49
mGPUImage = new GPUImage (this );
36
50
mGPUImage .setGLSurfaceView ((GLSurfaceView ) findViewById (R .id .surfaceView ));
@@ -52,13 +66,116 @@ protected void onPause() {
52
66
53
67
@ Override
54
68
public void onClick (final View v ) {
55
- GPUImageFilterTools .showDialog (this , new OnGpuImageFilterChosenListener () {
69
+ switch (v .getId ()) {
70
+ case R .id .button_choose_filter :
71
+ GPUImageFilterTools .showDialog (this , new OnGpuImageFilterChosenListener () {
72
+
73
+ @ Override
74
+ public void onGpuImageFilterChosenListener (final GPUImageFilter filter ) {
75
+ switchFilterTo (filter );
76
+ }
77
+ });
78
+ break ;
79
+
80
+ case R .id .button_capture :
81
+ Camera .Size size = mCamera .mCameraInstance .getParameters ().getPictureSize ();
82
+ Log .i ("ASDF" , size .width + "x" + size .height );
83
+ // TODO get a size that is about the size of the screen
84
+ Camera .Parameters params = mCamera .mCameraInstance .getParameters ();
85
+ params .setPictureSize (1280 , 960 );
86
+ params .setRotation (90 );
87
+ mCamera .mCameraInstance .setParameters (params );
88
+ for (Camera .Size size2 : mCamera .mCameraInstance .getParameters ()
89
+ .getSupportedPictureSizes ()) {
90
+ Log .i ("ASDF" , "Supported: " + size2 .width + "x" + size2 .height );
91
+ }
92
+ mCamera .mCameraInstance .autoFocus (new Camera .AutoFocusCallback () {
93
+
94
+ @ Override
95
+ public void onAutoFocus (final boolean success , final Camera camera ) {
96
+ mCamera .mCameraInstance .takePicture (null , null ,
97
+ new Camera .PictureCallback () {
98
+
99
+ @ Override
100
+ public void onPictureTaken (byte [] data , final Camera camera ) {
101
+
102
+ final File pictureFile = getOutputMediaFile (MEDIA_TYPE_IMAGE );
103
+ if (pictureFile == null ) {
104
+ Log .d ("ASDF" ,
105
+ "Error creating media file, check storage permissions" );
106
+ return ;
107
+ }
108
+
109
+ try {
110
+ FileOutputStream fos = new FileOutputStream (pictureFile );
111
+ fos .write (data );
112
+ fos .close ();
113
+ } catch (FileNotFoundException e ) {
114
+ Log .d ("ASDF" , "File not found: " + e .getMessage ());
115
+ } catch (IOException e ) {
116
+ Log .d ("ASDF" , "Error accessing file: " + e .getMessage ());
117
+ }
118
+
119
+ data = null ;
120
+ Bitmap bitmap = BitmapFactory .decodeFile (pictureFile
121
+ .getAbsolutePath ());
122
+ // mGPUImage.setImage(bitmap);
123
+ final GLSurfaceView view = (GLSurfaceView ) findViewById (R .id .surfaceView );
124
+ view .setRenderMode (GLSurfaceView .RENDERMODE_WHEN_DIRTY );
125
+ mGPUImage .saveToPictures (bitmap , "GPUImage" ,
126
+ System .currentTimeMillis () + ".jpg" ,
127
+ new OnPictureSavedListener () {
128
+
129
+ @ Override
130
+ public void onPictureSaved (final Uri
131
+ uri ) {
132
+ pictureFile .delete ();
133
+ camera .startPreview ();
134
+ view .setRenderMode (GLSurfaceView .RENDERMODE_CONTINUOUSLY );
135
+ }
136
+ });
137
+ }
138
+ });
139
+ }
140
+ });
141
+ break ;
142
+ }
143
+ }
144
+
145
+ public static final int MEDIA_TYPE_IMAGE = 1 ;
146
+ public static final int MEDIA_TYPE_VIDEO = 2 ;
147
+
148
+ private static File getOutputMediaFile (final int type ) {
149
+ // To be safe, you should check that the SDCard is mounted
150
+ // using Environment.getExternalStorageState() before doing this.
56
151
57
- @ Override
58
- public void onGpuImageFilterChosenListener (final GPUImageFilter filter ) {
59
- switchFilterTo (filter );
152
+ File mediaStorageDir = new File (Environment .getExternalStoragePublicDirectory (
153
+ Environment .DIRECTORY_PICTURES ), "MyCameraApp" );
154
+ // This location works best if you want the created images to be shared
155
+ // between applications and persist after your app has been uninstalled.
156
+
157
+ // Create the storage directory if it does not exist
158
+ if (!mediaStorageDir .exists ()) {
159
+ if (!mediaStorageDir .mkdirs ()) {
160
+ Log .d ("MyCameraApp" , "failed to create directory" );
161
+ return null ;
60
162
}
61
- });
163
+ }
164
+
165
+ // Create a media file name
166
+ String timeStamp = new SimpleDateFormat ("yyyyMMdd_HHmmss" ).format (new Date ());
167
+ File mediaFile ;
168
+ if (type == MEDIA_TYPE_IMAGE ) {
169
+ mediaFile = new File (mediaStorageDir .getPath () + File .separator +
170
+ "IMG_" + timeStamp + ".jpg" );
171
+ } else if (type == MEDIA_TYPE_VIDEO ) {
172
+ mediaFile = new File (mediaStorageDir .getPath () + File .separator +
173
+ "VID_" + timeStamp + ".mp4" );
174
+ } else {
175
+ return null ;
176
+ }
177
+
178
+ return mediaFile ;
62
179
}
63
180
64
181
private void switchFilterTo (final GPUImageFilter filter ) {
@@ -94,6 +211,7 @@ public void onResume() {
94
211
// TODO adjust by getting supportedPreviewSizes and then choosing
95
212
// the best one for screen size (best fill screen)
96
213
parameters .setPreviewSize (720 , 480 );
214
+ parameters .setFocusMode (Camera .Parameters .FOCUS_MODE_CONTINUOUS_PICTURE );
97
215
mCameraInstance .setParameters (parameters );
98
216
99
217
mGPUImage .setUpCamera (mCameraInstance );
0 commit comments