3dphotoshoot
annotate src/android/MainActivity.java @ 16:c6952cc82cca
asset manager, android version
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 09 Jun 2015 18:38:33 +0300 |
parents | 9fc7d52f578d |
children | dccbd7d65517 |
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@9 | 27 private static Camera.Parameters params; |
nuclear@9 | 28 private static int preview_width, preview_height; |
nuclear@7 | 29 private static SurfaceTexture surftex; |
nuclear@3 | 30 |
nuclear@4 | 31 public static int start_video(int texid) |
nuclear@4 | 32 { |
nuclear@4 | 33 int num_cams = Camera.getNumberOfCameras(); |
nuclear@4 | 34 Log.i(tag, "Found " + num_cams + " cameras"); |
nuclear@3 | 35 |
nuclear@8 | 36 if(num_cams < 1) { |
nuclear@8 | 37 return -1; |
nuclear@8 | 38 } |
nuclear@8 | 39 |
nuclear@4 | 40 for(int i=0; i<num_cams; i++) { |
nuclear@4 | 41 CameraInfo info = new CameraInfo(); |
nuclear@4 | 42 Camera.getCameraInfo(i, info); |
nuclear@3 | 43 |
nuclear@4 | 44 String facing_str = info.facing == CameraInfo.CAMERA_FACING_FRONT ? "front" : "back"; |
nuclear@3 | 45 |
nuclear@4 | 46 Log.i(tag, "camera[" + i + "]: " + facing_str); |
nuclear@3 | 47 } |
nuclear@3 | 48 |
nuclear@7 | 49 surftex = new SurfaceTexture(texid); |
nuclear@4 | 50 surftex.setOnFrameAvailableListener(new FrameHandler()); |
nuclear@4 | 51 |
nuclear@8 | 52 cam = Camera.open(0); |
nuclear@8 | 53 |
nuclear@9 | 54 params = cam.getParameters(); |
nuclear@8 | 55 params.set("orientation", "landscape"); |
nuclear@8 | 56 cam.setParameters(params); |
nuclear@8 | 57 |
nuclear@9 | 58 preview_width = params.getPreviewSize().width; |
nuclear@9 | 59 preview_height = params.getPreviewSize().height; |
nuclear@9 | 60 |
nuclear@4 | 61 try { |
nuclear@4 | 62 cam.setPreviewTexture(surftex); |
nuclear@4 | 63 } |
nuclear@4 | 64 catch(java.io.IOException e) { |
nuclear@4 | 65 Log.e(tag, "failed to set preview texture"); |
nuclear@4 | 66 cam = null; |
nuclear@4 | 67 return -1; |
nuclear@4 | 68 } |
nuclear@4 | 69 cam.startPreview(); |
nuclear@4 | 70 |
nuclear@4 | 71 return 0; |
nuclear@3 | 72 } |
nuclear@3 | 73 |
nuclear@4 | 74 public static void stop_video() |
nuclear@3 | 75 { |
nuclear@4 | 76 cam.stopPreview(); |
nuclear@4 | 77 cam = null; |
nuclear@9 | 78 params = null; |
nuclear@7 | 79 surftex = null; |
nuclear@7 | 80 } |
nuclear@7 | 81 |
nuclear@9 | 82 public static synchronized void update(float [] matrix) |
nuclear@7 | 83 { |
nuclear@9 | 84 if(cam != null && surftex != null) { |
nuclear@9 | 85 if(frame_pending) { |
nuclear@9 | 86 surftex.updateTexImage(); |
nuclear@9 | 87 frame_pending = false; |
nuclear@9 | 88 } |
nuclear@9 | 89 surftex.getTransformMatrix(matrix); |
nuclear@7 | 90 } |
nuclear@3 | 91 } |
nuclear@0 | 92 } |
nuclear@3 | 93 |
nuclear@4 | 94 class FrameHandler implements SurfaceTexture.OnFrameAvailableListener { |
nuclear@6 | 95 @Override |
nuclear@6 | 96 public synchronized void onFrameAvailable(SurfaceTexture stex) |
nuclear@3 | 97 { |
nuclear@8 | 98 MainActivity.frame_pending = true; |
nuclear@3 | 99 } |
nuclear@3 | 100 } |