Advertisement
Guest User

ClarifaiUtil

a guest
Feb 20th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. package com.example.naris.brynmedicalassistant.ComputerVisionJF;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.ContextWrapper;
  6. import android.content.Intent;
  7. import android.graphics.Bitmap;
  8. import android.graphics.BitmapFactory;
  9. import android.support.annotation.NonNull;
  10. import android.support.annotation.Nullable;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13.  
  14. import java.io.ByteArrayOutputStream;
  15. import java.io.FileNotFoundException;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20.  
  21. public final class ClarifaiUtil {
  22.  
  23. private ClarifaiUtil() {
  24. throw new UnsupportedOperationException("No instances");
  25. }
  26.  
  27. /**
  28. * @param context
  29. * @param data
  30. * @return
  31. */
  32. @Nullable
  33. public static byte[] retrieveSelectedImage(@NonNull Context context, @NonNull Intent data) {
  34. InputStream inStream = null;
  35. Bitmap bitmap = null;
  36. try {
  37. inStream = context.getContentResolver().openInputStream(data.getData());
  38. bitmap = BitmapFactory.decodeStream(inStream);
  39. final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  40. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
  41. return outStream.toByteArray();
  42. } catch (FileNotFoundException e) {
  43. return null;
  44. } finally {
  45. if (inStream != null) {
  46. try {
  47. inStream.close();
  48. } catch (IOException ignored) {
  49. }
  50. }
  51. if (bitmap != null) {
  52. bitmap.recycle();
  53. }
  54. }
  55. }
  56.  
  57. @NonNull
  58. public static Activity unwrapActivity(@NonNull Context startFrom) {
  59. while (startFrom instanceof ContextWrapper) {
  60. if (startFrom instanceof Activity) {
  61. return ((Activity) startFrom);
  62. }
  63. startFrom = ((ContextWrapper) startFrom).getBaseContext();
  64. }
  65. throw new IllegalStateException("This Context can't be unwrapped to an Activity!");
  66. }
  67.  
  68. @Nullable
  69. public static <T> T firstChildOfType(@NonNull View root, @NonNull Class<T> type) {
  70. if (type.isInstance(root)) {
  71. return type.cast(root);
  72. }
  73. if (root instanceof ViewGroup) {
  74. final ViewGroup rootGroup = (ViewGroup) root;
  75. for (int i = 0; i < rootGroup.getChildCount(); i++) {
  76. final View child = rootGroup.getChildAt(i);
  77. final T childResult = firstChildOfType(child, type);
  78. if (childResult != null) {
  79. return childResult;
  80. }
  81. }
  82. }
  83. return null;
  84. }
  85.  
  86. @NonNull
  87. public static <T> List<T> childrenOfType(@NonNull View root, @NonNull Class<T> type) {
  88. final List<T> children = new ArrayList<>();
  89. if (type.isInstance(root)) {
  90. children.add(type.cast(root));
  91. }
  92. if (root instanceof ViewGroup) {
  93. final ViewGroup rootGroup = (ViewGroup) root;
  94. for (int i = 0; i < rootGroup.getChildCount(); i++) {
  95. final View child = rootGroup.getChildAt(i);
  96. children.addAll(childrenOfType(child, type));
  97. }
  98. }
  99. return children;
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement