3dphotoshoot
annotate src/android/MainActivity.java @ 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@0 | 1 package com.mutantstargoat.photoshoot3d; |
nuclear@0 | 2 |
nuclear@0 | 3 import android.app.NativeActivity; |
nuclear@0 | 4 import android.util.Log; |
nuclear@4 | 5 import static android.opengl.GLES20.*; |
nuclear@4 | 6 import android.hardware.Camera; |
nuclear@4 | 7 import android.hardware.Camera.CameraInfo; |
nuclear@4 | 8 import android.graphics.SurfaceTexture; |
nuclear@7 | 9 |
nuclear@3 | 10 /* |
nuclear@3 | 11 import android.os.Bundle; |
nuclear@3 | 12 import android.content.Context; |
nuclear@3 | 13 import android.hardware.camera2.*; |
nuclear@3 | 14 */ |
nuclear@0 | 15 |
nuclear@0 | 16 public class MainActivity extends NativeActivity { |
nuclear@6 | 17 public static String tag = "photoshoot3d"; |
nuclear@8 | 18 public static boolean frame_pending; |
nuclear@3 | 19 |
nuclear@4 | 20 public static void foo() |
nuclear@0 | 21 { |
nuclear@4 | 22 glClearColor(0.2f, 1.0f, 0.3f, 1.0f); |
nuclear@0 | 23 } |
nuclear@3 | 24 |
nuclear@3 | 25 // old camera api (fallback) |
nuclear@4 | 26 private static Camera cam; |
nuclear@7 | 27 private static SurfaceTexture surftex; |
nuclear@3 | 28 |
nuclear@4 | 29 public static int start_video(int texid) |
nuclear@4 | 30 { |
nuclear@4 | 31 int num_cams = Camera.getNumberOfCameras(); |
nuclear@4 | 32 Log.i(tag, "Found " + num_cams + " cameras"); |
nuclear@3 | 33 |
nuclear@8 | 34 if(num_cams < 1) { |
nuclear@8 | 35 return -1; |
nuclear@8 | 36 } |
nuclear@8 | 37 |
nuclear@4 | 38 for(int i=0; i<num_cams; i++) { |
nuclear@4 | 39 CameraInfo info = new CameraInfo(); |
nuclear@4 | 40 Camera.getCameraInfo(i, info); |
nuclear@3 | 41 |
nuclear@4 | 42 String facing_str = info.facing == CameraInfo.CAMERA_FACING_FRONT ? "front" : "back"; |
nuclear@3 | 43 |
nuclear@4 | 44 Log.i(tag, "camera[" + i + "]: " + facing_str); |
nuclear@3 | 45 } |
nuclear@3 | 46 |
nuclear@7 | 47 surftex = new SurfaceTexture(texid); |
nuclear@4 | 48 surftex.setOnFrameAvailableListener(new FrameHandler()); |
nuclear@4 | 49 |
nuclear@8 | 50 cam = Camera.open(0); |
nuclear@8 | 51 |
nuclear@8 | 52 Camera.Parameters params = cam.getParameters(); |
nuclear@8 | 53 params.set("orientation", "landscape"); |
nuclear@8 | 54 cam.setParameters(params); |
nuclear@8 | 55 |
nuclear@4 | 56 try { |
nuclear@4 | 57 cam.setPreviewTexture(surftex); |
nuclear@4 | 58 } |
nuclear@4 | 59 catch(java.io.IOException e) { |
nuclear@4 | 60 Log.e(tag, "failed to set preview texture"); |
nuclear@4 | 61 cam = null; |
nuclear@4 | 62 return -1; |
nuclear@4 | 63 } |
nuclear@4 | 64 cam.startPreview(); |
nuclear@4 | 65 |
nuclear@4 | 66 return 0; |
nuclear@3 | 67 } |
nuclear@3 | 68 |
nuclear@4 | 69 public static void stop_video() |
nuclear@3 | 70 { |
nuclear@4 | 71 cam.stopPreview(); |
nuclear@4 | 72 cam = null; |
nuclear@7 | 73 surftex = null; |
nuclear@7 | 74 } |
nuclear@7 | 75 |
nuclear@8 | 76 public static synchronized void update() |
nuclear@7 | 77 { |
nuclear@8 | 78 if(cam != null && surftex != null && frame_pending) { |
nuclear@7 | 79 surftex.updateTexImage(); |
nuclear@8 | 80 frame_pending = false; |
nuclear@7 | 81 } |
nuclear@3 | 82 } |
nuclear@0 | 83 } |
nuclear@3 | 84 |
nuclear@4 | 85 class FrameHandler implements SurfaceTexture.OnFrameAvailableListener { |
nuclear@6 | 86 @Override |
nuclear@6 | 87 public synchronized void onFrameAvailable(SurfaceTexture stex) |
nuclear@3 | 88 { |
nuclear@8 | 89 MainActivity.frame_pending = true; |
nuclear@3 | 90 } |
nuclear@3 | 91 } |