3dphotoshoot
view src/android/camera.c @ 7:7f6e6eb3d20e
some progress...
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 23 May 2015 23:14:44 +0300 |
parents | e31e23ead56f |
children | 9fc7d52f578d |
line source
1 #include <stdio.h>
2 #include <assert.h>
3 #include <jni.h>
4 #include "opengl.h"
5 #include "camera.h"
8 static JavaVM *jvm;
9 static JNIEnv *jni;
10 static jclass activity_class;
11 static unsigned int tex;
12 static int capturing;
14 int cam_init(void *platform_data)
15 {
16 int glerr;
17 struct cam_android_platform_data *pdata = platform_data;
19 jvm = pdata->vm;
20 jni = pdata->jni;
21 activity_class = pdata->activity_class;
23 // create the camera texture
24 assert(glGetError() == GL_NO_ERROR);
25 glGenTextures(1, &tex);
26 glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex);
27 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
28 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
30 glerr = glGetError();
31 assert(glerr == GL_NO_ERROR);
32 return glerr == GL_NO_ERROR ? 0 : -1;
33 }
35 void cam_shutdown(void)
36 {
37 cam_stop_video();
38 glDeleteTextures(1, &tex);
39 tex = 0;
40 }
42 unsigned int cam_texture(void)
43 {
44 return tex;
45 }
47 void cam_texture_size(int *w, int *h)
48 {
49 /* TODO */
50 }
52 int cam_start_video(void)
53 {
54 jmethodID method;
56 if(!jvm) {
57 fprintf(stderr, "failed to start video, camera not initialized\n");
58 return -1;
59 }
60 if(cam_is_capturing()) {
61 return 0;
62 }
64 if(!(method = (*jni)->GetStaticMethodID(jni, activity_class, "start_video", "(I)I"))) {
65 fprintf(stderr, "failed to find static method: start_video\n");
66 return -1;
67 }
68 if((*jni)->CallStaticIntMethod(jni, activity_class, method, tex) == -1) {
69 fprintf(stderr, "failed to start video capture\n");
70 capturing = 0;
71 return -1;
72 }
73 capturing = 1;
75 printf("video started\n");
77 return 1;
78 }
80 int cam_stop_video(void)
81 {
82 jmethodID method;
84 if(!jvm) {
85 fprintf(stderr, "failed to stop video, camera not initialized\n");
86 return -1;
87 }
89 if(!cam_is_capturing()) {
90 return 0;
91 }
93 if(!(method = (*jni)->GetStaticMethodID(jni, activity_class, "stop_video", "()V"))) {
94 fprintf(stderr, "failed to find static method: stop_video\n");
95 return -1;
96 }
97 (*jni)->CallStaticVoidMethod(jni, activity_class, method);
98 capturing = 0;
100 printf("video stopped\n");
102 return 0;
103 }
105 void cam_update(void)
106 {
107 jmethodID method;
109 if(!jvm) {
110 fprintf(stderr, "failed to update camera\n");
111 return -1;
112 }
113 if(!cam_is_capturing()) {
114 return 0;
115 }
117 if(!(method = (*jni)->GetStaticMethodID(jni, activity_class, "update", "()V"))) {
118 fprintf(stderr, "failed to find static method: update\n");
119 return -1;
120 }
121 (*jni)->CallStaticVoidMethod(jni, activity_class, method);
122 return;
123 }
125 int cam_is_capturing(void)
126 {
127 return capturing; // XXX is it better to do this properly through JNI?
128 }
130 int cam_take_picture(void)
131 {
132 return -1; // TODO
133 }