/ocr/ocrservice/src/com/googlecode/eyesfree/env/Size.java

http://eyes-free.googlecode.com/ · Java · 98 lines · 59 code · 15 blank · 24 comment · 10 complexity · 5c789eb7a8b92931764d460356b9302c 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.env;
  17. import android.hardware.Camera;
  18. import android.text.TextUtils;
  19. /**
  20. * Code stolen shamelessly from Camera.Size. This class is what Camera.Size
  21. * should have been but never was: independent of a Camera object. Unlike
  22. * Camera.Size, this class does not hold an unused reference to a Camera. It is
  23. * also comparable to allow sorting a list of sizes.
  24. *
  25. * @author Sharvil Nanavati
  26. */
  27. public class Size implements Comparable<Size> {
  28. public final int width;
  29. public final int height;
  30. public Size(final int width, final int height) {
  31. this.width = width;
  32. this.height = height;
  33. }
  34. public Size(final Camera.Size s) {
  35. this.width = s.width;
  36. this.height = s.height;
  37. }
  38. public static Size parseFromString(String sizeString) {
  39. if (TextUtils.isEmpty(sizeString)) {
  40. return null;
  41. }
  42. sizeString = sizeString.trim();
  43. // The expected format is "<width>x<height>".
  44. final String[] components = sizeString.split("x");
  45. if (components.length == 2) {
  46. try {
  47. final int width = Integer.parseInt(components[0]);
  48. final int height = Integer.parseInt(components[1]);
  49. return new Size(width, height);
  50. } catch (final NumberFormatException e) {
  51. return null;
  52. }
  53. } else {
  54. return null;
  55. }
  56. }
  57. @Override
  58. public int compareTo(final Size other) {
  59. return width * height - other.width * other.height;
  60. }
  61. @Override
  62. public boolean equals(final Object other) {
  63. if (other == null) {
  64. return false;
  65. }
  66. if (!(other instanceof Size)) {
  67. return false;
  68. }
  69. final Size otherSize = (Size) other;
  70. return (width == otherSize.width && height == otherSize.height);
  71. }
  72. @Override
  73. public int hashCode() {
  74. return width * 32713 + height;
  75. }
  76. @Override
  77. public String toString() {
  78. return dimensionsAsString(width, height);
  79. }
  80. public static final String dimensionsAsString(final int width, final int height) {
  81. return width + "x" + height;
  82. }
  83. }