3dphotoshoot

diff src/android/camera.c @ 6:e31e23ead56f

GAMO TO XRISTO KAI TIN PANAGIA TOU ANDROID
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 21 May 2015 19:03:00 +0300
parents
children 7f6e6eb3d20e
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/android/camera.c	Thu May 21 19:03:00 2015 +0300
     1.3 @@ -0,0 +1,113 @@
     1.4 +#include <stdio.h>
     1.5 +#include <assert.h>
     1.6 +#include <jni.h>
     1.7 +#include "opengl.h"
     1.8 +#include "camera.h"
     1.9 +
    1.10 +
    1.11 +static JavaVM *jvm;
    1.12 +static JNIEnv *jni;
    1.13 +static jclass activity_class;
    1.14 +static unsigned int tex;
    1.15 +static int capturing;
    1.16 +
    1.17 +int cam_init(void *platform_data)
    1.18 +{
    1.19 +	int glerr;
    1.20 +	struct cam_android_platform_data *pdata = platform_data;
    1.21 +
    1.22 +	jvm = pdata->vm;
    1.23 +	jni = pdata->jni;
    1.24 +	activity_class = pdata->activity_class;
    1.25 +
    1.26 +	// create the camera texture
    1.27 +	assert(glGetError() == GL_NO_ERROR);
    1.28 +	glGenTextures(1, &tex);
    1.29 +	glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex);
    1.30 +	glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    1.31 +	glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    1.32 +
    1.33 +	glerr = glGetError();
    1.34 +	assert(glerr == GL_NO_ERROR);
    1.35 +	return glerr == GL_NO_ERROR ? 0 : -1;
    1.36 +}
    1.37 +
    1.38 +void cam_shutdown(void)
    1.39 +{
    1.40 +	cam_stop_video();
    1.41 +	glDeleteTextures(1, &tex);
    1.42 +	tex = 0;
    1.43 +}
    1.44 +
    1.45 +unsigned int cam_texture(void)
    1.46 +{
    1.47 +	return tex;
    1.48 +}
    1.49 +
    1.50 +void cam_texture_size(int *w, int *h)
    1.51 +{
    1.52 +	/* TODO */
    1.53 +}
    1.54 +
    1.55 +int cam_start_video(void)
    1.56 +{
    1.57 +	jmethodID method;
    1.58 +
    1.59 +	if(!jvm) {
    1.60 +		fprintf(stderr, "failed to start video, camera not initialized\n");
    1.61 +		return -1;
    1.62 +	}
    1.63 +	if(cam_is_capturing()) {
    1.64 +		return 0;
    1.65 +	}
    1.66 +
    1.67 +	if(!(method = (*jni)->GetStaticMethodID(jni, activity_class, "start_video", "(I)I"))) {
    1.68 +		fprintf(stderr, "failed to find static method: start_video\n");
    1.69 +		return -1;
    1.70 +	}
    1.71 +	if((*jni)->CallStaticIntMethod(jni, activity_class, method, tex) == -1) {
    1.72 +		fprintf(stderr, "failed to start video capture\n");
    1.73 +		capturing = 0;
    1.74 +		return -1;
    1.75 +	}
    1.76 +	capturing = 1;
    1.77 +
    1.78 +	printf("video started\n");
    1.79 +
    1.80 +	return 1;
    1.81 +}
    1.82 +
    1.83 +int cam_stop_video(void)
    1.84 +{
    1.85 +	jmethodID method;
    1.86 +
    1.87 +	if(!jvm) {
    1.88 +		fprintf(stderr, "failed to stop video, camera not initialized\n");
    1.89 +		return -1;
    1.90 +	}
    1.91 +
    1.92 +	if(!cam_is_capturing()) {
    1.93 +		return 0;
    1.94 +	}
    1.95 +
    1.96 +	if(!(method = (*jni)->GetStaticMethodID(jni, activity_class, "stop_video", "()V"))) {
    1.97 +		fprintf(stderr, "failed to find static method: stop_video\n");
    1.98 +		return -1;
    1.99 +	}
   1.100 +	(*jni)->CallStaticVoidMethod(jni, activity_class, method);
   1.101 +	capturing = 0;
   1.102 +
   1.103 +	printf("video stopped\n");
   1.104 +
   1.105 +	return 0;
   1.106 +}
   1.107 +
   1.108 +int cam_is_capturing(void)
   1.109 +{
   1.110 +	return capturing;	// XXX is it better to do this properly through JNI?
   1.111 +}
   1.112 +
   1.113 +int cam_take_picture(void)
   1.114 +{
   1.115 +	return -1;	// TODO
   1.116 +}