Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MainActivity.java
- =========================
- package com.example.zeevm.mypersonalassistance;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.net.Uri;
- import android.os.Environment;
- import android.provider.MediaStore;
- import android.speech.RecognizerIntent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.io.File;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- //identify our system service
- final int AUDIO_RESULT=100;
- final int IMAGE_RESULT=101;
- TextView txtRes;
- ImageView btnMic;
- ImageView imgpic;
- ImageView btnCam;
- Context context;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- }
- private void setPointer()
- {
- this.context=this;
- txtRes=(TextView)findViewById(R.id.txtMic);
- btnMic=(ImageView)findViewById(R.id.btnMic);
- btnMic.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- sendAudio();
- }
- });
- imgpic=(ImageView)findViewById(R.id.imgPic);
- btnCam.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- getPic();
- }
- });
- }
- private void getPic()
- {
- //start intent of camera
- Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
- //create a new file for our picture
- File file=new File(Environment.getExternalStorageDirectory()+File.separator+"image.jpg");
- //set a pointer to our file, so we will get in onActivityResult, this file with the image, and not a thumbNail
- intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
- //start activity for result
- startActivityForResult(intent,IMAGE_RESULT);
- }
- private void sendAudio()
- {
- //we are going to existing activity from the android os itself.
- Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
- intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
- RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
- intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Im ready my master..");
- startActivityForResult(intent,AUDIO_RESULT);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (resultCode==RESULT_OK) {
- //handle audio result
- if (requestCode == AUDIO_RESULT) {
- String strRes = "";
- Bundle bundle = data.getExtras();
- final ArrayList<String> matches = bundle.getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
- for (int counter = 0; counter < matches.size(); counter += 1) {
- strRes += matches.get(counter) + "\n";
- }
- txtRes.setText(strRes);
- }
- //handle image result
- if (requestCode == IMAGE_RESULT)
- {
- Bitmap bitmap = null;
- Bitmap thumb = null;
- try
- {
- //get our saved file into a bitmap object.
- File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
- //set size of the picture
- bitmap = decodeSampleBitmapFromFile(file.getAbsolutePath(),4096,2304);
- thumb = decodeSampleBitmapFromFile(file.getAbsolutePath(),160,120);
- imgpic.setImageBitmap(bitmap);
- }
- catch (Exception e)
- {
- Toast.makeText(context, "Error in getting image....", Toast.LENGTH_SHORT).show();
- }
- }
- }
- }
- //method to decode an image file (made by zeev)
- public static Bitmap decodeSampleBitmapFromFile (String path, int reqWidth, int reqHeight)
- {
- //first we decode with inJustDecideBounds=true to check dimensions
- final BitmapFactory.Options options = new BitmapFactory.Options();
- options.inJustDecodeBounds=true;
- BitmapFactory.decodeFile(path,options);
- //calculate inSampleSize, raw height and width of image
- final int height = options.outHeight;
- final int width = options.outWidth;
- options.inPreferredConfig = Bitmap.Config.RGB_565;
- int inSampleSize = 1;
- if (height>reqHeight)
- {
- inSampleSize=Math.round((float) height / (float)reqHeight);
- }
- int expectedWidth = width/inSampleSize;
- if (expectedWidth > reqWidth)
- {
- inSampleSize = Math.round((float)width/(float)reqWidth);
- }
- options.inSampleSize=inSampleSize;
- //decode bitmap with inSampleSize set
- options.inJustDecodeBounds=false;
- //return our updated picture
- return BitmapFactory.decodeFile(path,options);
- }
- }
- activity_main.xml
- ======================
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#fff"
- android:orientation="vertical">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="150dp"
- android:orientation="horizontal">
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:src="@drawable/mic"
- android:id="@+id/btnMic"
- android:layout_weight="1"/>
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/camera"
- android:layout_weight="1"
- android:id="@+id/btnCam"/>
- </LinearLayout>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="100dp"
- android:text="\nready to get your command my master....."
- android:id="@+id/txtMic"/>
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/imgPic"/>
- </LinearLayout>
- Manifest (add this permissions)
- =============================
- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="android.permission.RECORD_AUDIO"/>
- <uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT"/>
- <uses-permission android:name="android.permission.CAMERA"/>
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement