/ocr/ocrservice/src/com/googlecode/eyesfree/ocr/client/OcrResult.java

http://eyes-free.googlecode.com/ · Java · 158 lines · 70 code · 24 blank · 64 comment · 5 complexity · a4e335d193c63bb25393d068a80aca6e 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.ocr.client;
  17. import android.graphics.Rect;
  18. import android.os.Parcel;
  19. import android.os.Parcelable;
  20. /**
  21. * This class represents the result of text recognition. It includes the text
  22. * itself as well as the word confidence values.
  23. *
  24. * @author alanv@google.com (Alan Viverette)
  25. */
  26. public class OcrResult implements Parcelable {
  27. /** The text area boundary. */
  28. private final Rect mBounds;
  29. /** The character contents of this text area. */
  30. private final String mString;
  31. /** The word confidences of this text area. */
  32. private final int[] mConfidences;
  33. private final float mAngle;
  34. /**
  35. * The average word confidence of this text area. Initially -1 and lazily
  36. * generated when getAverageConfidence() is called.
  37. */
  38. private float mAvgConfidence;
  39. /**
  40. * Creates an OCR result.
  41. *
  42. * @param result The result of text recognition.
  43. * @param confidences The array of word confidences.
  44. */
  45. public OcrResult(Rect bounds, String result, int[] confidences, float angle) {
  46. mBounds = bounds;
  47. mString = result;
  48. mConfidences = confidences;
  49. mAngle = angle;
  50. mAvgConfidence = -1;
  51. }
  52. /**
  53. * Returns the bounding box containing the text result as a Rect.
  54. *
  55. * @return The bounding box containing the text result.
  56. */
  57. public Rect getBounds() {
  58. return mBounds;
  59. }
  60. /**
  61. * Returns the result of text recognition as a String.
  62. *
  63. * @return The result of text recognition.
  64. */
  65. public String getString() {
  66. return mString;
  67. }
  68. /**
  69. * Returns the array of word confidences. Each entry corresponds to a single
  70. * space-delimited word in the result string.
  71. *
  72. * @return The array of word confidences.
  73. */
  74. public int[] getConfidences() {
  75. return mConfidences;
  76. }
  77. /**
  78. * Returns the angle of this text. Rotation is relative to the center of the
  79. * source image.
  80. *
  81. * @return The source image center-relative angle of this text.
  82. */
  83. public float getAngle() {
  84. return mAngle;
  85. }
  86. /**
  87. * Returns the average confidence of the words in this result.
  88. *
  89. * @return The average confidence of the words in this result.
  90. */
  91. public float getAverageConfidence() {
  92. if (mAvgConfidence >= 0) {
  93. return mAvgConfidence;
  94. } else if (mConfidences == null) {
  95. return 0.0f;
  96. }
  97. mAvgConfidence = 0.0f;
  98. for (int i = 0; i < mConfidences.length; i++) {
  99. mAvgConfidence += mConfidences[i];
  100. }
  101. mAvgConfidence /= mConfidences.length;
  102. return mAvgConfidence;
  103. }
  104. /************************
  105. * Parcelable functions *
  106. ************************/
  107. private OcrResult(Parcel src) {
  108. mString = src.readString();
  109. mConfidences = src.createIntArray();
  110. mBounds = src.readParcelable(null);
  111. mAngle = src.readFloat();
  112. }
  113. @Override
  114. public int describeContents() {
  115. return 0;
  116. }
  117. @Override
  118. public void writeToParcel(Parcel dest, int flags) {
  119. dest.writeString(mString);
  120. dest.writeIntArray(mConfidences);
  121. dest.writeParcelable(mBounds, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
  122. dest.writeFloat(mAngle);
  123. }
  124. public static final Parcelable.Creator<OcrResult> CREATOR = new Parcelable.Creator<OcrResult>() {
  125. @Override
  126. public OcrResult createFromParcel(Parcel in) {
  127. return new OcrResult(in);
  128. }
  129. @Override
  130. public OcrResult[] newArray(int size) {
  131. return new OcrResult[size];
  132. }
  133. };
  134. }