Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.hc.apps.colormatch;
- import androidx.appcompat.app.AppCompatActivity;
- import in.mayanknagwanshi.imagepicker.ImageSelectActivity;
- import android.app.Activity;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.ImageView;
- import android.widget.TextView;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.IOException;
- import okhttp3.Call;
- import okhttp3.Callback;
- import okhttp3.OkHttpClient;
- import okhttp3.Request;
- import okhttp3.Response;
- import okhttp3.RequestBody;
- import okhttp3.MediaType;
- public class ColorsActivity extends AppCompatActivity {
- private TextView mTextViewResult;
- private String colorDetection;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_colors);
- Intent intent = new Intent(this, ImageSelectActivity.class);
- intent.putExtra(ImageSelectActivity.FLAG_COMPRESS, false);//default is true
- intent.putExtra(ImageSelectActivity.FLAG_CAMERA, true);//default is true
- intent.putExtra(ImageSelectActivity.FLAG_GALLERY, false);//default is true
- startActivityForResult(intent, 1213);
- mTextViewResult = findViewById(R.id.text_view_result);
- OkHttpClient client = new OkHttpClient();
- MediaType JSON = MediaType.parse("application/json; charset=utf-8");
- RequestBody body = RequestBody.create(JSON, "{\"requests\":[{" +
- "\"features\": [ "+
- "{\"maxResults\": 1, \"type\": \"IMAGE_PROPERTIES\" }" +
- "], "+
- "\"image\": " +
- "{ \"source\": { " +
- "\"imageUri\": \"gs://cloud-samples-data/vision/image_properties/bali.jpeg\" " +
- "} }}]}"
- );
- String url = "https://vision.googleapis.com/v1/images:annotate?key=AIzaSyC1nVIx-O_CULBZGXYp9DLkQQBDAoscErU";
- Request request = new Request.Builder()
- .url(url)
- .post(body)
- .build();
- client.newCall(request).enqueue(new Callback() {
- @Override
- public void onFailure(Call call, IOException e) {
- Log.e("HttpService", "onFailure() Request was: " + call);
- e.printStackTrace();
- }
- @Override
- public void onResponse(Call call, Response response) throws IOException {
- if (response.isSuccessful()) {
- final String myResponse = response.body().string();
- ColorsActivity.this.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- //mTextViewResult.setText(myResponse);
- try {
- JSONObject myObject = new JSONObject(myResponse);
- JSONArray firstData_birthday = myObject.getJSONArray("responses");
- JSONObject first = firstData_birthday.getJSONObject(0);
- JSONObject second = first.getJSONObject("imagePropertiesAnnotation");
- JSONObject third = second.getJSONObject("dominantColors");
- JSONArray forth = third.getJSONArray("colors");
- JSONObject fifth = forth.getJSONObject(0);
- JSONObject sixth = fifth.getJSONObject("color");
- Integer r = Integer.parseInt(sixth.getString("red"));
- Integer g = Integer.parseInt(sixth.getString("green"));
- Integer b = Integer.parseInt(sixth.getString("blue"));
- String hex = String.format("#%02x%02x%02x", r, g, b);
- colorDetection = hex;
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
- });
- OkHttpClient client2 = new OkHttpClient();
- String url2 = "https://us-central1-bustling-opus-259312.cloudfunctions.net/hackathon-color?message=" + colorDetection;
- Request request2 = new Request.Builder()
- .url(url2)
- .build();
- client.newCall(request2).enqueue(new Callback() {
- @Override
- public void onFailure(Call call, IOException e) {
- e.printStackTrace();
- }
- @Override
- public void onResponse(Call call, Response response) throws IOException {
- if (response.isSuccessful()) {
- final String colorFinal = response.body().string();
- ColorsActivity.this.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- mTextViewResult.setText(colorFinal);
- }
- });
- }
- }
- });
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == 1213 && resultCode == Activity.RESULT_OK) {
- String filePath = data.getStringExtra(ImageSelectActivity.RESULT_FILE_PATH);
- Bitmap selectedImage = BitmapFactory.decodeFile(filePath);
- ImageView img= findViewById(R.id.image);
- img.setImageBitmap(selectedImage);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement