Advertisement
zeev

example of camera and audio intents

Nov 17th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.17 KB | None | 0 0
  1. MainActivity.java
  2. =========================
  3. package com.example.zeevm.mypersonalassistance;
  4.  
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.graphics.Bitmap;
  8. import android.graphics.BitmapFactory;
  9. import android.net.Uri;
  10. import android.os.Environment;
  11. import android.provider.MediaStore;
  12. import android.speech.RecognizerIntent;
  13. import android.support.v7.app.AppCompatActivity;
  14. import android.os.Bundle;
  15. import android.view.View;
  16. import android.widget.ImageView;
  17. import android.widget.TextView;
  18. import android.widget.Toast;
  19.  
  20. import java.io.File;
  21. import java.util.ArrayList;
  22.  
  23. public class MainActivity extends AppCompatActivity {
  24.  
  25.     //identify our system service
  26.     final int AUDIO_RESULT=100;
  27.     final int IMAGE_RESULT=101;
  28.  
  29.     TextView txtRes;
  30.     ImageView btnMic;
  31.     ImageView imgpic;
  32.     ImageView btnCam;
  33.  
  34.     Context context;
  35.     @Override
  36.     protected void onCreate(Bundle savedInstanceState) {
  37.         super.onCreate(savedInstanceState);
  38.         setContentView(R.layout.activity_main);
  39.         setPointer();
  40.     }
  41.  
  42.     private void setPointer()
  43.     {
  44.         this.context=this;
  45.         txtRes=(TextView)findViewById(R.id.txtMic);
  46.         btnMic=(ImageView)findViewById(R.id.btnMic);
  47.         btnMic.setOnClickListener(new View.OnClickListener() {
  48.             @Override
  49.             public void onClick(View view) {
  50.                 sendAudio();
  51.             }
  52.         });
  53.         imgpic=(ImageView)findViewById(R.id.imgPic);
  54.         btnCam.setOnClickListener(new View.OnClickListener() {
  55.             @Override
  56.             public void onClick(View view) {
  57.                 getPic();
  58.             }
  59.         });
  60.     }
  61.  
  62.     private void getPic()
  63.     {
  64.         //start intent of camera
  65.         Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
  66.         //create a new file for our picture
  67.         File file=new File(Environment.getExternalStorageDirectory()+File.separator+"image.jpg");
  68.         //set a pointer to our file, so we will get in onActivityResult, this file with the image, and not a thumbNail
  69.         intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
  70.         //start activity for result
  71.         startActivityForResult(intent,IMAGE_RESULT);
  72.     }
  73.  
  74.  
  75.     private void sendAudio()
  76.     {
  77.         //we are going to existing activity from the android os itself.
  78.         Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  79.         intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
  80.                 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
  81.         intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Im ready my master..");
  82.         startActivityForResult(intent,AUDIO_RESULT);
  83.     }
  84.  
  85.     @Override
  86.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  87.         if (resultCode==RESULT_OK) {
  88.             //handle audio result
  89.             if (requestCode == AUDIO_RESULT) {
  90.                 String strRes = "";
  91.                 Bundle bundle = data.getExtras();
  92.                 final ArrayList<String> matches = bundle.getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
  93.                 for (int counter = 0; counter < matches.size(); counter += 1) {
  94.                     strRes += matches.get(counter) + "\n";
  95.                 }
  96.                 txtRes.setText(strRes);
  97.             }
  98.             //handle image result
  99.             if (requestCode == IMAGE_RESULT)
  100.             {
  101.                 Bitmap bitmap = null;
  102.                 Bitmap thumb = null;
  103.                 try
  104.                 {
  105.                     //get our saved file into a bitmap object.
  106.                     File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
  107.                     //set size of the picture
  108.                     bitmap = decodeSampleBitmapFromFile(file.getAbsolutePath(),4096,2304);
  109.                     thumb = decodeSampleBitmapFromFile(file.getAbsolutePath(),160,120);
  110.                     imgpic.setImageBitmap(bitmap);
  111.                 }
  112.                 catch (Exception e)
  113.                 {
  114.                     Toast.makeText(context, "Error in getting image....", Toast.LENGTH_SHORT).show();
  115.                 }
  116.             }
  117.         }
  118.     }
  119.  
  120.     //method to decode an image file (made by zeev)
  121.     public static Bitmap decodeSampleBitmapFromFile (String path, int reqWidth, int reqHeight)
  122.     {
  123.         //first we decode with inJustDecideBounds=true to check dimensions
  124.         final BitmapFactory.Options options = new BitmapFactory.Options();
  125.         options.inJustDecodeBounds=true;
  126.         BitmapFactory.decodeFile(path,options);
  127.  
  128.         //calculate inSampleSize, raw height and width of image
  129.         final int height = options.outHeight;
  130.         final int width = options.outWidth;
  131.         options.inPreferredConfig = Bitmap.Config.RGB_565;
  132.         int inSampleSize = 1;
  133.  
  134.         if (height>reqHeight)
  135.         {
  136.             inSampleSize=Math.round((float) height / (float)reqHeight);
  137.         }
  138.  
  139.         int expectedWidth = width/inSampleSize;
  140.  
  141.         if (expectedWidth > reqWidth)
  142.         {
  143.             inSampleSize = Math.round((float)width/(float)reqWidth);
  144.         }
  145.  
  146.         options.inSampleSize=inSampleSize;
  147.  
  148.         //decode bitmap with inSampleSize set
  149.         options.inJustDecodeBounds=false;
  150.  
  151.         //return our updated picture
  152.         return BitmapFactory.decodeFile(path,options);
  153.     }
  154. }
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163. activity_main.xml
  164. ======================
  165. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  166.     android:layout_width="match_parent"
  167.     android:layout_height="match_parent"
  168.     android:background="#fff"
  169.     android:orientation="vertical">
  170.  
  171.     <LinearLayout
  172.         android:layout_width="match_parent"
  173.         android:layout_height="150dp"
  174.         android:orientation="horizontal">
  175.         <ImageView
  176.             android:layout_width="wrap_content"
  177.             android:layout_height="wrap_content"
  178.             android:layout_gravity="center"
  179.             android:src="@drawable/mic"
  180.             android:id="@+id/btnMic"
  181.             android:layout_weight="1"/>
  182.         <ImageView
  183.             android:layout_width="wrap_content"
  184.             android:layout_height="wrap_content"
  185.             android:src="@drawable/camera"
  186.             android:layout_weight="1"
  187.             android:id="@+id/btnCam"/>
  188.     </LinearLayout>
  189.  
  190.     <TextView
  191.         android:layout_width="match_parent"
  192.         android:layout_height="100dp"
  193.         android:text="\nready to get your command my master....."
  194.         android:id="@+id/txtMic"/>
  195.  
  196.     <ImageView
  197.         android:layout_width="match_parent"
  198.         android:layout_height="match_parent"
  199.         android:id="@+id/imgPic"/>
  200. </LinearLayout>
  201.  
  202.  
  203. Manifest (add this permissions)
  204. =============================
  205.   <uses-permission android:name="android.permission.INTERNET"/>
  206.     <uses-permission android:name="android.permission.RECORD_AUDIO"/>
  207.     <uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT"/>
  208.     <uses-permission android:name="android.permission.CAMERA"/>
  209.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  210.     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement