1
1
package in .omerjerk .processing .video .android ;
2
2
3
+ import java .io .IOException ;
3
4
import java .util .ArrayList ;
4
5
5
6
import android .content .Context ;
6
7
import android .content .pm .PackageManager ;
7
8
import android .hardware .Camera ;
9
+ import android .util .Log ;
10
+ import android .view .SurfaceHolder ;
11
+ import android .view .SurfaceView ;
8
12
import processing .core .PApplet ;
9
13
import processing .core .PConstants ;
10
14
import processing .core .PImage ;
11
15
12
- @ SuppressWarnings ("deprecation" )
16
+ // @SuppressWarnings("deprecation")
13
17
public class Capture extends PImage implements PConstants {
14
18
15
- private static Context context ;
19
+ private static final boolean DEBUG = true ;
20
+ public static void log (String log ) {if (DEBUG ) System .out .println (log );}
21
+
22
+ private Context context ;
16
23
17
24
private static ArrayList <String > camerasList = new ArrayList <String >();
18
25
@@ -22,20 +29,19 @@ public class Capture extends PImage implements PConstants {
22
29
private int selectedCamera = 0 ;
23
30
24
31
public Capture (PApplet context ) {
25
- this ( context , null ) ;
32
+ this . context = context ;
26
33
}
27
34
28
- public Capture (PApplet context , String camera ) {
29
- Capture .context = context ;
35
+ public void setCamera (String camera ) {
30
36
if (camera == null || camera .equals ("" )) {
31
37
selectedCamera = 0 ;
32
38
} else {
33
39
selectedCamera = camerasList .indexOf (camera );
34
40
}
41
+ log ("Selected camera = " + selectedCamera );
35
42
}
36
43
37
- public static String [] list () {
38
- ensureContext ();
44
+ public String [] list () {
39
45
if (true ) {
40
46
int nOfCameras = Camera .getNumberOfCameras ();
41
47
for (int i = 0 ; i < nOfCameras ; ++i ) {
@@ -55,12 +61,73 @@ public static String[] list() {
55
61
return null ;
56
62
}
57
63
58
- private static void ensureContext () {
59
- if (context == null ) {
60
- context = PApplet .getInstance ();
61
- if (context == null ) {
62
- throw new RuntimeException ("Create the instance of Capture class first." );
63
- }
64
+ private Camera .PreviewCallback previewCallback = new Camera .PreviewCallback () {
65
+
66
+ @ Override
67
+ public void onPreviewFrame (byte [] arg0 , Camera arg1 ) {
68
+ log ("Received Camera frame" );
69
+ }
70
+ };
71
+
72
+ private class CameraPreview extends SurfaceView implements SurfaceHolder .Callback {
73
+
74
+ private Camera mCamera ;
75
+ private SurfaceHolder mHolder ;
76
+
77
+ public CameraPreview (Context context , Camera camera ) {
78
+ super (context );
79
+ this .mCamera = camera ;
80
+
81
+ mHolder = getHolder ();
82
+ mHolder .addCallback (this );
83
+ mHolder .setType (SurfaceHolder .SURFACE_TYPE_PUSH_BUFFERS );
64
84
}
85
+
86
+ @ Override
87
+ public void surfaceCreated (SurfaceHolder holder ) {
88
+ try {
89
+ mCamera .setPreviewDisplay (holder );
90
+ mCamera .startPreview ();
91
+ } catch (IOException e ) {
92
+ Log .d ("PROCESSING" , "Error setting camera preview: " + e .getMessage ());
93
+ e .printStackTrace ();
94
+ }
95
+ }
96
+
97
+ @ Override
98
+ public void surfaceChanged (SurfaceHolder holder , int format , int width , int height ) {
99
+ // If your preview can change or rotate, take care of those events here.
100
+ // Make sure to stop the preview before resizing or reformatting it.
101
+
102
+ if (mHolder .getSurface () == null ){
103
+ // preview surface does not exist
104
+ return ;
105
+ }
106
+
107
+ // stop preview before making changes
108
+ try {
109
+ mCamera .stopPreview ();
110
+ } catch (Exception e ){
111
+ // ignore: tried to stop a non-existent preview
112
+ }
113
+
114
+ // set preview size and make any resize, rotate or
115
+ // reformatting changes here
116
+
117
+ // start preview with new settings
118
+ try {
119
+ mCamera .setPreviewDisplay (mHolder );
120
+ mCamera .startPreview ();
121
+
122
+ } catch (Exception e ){
123
+ Log .d ("PROCESSING" , "Error starting camera preview: " + e .getMessage ());
124
+ e .printStackTrace ();
125
+ }
126
+ }
127
+
128
+ @ Override
129
+ public void surfaceDestroyed (SurfaceHolder holder ) {
130
+ // do nothing
131
+ }
65
132
}
66
133
}
0 commit comments