• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 package org.tensorflow.demo.env;
17 
18 import android.graphics.Bitmap;
19 import android.text.TextUtils;
20 import java.io.Serializable;
21 import java.util.ArrayList;
22 import java.util.List;
23 
24 /**
25  * Size class independent of a Camera object.
26  */
27 public class Size implements Comparable<Size>, Serializable {
28 
29   // 1.4 went out with this UID so we'll need to maintain it to preserve pending queries when
30   // upgrading.
31   public static final long serialVersionUID = 7689808733290872361L;
32 
33   public final int width;
34   public final int height;
35 
Size(final int width, final int height)36   public Size(final int width, final int height) {
37     this.width = width;
38     this.height = height;
39   }
40 
Size(final Bitmap bmp)41   public Size(final Bitmap bmp) {
42     this.width = bmp.getWidth();
43     this.height = bmp.getHeight();
44   }
45 
46   /**
47    * Rotate a size by the given number of degrees.
48    * @param size Size to rotate.
49    * @param rotation Degrees {0, 90, 180, 270} to rotate the size.
50    * @return Rotated size.
51    */
getRotatedSize(final Size size, final int rotation)52   public static Size getRotatedSize(final Size size, final int rotation) {
53     if (rotation % 180 != 0) {
54       // The phone is portrait, therefore the camera is sideways and frame should be rotated.
55       return new Size(size.height, size.width);
56     }
57     return size;
58   }
59 
parseFromString(String sizeString)60   public static Size parseFromString(String sizeString) {
61     if (TextUtils.isEmpty(sizeString)) {
62       return null;
63     }
64 
65     sizeString = sizeString.trim();
66 
67     // The expected format is "<width>x<height>".
68     final String[] components = sizeString.split("x");
69     if (components.length == 2) {
70       try {
71         final int width = Integer.parseInt(components[0]);
72         final int height = Integer.parseInt(components[1]);
73         return new Size(width, height);
74       } catch (final NumberFormatException e) {
75         return null;
76       }
77     } else {
78       return null;
79     }
80   }
81 
sizeStringToList(final String sizes)82   public static List<Size> sizeStringToList(final String sizes) {
83     final List<Size> sizeList = new ArrayList<Size>();
84     if (sizes != null) {
85       final String[] pairs = sizes.split(",");
86       for (final String pair : pairs) {
87         final Size size = Size.parseFromString(pair);
88         if (size != null) {
89           sizeList.add(size);
90         }
91       }
92     }
93     return sizeList;
94   }
95 
sizeListToString(final List<Size> sizes)96   public static String sizeListToString(final List<Size> sizes) {
97     String sizesString = "";
98     if (sizes != null && sizes.size() > 0) {
99       sizesString = sizes.get(0).toString();
100       for (int i = 1; i < sizes.size(); i++) {
101         sizesString += "," + sizes.get(i).toString();
102       }
103     }
104     return sizesString;
105   }
106 
aspectRatio()107   public final float aspectRatio() {
108     return (float) width / (float) height;
109   }
110 
111   @Override
compareTo(final Size other)112   public int compareTo(final Size other) {
113     return width * height - other.width * other.height;
114   }
115 
116   @Override
equals(final Object other)117   public boolean equals(final Object other) {
118     if (other == null) {
119       return false;
120     }
121 
122     if (!(other instanceof Size)) {
123       return false;
124     }
125 
126     final Size otherSize = (Size) other;
127     return (width == otherSize.width && height == otherSize.height);
128   }
129 
130   @Override
hashCode()131   public int hashCode() {
132     return width * 32713 + height;
133   }
134 
135   @Override
toString()136   public String toString() {
137     return dimensionsAsString(width, height);
138   }
139 
dimensionsAsString(final int width, final int height)140   public static final String dimensionsAsString(final int width, final int height) {
141     return width + "x" + height;
142   }
143 }
144