3dphotoshoot

annotate src/android/camera.c @ 8:9fc7d52f578d

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