как получить uri обрезанного изображения, выбранного из галереи, с помощью звукового облака в студии android - PullRequest
0 голосов
/ 26 мая 2020
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);
}

Этот код предназначен для выбора изображения из галереи, обрезки его и последующей отправки кадрированного изображения в действие классификатора изображений.

1 Ответ

0 голосов
/ 29 мая 2020

Мне пришлось установить кнопку, которая выбирает изображение, как показано в коде с моделью, инициализированной как 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);
            }
        });

, тогда onActivity должен выглядеть так, как показано ниже

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);
        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...