package com.example.finalone;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import org.tensorflow.lite.Interpreter;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class MainActivity extends AppCompatActivity {
// Widget Declaration
private static final int SELECT_IMAGE = 1;
private ImageView imageView;
private TextView textViewResult;
private Interpreter tflite;
private final int INPUT_SIZE = 224; // Change if your model expects a different size
private final int PIXEL_SIZE = 3; // RGB = 3 channels
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Wiring
imageView = findViewById(R.id.imageView);
textViewResult = findViewById(R.id.textViewResult);
Button buttonSelect = findViewById(R.id.buttonSelect);
try {
tflite = new Interpreter(loadModelFile());
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Model load failed: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
buttonSelect.setOnClickListener(v -> {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, SELECT_IMAGE);
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_IMAGE && resultCode == RESULT_OK && data != null) {
Uri imageUri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
imageView.setImageBitmap(bitmap);
classifyImage(Bitmap.createScaledBitmap(bitmap, INPUT_SIZE, INPUT_SIZE, false));
} catch (IOException e) {
e.printStackTrace();
}
}
}
private MappedByteBuffer loadModelFile() throws IOException {
AssetFileDescriptor fileDescriptor = getAssets().openFd("model.tflite");
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
@SuppressLint("SetTextI18n")
private void classifyImage(Bitmap bitmap) {
ByteBuffer inputBuffer = ByteBuffer.allocateDirect(4 * INPUT_SIZE * INPUT_SIZE * PIXEL_SIZE);
inputBuffer.order(ByteOrder.nativeOrder());
int[] intValues = new int[INPUT_SIZE * INPUT_SIZE];
bitmap.getPixels(intValues, 0, INPUT_SIZE, 0, 0, INPUT_SIZE, INPUT_SIZE);
for (int i = 0; i < intValues.length; ++i) {
int val = intValues[i];
inputBuffer.putFloat(((val >> 16) & 0xFF) / 255.f);
inputBuffer.putFloat(((val >> 8) & 0xFF) / 255.f);
inputBuffer.putFloat((val & 0xFF) / 255.f);
}
float[][] output = new float[1][4]; // For 4 classes
tflite.run(inputBuffer, output);
int predicted = 0;
for (int i = 1; i < 4; i++) {
if (output[0][i] > output[0][predicted]) {
predicted = i;
}
}
String[] labels = {"Apple", "banana", "beetroot", "carrot"}; // Update to match your model
textViewResult.setText("Prediction: " + labels[predicted]);
}
}
--------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="250dp"
android:layout_height="250dp"
android:scaleType="centerCrop"
android:layout_marginBottom="16dp"
android:src="@drawable/ic_launcher_background" />
<Button
android:id="@+id/buttonSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Image" />
<TextView
android:id="@+id/textViewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prediction: "
android:textSize="18sp"
android:layout_marginTop="24dp" />
</LinearLayout>
--------------------------------------------------------------------------------------------------------------------------------