Skip to content

Commit

Permalink
added camera intent
Browse files Browse the repository at this point in the history
  • Loading branch information
aadityaguptaa committed Aug 7, 2020
1 parent 3eb5d2e commit fe1785e
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 30 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.android.material:material:1.3.0-alpha01'
implementation 'com.google.android.material:material:1.2.0-alpha02'

}
19 changes: 19 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
package="com.example.meaning4">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-feature android:name="android.hardware.camera"
android:required="true" />


<application
android:allowBackup="true"
Expand All @@ -20,6 +26,19 @@
</intent-filter>
</activity>

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.meaning4.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>




<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
Expand Down
131 changes: 107 additions & 24 deletions app/src/main/java/com/example/meaning4/findMeaning.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.example.meaning4;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.common.io.ByteStreams;
Expand All @@ -10,15 +14,25 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
Expand All @@ -44,10 +58,16 @@ public class findMeaning extends AppCompatActivity {
List<String> listOfWords = new ArrayList<>();
public int selectedInt;
public String selectedString;
public Bitmap mSelectedImage;
static final int REQUEST_IMAGE_CAPTURE = 1;
static final int REQUEST_TAKE_PHOTO = 1;
public String currentPhotoPath;



@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_meaning);
Button button = findViewById(R.id.button98);
Expand All @@ -67,6 +87,34 @@ public void onClick(View v) {

}
});

//Request For Camera Permission
if(ContextCompat.checkSelfPermission(findMeaning.this, Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(findMeaning.this, new String[]{
Manifest.permission.CAMERA
}, 100);
}

ImageView cameraClick = findViewById(R.id.floating_action_button);
cameraClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String fileName = "photo";
File storageDirectory = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

try{
File imageFile = File.createTempFile(fileName, ".jpg", storageDirectory);
currentPhotoPath = imageFile.getAbsolutePath();

Uri imageUri = FileProvider.getUriForFile(findMeaning.this, "com.example.meaning4.fileprovider", imageFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, 1);
}catch (IOException e){
e.printStackTrace();
}
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down Expand Up @@ -167,21 +215,44 @@ public void onClick(View v) {
});


AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
ComputerVisionClient compVisClient = ComputerVisionManager.authenticate(subscriptionKey).withEndpoint(endpoint);
Log.i("a", "\nAzure Cognitive Services Computer Vision - Java Quickstart Sample");
RecognizeTextOCRLocal(compVisClient);
return null;
}
};
task.execute();






}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Log.i("tyu", "00");
Log.i("tyu", String.valueOf(requestCode));

if (resultCode == RESULT_OK && requestCode == 1) {
Bitmap bitmap = BitmapFactory.decodeFile(currentPhotoPath);
if (bitmap != null) {
Log.i("tyu", "02");
ImageView im = findViewById(R.id.image98);
mSelectedImage = Bitmap.createScaledBitmap(bitmap, im.getWidth(), im.getHeight(), true);
im.setImageBitmap(mSelectedImage);

AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
ComputerVisionClient compVisClient = ComputerVisionManager.authenticate(subscriptionKey).withEndpoint(endpoint);
Log.i("a", "\nAzure Cognitive Services Computer Vision - Java Quickstart Sample");
RecognizeTextOCRLocal(compVisClient);
return null;
}
};
task.execute();
}
}




}

@Override
Expand Down Expand Up @@ -221,21 +292,8 @@ public void RecognizeTextOCRLocal(ComputerVisionClient client) {

// Replace this string with the path to your own image.
Bitmap icon4 = BitmapFactory.decodeResource(getResources(),R.drawable.camera6);
final ImageView image98 = findViewById(R.id.image98);
View parent = (View)image98.getParent();
int width = image98.getWidth();

Log.i("wittthy", String.valueOf(width) + " "+String.valueOf(image98.getHeight()));
final Bitmap icon = Bitmap.createScaledBitmap(icon4, image98.getWidth(), image98.getHeight(), false);
runOnUiThread(new Runnable() {
Bitmap icon = mSelectedImage;

@Override
public void run() {

image98.setImageBitmap(icon);

}
});


ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Expand Down Expand Up @@ -296,4 +354,29 @@ public int getStatusBarHeight() {
return result;
}


private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);

// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}

private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(currentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}

}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_add_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="48dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="48dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_add_black_48dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="48dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="48dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
</vector>
24 changes: 21 additions & 3 deletions app/src/main/res/layout/activity_find_meaning.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

<ImageView
android:id="@+id/image98"
android:layout_width="1080px"
android:layout_height="1425px"
android:scaleType="centerInside"
android:layout_width="match_parent"
android:layout_height="535dp"
android:scaleType="fitStart"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
Expand Down Expand Up @@ -75,6 +75,24 @@
app:layout_constraintTop_toBottomOf="@+id/image98"
app:layout_constraintVertical_bias="1.0" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floating_action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:layout_marginStart="16dp"
android:layout_marginBottom="4dp"
android:background="#00000000"
app:backgroundTint="#000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/choosenWord"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="@drawable/ic_add_black_48dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

<include layout="@layout/bottom_sheet_persistent" />
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/bottom_sheet_persistent.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
android:background="#FFFFFF"
android:paddingBottom="32dp"
app:behavior_hideable="true"
app:behavior_peekHeight="16dp"
app:behavior_peekHeight="0dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">


Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/file_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="my_image"
path="Android/data/com.example.meaning4/files/Pictures"></external-path>
</paths>
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'



// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down

0 comments on commit fe1785e

Please sign in to comment.