if(requestCode == GET_FROM_GALLERY && resultCode == RESULT_OK) {
try {
Uri source_uri = imageUri;
Uri dest_uri = Uri.fromFile(new File(getCacheDir(), "cropped"));
// need to crop it to square image as CNN's always required square input
Crop.of(source_uri, dest_uri).asSquare().start(MainActivity.this);
} catch (Exception e) {
e.printStackTrace();
}
}
// if cropping acitivty is finished,
// get the resulting cropped image uri and send it
// to 'Classify' activity
else if(requestCode == Crop.REQUEST_CROP && resultCode == RESULT_OK) {
imageUri = Crop.getOutput(data);
Intent i = new Intent(MainActivity.this, Classify.class);
// put image data in extras to send
i.putExtra("resID_uri", imageUri);
// put filename in extras
i.putExtra("chosen", chosen);
// put model type in extras
i.putExtra("quant", quant);
// send other required data
startActivity(i);
}
This code is meant to select an image from gallery crop it and then send the cropped image output to the image classifier activity.
I had to set the button that picks the image as shown in the code with the model initialized as model= "model.tflite"
pick_image = findViewById(R.id.pick_picture);
pick_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// filename in assets
model = "model.tflite";
Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, GET_FROM_GALLERY);
}
});
then the onActivity has to look like the one below
if(requestCode == REQUEST_IMAGE && resultCode == RESULT_OK) {
try {
Uri source_uri = imageUri;
Uri dest_uri = Uri.fromFile(new File(getCacheDir(), "cropped"));
// need to crop it to square image as CNN's always required square input
Crop.of(source_uri, dest_uri).asSquare().start(MainActivity.this);
} catch (Exception e) {
e.printStackTrace();
}
}
// if cropping acitivty is finished, get the resulting cropped image uri and send it to 'Classify' activity
else if(requestCode == Crop.REQUEST_CROP && resultCode == RESULT_OK){
imageUri = Crop.getOutput(data);
Intent i = new Intent(MainActivity.this, Classify.class);
// put image data in extras to send
i.putExtra("resID_uri", imageUri);
// put filename in extras
i.putExtra("chosen", chosen);
// send other required data
startActivity(i);
}
if (requestCode == GET_FROM_GALLERY && resultCode == RESULT_OK){
// beginCrop(data.getData());
try{
Uri source =data.getData();
Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
Crop.of(source, destination).asSquare().start(this);
}catch (Exception e){
e.printStackTrace();
}
}else if (requestCode == Crop.REQUEST_CROP && resultCode ==RESULT_OK){
// handleCrop(resultCode, data);
Uri gon = Crop.getOutput(data);
Intent i = new Intent(MainActivity.this, Classify.class);
// put image data in extras to send
i.putExtra("resID_uri", gon);
// put filename in extras
i.putExtra("chosen", chosen);
// send other required data
startActivity(i);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.