Commit 366a786d authored by Hasaranga Nadeesh's avatar Hasaranga Nadeesh

New File

parent 7fc455e9
package com.example.myapplicationopencv;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Surface;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.JavaCameraView;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
public class MainActivity extends AppCompatActivity {
Button button;
EditText mResultEt;
ImageView mPreviewIv;
private static final int CAMERA_REQUEST_CODE = 200;
private static final int STORAGE_REQUEST_CODE = 400;
private static final int IMAGE_PICK_GALLERY_CODE = 1000;
private static final int IMAGE_PICK_CAMERA_CODE = 1001;
String cameraPermission[];
String storagePermission[];
Uri image_uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mResultEt = findViewById(R.id.resultEt);
mPreviewIv = findViewById(R.id.ImageIv);
cameraPermission = new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE};
storagePermission = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
// addListnerOnButton();
/*if(OpenCVLoader.initDebug()){
Toast.makeText(getApplicationContext(),"Loaded open cv",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),"Could not load open cv",Toast.LENGTH_SHORT).show();
}*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if(id == R.id.addImage){
showImageImportDialog();
}
if(id == R.id.settings){
Toast.makeText(this,"Settings",Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
private void showImageImportDialog() {
String[] items = {" Camera", "Gallery"};
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Select Image");
dialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(which == 0){
if(!checkCameraPermission()){
requestCameraPermission();
}else{
pickCamera();
}
}
if(which == 1){
if(!checkStoragePermission()){
requestStoragePermission();
}
else{
pickGallery();
}
}
}
});
dialog.create().show();
}
private void pickGallery() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
startActivityForResult(intent,IMAGE_PICK_GALLERY_CODE);
}
private void pickCamera() {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "NewPic");
values.put(MediaStore.Images.Media.DESCRIPTION, "Image to Text");
image_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,image_uri);
startActivityForResult(cameraIntent,IMAGE_PICK_CAMERA_CODE);
}
private void requestStoragePermission() {
ActivityCompat.requestPermissions(this,storagePermission,STORAGE_REQUEST_CODE);
}
private boolean checkStoragePermission() {
boolean result = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == (PackageManager.PERMISSION_GRANTED);
return result;
}
private void requestCameraPermission() {
ActivityCompat.requestPermissions(this,cameraPermission,CAMERA_REQUEST_CODE);
}
private boolean checkCameraPermission() {
boolean result = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == (PackageManager.PERMISSION_GRANTED);
boolean result1 = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == (PackageManager.PERMISSION_GRANTED);
return result && result1;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case CAMERA_REQUEST_CODE:
if(grantResults.length > 0 ){
boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean writeStorageAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if(cameraAccepted && writeStorageAccepted){
pickCamera();
}else{
Toast.makeText(this,"Permission denied",Toast.LENGTH_SHORT).show();
}
}
break;
case STORAGE_REQUEST_CODE:
if(grantResults.length > 0 ){
boolean writeStorageAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if(writeStorageAccepted){
pickGallery();
}else{
Toast.makeText(this,"Permission denied",Toast.LENGTH_SHORT).show();
}
}
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(resultCode == RESULT_OK){
if(requestCode == IMAGE_PICK_GALLERY_CODE){
//CropImage.activity();
}
if(requestCode == IMAGE_PICK_CAMERA_CODE){
}
}
}
public void addListnerOnButton(){
final Context context = this;
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0){
Intent intent = new Intent(context,CameraActivity.class);
startActivity(intent);
}
});
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment