visageSDK
CameraActivity.java
1 package com.visagetechnologies.androidcameraplugin;
2 
3 import android.app.Activity;
4 import android.content.Context;
5 import android.content.DialogInterface;
6 import android.content.Intent;
7 import android.content.res.Configuration;
8 import android.graphics.Bitmap;
9 import android.app.AlertDialog;
10 import android.widget.TextView;
11 import android.graphics.ImageFormat;
12 import android.graphics.SurfaceTexture;
13 import android.hardware.Camera;
14 import android.hardware.Camera.CameraInfo;
15 import android.hardware.Camera.PreviewCallback;
16 import android.hardware.Camera.Size;
17 import android.os.Bundle;
18 import android.util.TypedValue;
19 import android.util.Log;
20 import android.view.Display;
21 import android.view.Gravity;
22 import android.view.WindowManager;
23 import android.widget.Toast;
24 
25 import java.io.IOException;
26 import java.util.List;
27 import java.lang.*;
28 import java.lang.String;
29 
30 import com.unity3d.player.UnityPlayer;
31 import com.unity3d.player.UnityPlayerActivity;
32 
33 public class CameraActivity extends UnityPlayerActivity
34 {
35  public final String TAG = "JavaCamTrackerView";
36 
37  Camera cam;
38  int ImageWidth = -1;
39  int ImageHeight = -1;
40  SurfaceTexture tex;
41  public float cameraFps;
42  int pickCam = -1;
43  int camId = -1;
44  int orientation;
45  boolean openCam = false;
46 
47 
51  @Override
52  public void onConfigurationChanged(Configuration newConfig) {
53 
54  super.onConfigurationChanged(newConfig);
55 
56  CameraInfo camInfo = new CameraInfo();
57 
58  try{
59  Camera.getCameraInfo(camId, camInfo);
60  }catch(Exception e){
61 
62  }
63  Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
64 
65  orientation = camInfo.orientation;
66 
67  if (camInfo.facing == CameraInfo.CAMERA_FACING_FRONT)
68  setParameters((display.getRotation()*90 + orientation)%360, -1, -1, -1);
69  else if (camInfo.facing == CameraInfo.CAMERA_FACING_BACK)
70  setParameters((orientation - display.getRotation()*90 + 360)%360, -1, -1, -1);
71  }
72 
73  @Override
74  public void onCreate(Bundle _savedInstanceState) {
75  super.onCreate(_savedInstanceState);
76  }
77 
78  @Override
79  public void onPause() {
80  closeCamera();
81  super.onPause();
82  }
83 
84  @Override
85  public void onResume() {
86  if (openCam)
87  GrabFromCamera(ImageWidth, ImageHeight, pickCam);
88  super.onResume();
89  }
90 
98  private void setPreviewSize(Camera.Parameters parameters, int width, int height){
99  int idx = 0;
100  double dist1 = 100000;
101  double dist2 = 100000;
102  List<Size> sizes = parameters.getSupportedPreviewSizes();
103  for (int i = 0;i<sizes.size();i++){
104  dist2 = Math.abs(Math.sqrt(Math.pow((sizes.get(i).width-width),2) + Math.pow((sizes.get(i).height-height),2)));
105  if (dist2<dist1){
106  idx = i;
107  dist1 = dist2;
108  }
109  }
110  parameters.setPreviewSize(sizes.get(idx).width, sizes.get(idx).height);
111  }
112 
116  public void GrabFromCamera(int imWidth, int imHeight, int pickCamera){
117  //on first call (from onResume), function parameters are set to -1; frame dimensions and camera are set on next call from Unity script
118  if (imWidth == -1 || imHeight == -1)
119  {
120  imWidth = 320;
121  imHeight = 240;
122  }
123 
124  this.pickCam = pickCamera;
125  camId = getCameraId(pickCam);
126  try{
127  cam = Camera.open(camId);
128  }catch(Exception e){
129  Log.e(TAG, "Unable to open camera");
130  Toast.makeText(getBaseContext(), "Unable to open camera", Toast.LENGTH_SHORT).show();
131  return;
132  }
133 
134  ImageWidth = imWidth;
135  ImageHeight = imHeight;
136  Camera.Parameters parameters = cam.getParameters();
137  setPreviewSize(parameters, ImageWidth, ImageHeight);
138  parameters.setPreviewFormat(ImageFormat.NV21);
139  cam.setParameters(parameters);
140  Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
141  int screenOrientation = display.getRotation();
142 
143  final Size previewSize=cam.getParameters().getPreviewSize();
144  Log.i (TAG, "Current preview size is " + previewSize.width + ", " + previewSize.height);
145 
146  int dataBufferSize=(int)(previewSize.height*previewSize.width*
147  (ImageFormat.getBitsPerPixel(cam.getParameters().getPreviewFormat())/8.0));
148 
149  for (int i=0;i<10;i++){
150  cam.addCallbackBuffer(new byte[dataBufferSize]);
151  }
152 
153  tex = new SurfaceTexture(0);
154  try {
155  cam.setPreviewTexture(tex);
156  } catch (IOException e) {
157  e.printStackTrace();
158  }
159 
160  CameraInfo cameraInfo = new CameraInfo();
161  Camera.getCameraInfo(camId, cameraInfo);
162  orientation = cameraInfo.orientation;
163  int flip = 0;
164  if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT){
165  flip = 1; // Mirror image from frontal camera
166  }
167  ImageWidth = previewSize.width;
168  ImageHeight = previewSize.height;
169 
170  if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT)
171  setParameters((screenOrientation*90 + orientation)%360, ImageWidth, ImageHeight, flip);
172  else
173  setParameters((orientation - screenOrientation*90 + 360)%360, ImageWidth, ImageHeight, flip);
174 
175  cam.setPreviewCallbackWithBuffer(new PreviewCallback() {
176  private long timestamp = 0;
177  public void onPreviewFrame(byte[] data, Camera camera) {
178  //Log.v("CameraTest","FPS = "+1000.0/(System.currentTimeMillis()-timestamp));
179  //cameraFps = 1000.0f/(System.currentTimeMillis()-timestamp);
180  //timestamp=System.currentTimeMillis();
181  WriteFrame(data);
182  camera.addCallbackBuffer(data);
183  }
184  });
185  cam.startPreview();
186  openCam = true;
187  }
188 
189  public void closeCamera ()
190  {
191  if (cam !=null){
192  cam.stopPreview();
193  cam.release();
194  cam = null;
195  }
196  }
197 
204  int getCameraId(int pickCamera){
205  int cameraId = -1;
206  int numberOfCameras = Camera.getNumberOfCameras();
207  for (int i = 0; i < numberOfCameras; i++) {
208  CameraInfo info = new CameraInfo();
209  Camera.getCameraInfo(i, info);
210  cameraId = i;
211  if (info.facing == CameraInfo.CAMERA_FACING_FRONT && pickCamera != 1)
212  return cameraId;
213  else if (info.facing == CameraInfo.CAMERA_FACING_BACK && pickCamera == 1)
214  return cameraId;
215  }
216  return -1;
217  }
218 
219  public void AlertDialogFunction(final String message)
220  {
221  runOnUiThread(new Runnable() {
222  private String licenseMessage = message;
223  @Override
224  public void run() {
225  TextView title = new TextView(UnityPlayer.currentActivity);
226  title.setText("License warning");
227  title.setGravity(Gravity.CENTER);
228  title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
229 
230  TextView msg = new TextView(UnityPlayer.currentActivity);
231  msg.setText(message);
232  msg.setGravity(Gravity.CENTER);
233  msg.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
234 
235  new AlertDialog.Builder(UnityPlayer.currentActivity, AlertDialog.THEME_TRADITIONAL)
236  .setCustomTitle(title)
237  .setView(msg)
238  .setPositiveButton(android.R.string.yes, null)
239  .setIcon(android.R.drawable.ic_dialog_alert)
240  .show();
241 
242  }
243  });
244  }
245 
246  @Override
247  public void onDestroy()
248  {
249  closeCamera();
250  super.onDestroy();
251  }
252 
253  public static native void WriteFrame(byte[] frame);
254 
255  public static native void setParameters(int orientation, int width, int height, int flip);
256  public static native int getImageWidth();
257 
258  static
259  {
260  System.loadLibrary("VisageVision");
261  System.loadLibrary("VisageTrackerUnityPlugin");
262  }
263 
264 
265 }