Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. package com.hc.apps.colormatch;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import in.mayanknagwanshi.imagepicker.ImageSelectActivity;
  5.  
  6. import android.app.Activity;
  7. import android.content.Intent;
  8. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.widget.ImageView;
  13.  
  14. import android.widget.TextView;
  15.  
  16. import org.json.JSONArray;
  17. import org.json.JSONException;
  18. import org.json.JSONObject;
  19.  
  20. import java.io.IOException;
  21.  
  22. import okhttp3.Call;
  23. import okhttp3.Callback;
  24. import okhttp3.OkHttpClient;
  25. import okhttp3.Request;
  26. import okhttp3.Response;
  27. import okhttp3.RequestBody;
  28. import okhttp3.MediaType;
  29.  
  30.  
  31.  
  32. public class ColorsActivity extends AppCompatActivity {
  33.  
  34. private TextView mTextViewResult;
  35. private String colorDetection;
  36.  
  37. @Override
  38. protected void onCreate(Bundle savedInstanceState) {
  39. super.onCreate(savedInstanceState);
  40. setContentView(R.layout.activity_colors);
  41.  
  42. Intent intent = new Intent(this, ImageSelectActivity.class);
  43. intent.putExtra(ImageSelectActivity.FLAG_COMPRESS, false);//default is true
  44. intent.putExtra(ImageSelectActivity.FLAG_CAMERA, true);//default is true
  45. intent.putExtra(ImageSelectActivity.FLAG_GALLERY, false);//default is true
  46. startActivityForResult(intent, 1213);
  47.  
  48. mTextViewResult = findViewById(R.id.text_view_result);
  49.  
  50. OkHttpClient client = new OkHttpClient();
  51.  
  52. MediaType JSON = MediaType.parse("application/json; charset=utf-8");
  53. RequestBody body = RequestBody.create(JSON, "{\"requests\":[{" +
  54. "\"features\": [ "+
  55. "{\"maxResults\": 1, \"type\": \"IMAGE_PROPERTIES\" }" +
  56. "], "+
  57. "\"image\": " +
  58. "{ \"source\": { " +
  59. "\"imageUri\": \"gs://cloud-samples-data/vision/image_properties/bali.jpeg\" " +
  60. "} }}]}"
  61. );
  62.  
  63. String url = "https://vision.googleapis.com/v1/images:annotate?key=AIzaSyC1nVIx-O_CULBZGXYp9DLkQQBDAoscErU";
  64.  
  65. Request request = new Request.Builder()
  66. .url(url)
  67. .post(body)
  68. .build();
  69.  
  70. client.newCall(request).enqueue(new Callback() {
  71. @Override
  72. public void onFailure(Call call, IOException e) {
  73. Log.e("HttpService", "onFailure() Request was: " + call);
  74.  
  75. e.printStackTrace();
  76. }
  77.  
  78. @Override
  79. public void onResponse(Call call, Response response) throws IOException {
  80. if (response.isSuccessful()) {
  81. final String myResponse = response.body().string();
  82.  
  83. ColorsActivity.this.runOnUiThread(new Runnable() {
  84. @Override
  85. public void run() {
  86. //mTextViewResult.setText(myResponse);
  87. try {
  88. JSONObject myObject = new JSONObject(myResponse);
  89.  
  90. JSONArray firstData_birthday = myObject.getJSONArray("responses");
  91.  
  92. JSONObject first = firstData_birthday.getJSONObject(0);
  93. JSONObject second = first.getJSONObject("imagePropertiesAnnotation");
  94. JSONObject third = second.getJSONObject("dominantColors");
  95.  
  96. JSONArray forth = third.getJSONArray("colors");
  97.  
  98. JSONObject fifth = forth.getJSONObject(0);
  99.  
  100. JSONObject sixth = fifth.getJSONObject("color");
  101.  
  102. Integer r = Integer.parseInt(sixth.getString("red"));
  103. Integer g = Integer.parseInt(sixth.getString("green"));
  104. Integer b = Integer.parseInt(sixth.getString("blue"));
  105.  
  106. String hex = String.format("#%02x%02x%02x", r, g, b);
  107. colorDetection = hex;
  108.  
  109.  
  110. } catch (JSONException e) {
  111. e.printStackTrace();
  112. }
  113.  
  114. }
  115. });
  116. }
  117. }
  118. });
  119.  
  120. OkHttpClient client2 = new OkHttpClient();
  121.  
  122. String url2 = "https://us-central1-bustling-opus-259312.cloudfunctions.net/hackathon-color?message=" + colorDetection;
  123.  
  124. Request request2 = new Request.Builder()
  125. .url(url2)
  126. .build();
  127.  
  128. client.newCall(request2).enqueue(new Callback() {
  129. @Override
  130. public void onFailure(Call call, IOException e) {
  131. e.printStackTrace();
  132. }
  133.  
  134. @Override
  135. public void onResponse(Call call, Response response) throws IOException {
  136. if (response.isSuccessful()) {
  137. final String colorFinal = response.body().string();
  138.  
  139. ColorsActivity.this.runOnUiThread(new Runnable() {
  140. @Override
  141. public void run() {
  142. mTextViewResult.setText(colorFinal);
  143. }
  144. });
  145. }
  146. }
  147. });
  148.  
  149. }
  150.  
  151.  
  152.  
  153.  
  154. @Override
  155. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  156. super.onActivityResult(requestCode, resultCode, data);
  157. if (requestCode == 1213 && resultCode == Activity.RESULT_OK) {
  158. String filePath = data.getStringExtra(ImageSelectActivity.RESULT_FILE_PATH);
  159. Bitmap selectedImage = BitmapFactory.decodeFile(filePath);
  160.  
  161. ImageView img= findViewById(R.id.image);
  162. img.setImageBitmap(selectedImage);
  163. }
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement