-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
11bdadd
commit 670c492
Showing
7 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...maamir/mlkitdemo/ImageClassificationLocalModel/ImageClassificationLocalModelActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.asmaamir.mlkitdemo.ImageClassificationLocalModel; | ||
|
||
import android.content.Intent; | ||
import android.content.pm.PackageManager; | ||
import android.graphics.Bitmap; | ||
import android.graphics.Canvas; | ||
import android.graphics.Paint; | ||
import android.os.Bundle; | ||
import android.widget.ImageButton; | ||
import android.widget.ImageView; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
import androidx.core.app.ActivityCompat; | ||
import androidx.core.content.ContextCompat; | ||
|
||
import com.asmaamir.mlkitdemo.R; | ||
import com.google.firebase.ml.vision.common.FirebaseVisionImage; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
public class ImageClassificationLocalModelActivity extends AppCompatActivity { | ||
private static final String TAG = "PickActivity"; | ||
public static final int REQUEST_CODE_PERMISSION = 111; | ||
public static final String[] REQUIRED_PERMISSIONS = new String[]{"android.permission.WRITE_EXTERNAL_STORAGE", | ||
"android.permission.READ_EXTERNAL_STORAGE"}; | ||
private static final int PICK_IMAGE_CODE = 100; | ||
private ImageView imageView; | ||
private ImageView imageViewCanvas; | ||
private FirebaseVisionImage image; | ||
private Bitmap bitmap; | ||
private Canvas canvas; | ||
private Paint dotPaint, linePaint; | ||
private TextView textView; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_image_classification_local_model); | ||
if (allPermissionsGranted()) { | ||
initViews(); | ||
} else { | ||
ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSION); | ||
} | ||
} | ||
|
||
private void initViews() { | ||
imageView = findViewById(R.id.img_view_pick_local); | ||
ImageButton imageButton = findViewById(R.id.img_btn_pick_local); | ||
imageViewCanvas = findViewById(R.id.img_view_pick_canvas_local); | ||
textView = findViewById(R.id.tv_props_local); | ||
imageButton.setOnClickListener(v -> pickImage()); | ||
} | ||
|
||
private void pickImage() { | ||
Intent intent = new Intent(Intent.ACTION_PICK); | ||
intent.setType("image/*"); | ||
startActivityForResult(intent, PICK_IMAGE_CODE); | ||
} | ||
|
||
@Override | ||
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { | ||
super.onActivityResult(requestCode, resultCode, data); | ||
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE_CODE) { | ||
if (data != null) { | ||
imageView.setImageURI(data.getData()); | ||
textView.setText("Classes: "); | ||
try { | ||
image = FirebaseVisionImage.fromFilePath(this, Objects.requireNonNull(data.getData())); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | ||
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | ||
if (requestCode == REQUEST_CODE_PERMISSION) { | ||
if (allPermissionsGranted()) { | ||
initViews(); | ||
} else { | ||
Toast.makeText(this, | ||
"Permissions not granted by the user.", | ||
Toast.LENGTH_SHORT).show(); | ||
finish(); | ||
} | ||
} | ||
} | ||
|
||
private boolean allPermissionsGranted() { | ||
for (String permission : REQUIRED_PERMISSIONS) { | ||
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
app/src/main/res/layout/activity_image_classification_local_model.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".ImageClassificationLocalModel.ImageClassificationLocalModelActivity"> | ||
|
||
<ImageView | ||
android:id="@+id/img_view_pick_local" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" /> | ||
|
||
<ImageView | ||
android:id="@+id/img_view_pick_canvas_local" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" /> | ||
|
||
<ImageButton | ||
android:id="@+id/img_btn_pick_local" | ||
android:layout_width="72dp" | ||
android:layout_height="72dp" | ||
android:layout_alignParentBottom="true" | ||
android:src="@drawable/camera" /> | ||
|
||
<TextView | ||
android:id="@+id/tv_props_local" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="Classes: " /> | ||
</RelativeLayout> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters