PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/OpenCVSample/src/org/siprop/opencv/OpenCVSample.java

http://github.com/billmccord/OpenCV-Android
Java | 312 lines | 221 code | 65 blank | 26 comment | 14 complexity | d49a24661e0904c8e3331a826b883460 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. OpenCV for Android NDK
  3. Copyright (c) 2006-2009 SIProp Project http://www.siprop.org/
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. package org.siprop.opencv;
  14. import android.app.Activity;
  15. import android.app.Dialog;
  16. import android.app.ProgressDialog;
  17. import android.graphics.Bitmap;
  18. import android.graphics.BitmapFactory;
  19. import android.hardware.Camera;
  20. import android.hardware.Camera.PreviewCallback;
  21. import android.os.Bundle;
  22. import android.os.Handler;
  23. import android.os.Message;
  24. import android.util.Log;
  25. import android.view.KeyEvent;
  26. import android.view.SurfaceHolder;
  27. import android.view.SurfaceView;
  28. import android.view.View;
  29. import android.view.ViewGroup;
  30. import android.view.WindowManager;
  31. import android.view.ViewGroup.LayoutParams;
  32. import android.widget.ImageView;
  33. public class OpenCVSample extends Activity implements View.OnClickListener, SurfaceHolder.Callback {
  34. public static final int CROP_MSG = 1;
  35. public static final int KEEP = 2;
  36. public static final int RESTART_PREVIEW = 3;
  37. public static final int CLEAR_SCREEN_DELAY = 4;
  38. public static final int SHOW_LOADING = 5;
  39. public static final int HIDE_LOADING = 6;
  40. public static final int MEDIA_LOADING = 7;
  41. public static final int TAKE_PICT = 8;
  42. public static final int DEFAULT_SOCKET_CAMERA_IMAGE_WIDTH = 320;
  43. public static final int DEFAULT_SOCKET_CAMERA_IMAGE_HEIGHT = 240;
  44. private CameraIF mCameraDevice;
  45. private PreviewCallback mPreviewCallback = new JpegPreviewCallback();
  46. private SurfaceView mSurfaceView = null;
  47. private Handler mHandler = new MainHandler();
  48. public OpenCV opencv = new OpenCV();
  49. private Bitmap faceDetectBitmap;
  50. private final int FP = ViewGroup.LayoutParams.FILL_PARENT;
  51. private boolean mUseRemoteCamera = false;
  52. private String mRemoteCameraAddress = null;
  53. private int mRemoteCameraPort = 0;
  54. private void loadMedia(Integer mode) {
  55. changeView(mode);
  56. }
  57. private void changeView(int mode) {
  58. switch (mode) {
  59. case 1: {
  60. ImageView imageView = new ImageView(this);
  61. imageView.setImageBitmap(faceDetectBitmap);
  62. setContentView(imageView, new LayoutParams(FP, FP));
  63. Message msg = new Message();
  64. msg.what = HIDE_LOADING;
  65. msg.obj = 1;
  66. mHandler.sendMessage(msg);
  67. break;
  68. }
  69. }
  70. }
  71. // ----------------------- Override Methods ------------------------
  72. /** Called with the activity is first created. */
  73. @Override
  74. public void onCreate(Bundle icicle) {
  75. super.onCreate(icicle);
  76. // init Camera.
  77. setContentView(R.layout.main);
  78. mSurfaceView = (SurfaceView)findViewById(R.id.camera_preview);
  79. Bundle extras = getIntent().getExtras();
  80. mUseRemoteCamera = extras.getBoolean(CameraConfiguration.EXTRA_IS_REMOTE_CAMERA);
  81. if (!mUseRemoteCamera) {
  82. mCameraDevice = new Dev1Camera(this, mSurfaceView);
  83. } else {
  84. mRemoteCameraAddress = extras
  85. .getString(CameraConfiguration.EXTRA_REMOTE_CAMERA_ADDRESS);
  86. mRemoteCameraPort = extras.getInt(CameraConfiguration.EXTRA_REMOTE_CAMERA_PORT);
  87. mCameraDevice = new SocketCamera(this, mSurfaceView, mRemoteCameraAddress,
  88. mRemoteCameraPort, DEFAULT_SOCKET_CAMERA_IMAGE_WIDTH,
  89. DEFAULT_SOCKET_CAMERA_IMAGE_HEIGHT);
  90. }
  91. mCameraDevice.setPreviewCallback(mPreviewCallback);
  92. }
  93. @Override
  94. public void onStart() {
  95. super.onStart();
  96. mCameraDevice.onStart();
  97. }
  98. @Override
  99. public void onResume() {
  100. super.onResume();
  101. Log.d("Main", "onResume");
  102. mCameraDevice.onResume();
  103. }
  104. @Override
  105. public void onStop() {
  106. mCameraDevice.onStop();
  107. super.onStop();
  108. }
  109. @Override
  110. protected void onPause() {
  111. mCameraDevice.onPause();
  112. super.onPause();
  113. }
  114. @Override
  115. protected void onDestroy() {
  116. mCameraDevice.onDestroy();
  117. super.onDestroy();
  118. }
  119. public void onClick(View v) {
  120. }
  121. @Override
  122. public boolean onKeyDown(int keyCode, KeyEvent event) {
  123. if (keyCode == KeyEvent.KEYCODE_CAMERA || keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
  124. takePicture();
  125. }
  126. return super.onKeyDown(keyCode, event);
  127. }
  128. public void takePicture() {
  129. mCameraDevice.takePicture();
  130. }
  131. public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
  132. Log.d("Main", "surfaceChanged");
  133. mCameraDevice.surfaceChanged(holder, format, w, h);
  134. }
  135. public void surfaceCreated(SurfaceHolder holder) {
  136. mCameraDevice.surfaceCreated(holder);
  137. }
  138. public void surfaceDestroyed(SurfaceHolder holder) {
  139. mCameraDevice.surfaceDestroyed(holder);
  140. }
  141. // ---------------------------- Callback classes
  142. // ---------------------------------
  143. private final class JpegPreviewCallback implements PreviewCallback {
  144. private boolean isProcessing = false;
  145. public void onPreviewFrame(byte[] jpegData, Camera camera) {
  146. Log.d("JpegPreviewCallback", "in JpegPreviewCallback...");
  147. if (isProcessing) {
  148. Log.d("JpegPreviewCallback", "isProcessing..");
  149. return;
  150. }
  151. isProcessing = true;
  152. {
  153. mCameraDevice.onStop();
  154. Message msg = new Message();
  155. msg.what = SHOW_LOADING;
  156. msg.obj = 1;
  157. mHandler.sendMessage(msg);
  158. }
  159. try {
  160. BitmapFactory.Options options = new BitmapFactory.Options();
  161. options.inSampleSize = 4;
  162. Bitmap bitmap = BitmapFactory
  163. .decodeByteArray(jpegData, 0, jpegData.length, options);
  164. int w = bitmap.getWidth();
  165. int h = bitmap.getHeight();
  166. int[] pixels = new int[w * h];
  167. bitmap.getPixels(pixels, 0, w, 0, 0, w, h);
  168. byte[] data = opencv.findContours(pixels, w, h);
  169. faceDetectBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
  170. if (faceDetectBitmap == null) {
  171. Log.d("JpegPreviewCallback", "bitmapNew is null..");
  172. isProcessing = false;
  173. return;
  174. }
  175. Log.d("JpegPreviewCallback", "bitmapNew is " + faceDetectBitmap.getHeight());
  176. Message msg = new Message();
  177. msg.what = MEDIA_LOADING;
  178. msg.obj = 1;
  179. mHandler.sendMessage(msg);
  180. } catch (Exception e) {
  181. e.printStackTrace();
  182. }
  183. isProcessing = false;
  184. }
  185. };
  186. // ---------------------------- Handler classes
  187. // ---------------------------------
  188. /**
  189. * This Handler is used to post message back onto the main thread of the
  190. * application
  191. */
  192. private class MainHandler extends Handler {
  193. public void handleMessage(Message msg) {
  194. mCameraDevice.handleMessage(msg);
  195. switch (msg.what) {
  196. case KEEP: {
  197. if (msg.obj != null) {
  198. mHandler.post((Runnable)msg.obj);
  199. }
  200. break;
  201. }
  202. case CLEAR_SCREEN_DELAY: {
  203. getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  204. break;
  205. }
  206. case SHOW_LOADING: {
  207. showDialog(DIALOG_LOADING);
  208. break;
  209. }
  210. case HIDE_LOADING: {
  211. try {
  212. dismissDialog(DIALOG_LOADING);
  213. removeDialog(DIALOG_LOADING);
  214. } catch (IllegalArgumentException e) {
  215. }
  216. break;
  217. }
  218. case MEDIA_LOADING: {
  219. loadMedia((Integer)msg.obj);
  220. break;
  221. }
  222. }
  223. }
  224. }
  225. public Handler getMessageHandler() {
  226. return this.mHandler;
  227. }
  228. private static final int DIALOG_LOADING = 0;
  229. @Override
  230. protected Dialog onCreateDialog(int id) {
  231. switch (id) {
  232. case DIALOG_LOADING: {
  233. ProgressDialog dialog = new ProgressDialog(this);
  234. dialog.setMessage("Loading ...");
  235. // dialog.setIndeterminate(true);
  236. dialog.setCancelable(false);
  237. dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
  238. WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
  239. return dialog;
  240. }
  241. default:
  242. return super.onCreateDialog(id);
  243. }
  244. }
  245. }