/ocr/ocrservice/src/com/googlecode/eyesfree/opticflow/OpticalFlow.java

http://eyes-free.googlecode.com/ · Java · 92 lines · 46 code · 22 blank · 24 comment · 0 complexity · 77d908cfe259e0119be2028e1165cbc8 MD5 · raw file

  1. /*
  2. * Copyright (C) 2011 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.googlecode.eyesfree.opticflow;
  17. import android.graphics.PointF;
  18. /**
  19. * Interface to native optical flow library.
  20. *
  21. * Modified by Alan Viverette from Andrew Harp's original source.
  22. *
  23. * @author Andrew Harp
  24. * @author alanv@google.com (Alan Viverette)
  25. */
  26. public class OpticalFlow {
  27. static {
  28. System.loadLibrary("lept");
  29. System.loadLibrary("opticalflow");
  30. }
  31. @Override
  32. protected void finalize() {
  33. resetNative();
  34. }
  35. public void initialize(int width, int height, int downsampleFactor) {
  36. initNative(width, height, downsampleFactor);
  37. }
  38. public void setImage(byte[] data, long timestamp) {
  39. addFrameNative(data, timestamp);
  40. }
  41. public void computeOpticalFlow() {
  42. computeFeaturesNative(true);
  43. computeFlowNative();
  44. printInfoNative();
  45. }
  46. public float[] getFeatures(boolean onlyReturnCorrespondingFeatures) {
  47. return getFeaturesNative(onlyReturnCorrespondingFeatures);
  48. }
  49. public PointF getAccumulatedDelta(
  50. long timestamp, float positionX, float positionY, float radius) {
  51. float[] delta = new float[2];
  52. getAccumulatedDeltaNative(timestamp, positionX, positionY, radius, delta);
  53. return new PointF(delta[0], delta[1]);
  54. }
  55. public void addInterestRegion(int numX, int numY, int left, int top, int right, int bottom) {
  56. addInterestRegionNative(numX, numY, left, top, right, bottom);
  57. }
  58. /*********************** NATIVE METHODS *************************************/
  59. private native void initNative(int width, int height, int downsampleFactor);
  60. private native void addFrameNative(byte[] data, long timeStamp);
  61. private native void computeFeaturesNative(boolean cachedOk);
  62. private native void computeFlowNative();
  63. private native void printInfoNative();
  64. private native void getAccumulatedDeltaNative(
  65. long timestamp, float positionX, float positionY, float radius, float[] delta);
  66. private native void addInterestRegionNative(
  67. int numX, int numY, float left, float top, float right, float bottom);
  68. private native float[] getFeaturesNative(boolean onlyReturnCorrespondingFeatures);
  69. private native void resetNative();
  70. }