Java tutorial
// // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. // // Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services // // Microsoft Cognitive Services (formerly Project Oxford) GitHub: // https://github.com/Microsoft/Cognitive-Vision-Android // // Copyright (c) Microsoft Corporation // All rights reserved. // // MIT License: // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // package com.microsoft.projectoxford.visionsample; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.microsoft.projectoxford.vision.VisionServiceClient; import com.microsoft.projectoxford.vision.VisionServiceRestClient; import com.microsoft.projectoxford.vision.contract.AnalysisInDomainResult; import com.microsoft.projectoxford.vision.contract.AnalysisResult; import com.microsoft.projectoxford.vision.contract.Category; import com.microsoft.projectoxford.vision.contract.Face; import com.microsoft.projectoxford.vision.rest.VisionServiceException; import com.microsoft.projectoxford.visionsample.helper.ImageHelper; import org.json.JSONObject; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; public class AnalyzeInDomainActivity extends ActionBarActivity { // Flag to indicate which task is to be performed. private static final int REQUEST_SELECT_IMAGE = 0; // The button to select an image private Button mButtonSelectImage; // The URI of the image selected to detect. private Uri mImageUri; // The image selected to detect. private Bitmap mBitmap; // The edit to show status and result. private EditText mEditText; private VisionServiceClient client; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_analyze_domain); if (client == null) { client = new VisionServiceRestClient(getString(R.string.subscription_key)); } mButtonSelectImage = (Button) findViewById(R.id.buttonSelectImage); mEditText = (EditText) findViewById(R.id.editTextResult); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_analyze, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public void doAnalyze() { mButtonSelectImage.setEnabled(false); mEditText.setText("Analyzing..."); try { new doRequest().execute(); } catch (Exception e) { mEditText.setText("Error encountered. Exception is: " + e.toString()); } } // Called when the "Select Image" button is clicked. public void selectImage(View view) { mEditText.setText(""); Intent intent; intent = new Intent(AnalyzeInDomainActivity.this, com.microsoft.projectoxford.visionsample.helper.SelectImageActivity.class); startActivityForResult(intent, REQUEST_SELECT_IMAGE); } // Called when image selection is done. @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d("AnalyzeInDomainActivity", "onActivityResult"); switch (requestCode) { case REQUEST_SELECT_IMAGE: if (resultCode == RESULT_OK) { // If image is selected successfully, set the image URI and bitmap. mImageUri = data.getData(); mBitmap = ImageHelper.loadSizeLimitedBitmapFromUri(mImageUri, getContentResolver()); if (mBitmap != null) { // Show the image on screen. ImageView imageView = (ImageView) findViewById(R.id.selectedImage); imageView.setImageBitmap(mBitmap); // Add detection log. Log.d("AnalyzeInDomainActivity", "Image: " + mImageUri + " resized to " + mBitmap.getWidth() + "x" + mBitmap.getHeight()); doAnalyze(); } } break; default: break; } } private String process() throws VisionServiceException, IOException { Gson gson = new Gson(); String model = "celebrities"; // Put the image into an input stream for detection. ByteArrayOutputStream output = new ByteArrayOutputStream(); mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, output); ByteArrayInputStream inputStream = new ByteArrayInputStream(output.toByteArray()); AnalysisInDomainResult v = this.client.analyzeImageInDomain(inputStream, model); String result = gson.toJson(v); Log.d("result", result); return result; } private class doRequest extends AsyncTask<String, String, String> { // Store error message private Exception e = null; public doRequest() { } @Override protected String doInBackground(String... args) { try { return process(); } catch (Exception e) { this.e = e; // Store error } return null; } @Override protected void onPostExecute(String data) { super.onPostExecute(data); // Display based on error existence mEditText.setText(""); if (e != null) { mEditText.setText("Error: " + e.getMessage()); this.e = null; } else { Gson gson = new Gson(); AnalysisInDomainResult result = gson.fromJson(data, AnalysisInDomainResult.class); mEditText.append("Image format: " + result.metadata.format + "\n"); mEditText.append( "Image width: " + result.metadata.width + ", height:" + result.metadata.height + "\n"); mEditText.append("\n"); // Decode the returned result // NOTE: this is different for each domain model JsonArray detectedCelebs = result.result.get("celebrities").getAsJsonArray(); mEditText.append("Celebrities detected: " + detectedCelebs.size() + "\n"); for (JsonElement celebElement : detectedCelebs) { JsonObject celeb = celebElement.getAsJsonObject(); mEditText.append("Name: " + celeb.get("name").getAsString() + ", score: " + celeb.get("confidence").getAsString() + "\n"); } mEditText.append("\n--- Raw Data ---\n\n"); mEditText.append(data); mEditText.setSelection(0); } mButtonSelectImage.setEnabled(true); } } }