简体   繁体   English

如何在 android 工作室中使用声音云从图库中选择裁剪图像的 uri

[英]how to get the uri of a cropped image selected from gallery using sound cloud in android studio

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.此代码用于 select 从图库中裁剪图像,然后将裁剪后的图像 output 发送到图像分类器活动。

I had to set the button that picks the image as shown in the code with the model initialized as model= "model.tflite"我必须设置选择图像的按钮,如代码所示,model 初始化为 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那么 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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM