苦苦挣扎与挑战裁剪图像

时间:2015-09-21 11:37:13

标签: android image crop

我试图让用户:

  1. 点击ImageView
  2. 选择图片
  3. 裁剪图像
  4. 最后在ImageView中显示裁剪后的图像
  5. 到目前为止它有点有用,但我有一些问题:

    • 选择图像后,用户会被问到他想要用于裁剪的程序。我不想要
    • 裁剪意图将区域显示为圆圈。我宁愿有一个正方形!

    感谢任何帮助,这里有完整的课程:

    public class settings extends Activity {
    ImageView masterUserImg;
    private static int LOAD_IMAGE_RESULTS = 1;
    final int PIC_CROP = 1;
    

    @覆盖

    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        //Remove title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.settings);
    
        masterUserImg=(ImageView)findViewById(R.id.masterUserImage);
    
    
        masterUserImg.setOnClickListener(new View.OnClickListener() {
    
            @Override
    
            public void onClick(View v) {
    
                // Create the Intent for Image Gallery.
                Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    
                // Start new activity with the LOAD_IMAGE_RESULTS to handle back the results when image is picked from the Image Gallery.
                startActivityForResult(i, LOAD_IMAGE_RESULTS);
    
            }
    
        });
    
    }
    
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        Uri pickedImage = null;
    
        // 1) PICK IMAGE
        if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
            pickedImage = data.getData();
        }
    
        // 2) CROP IMAGE
        performCrop(pickedImage);
    
        // 3) LOAD IMAGE
        if (requestCode == PIC_CROP) {
            if (data != null) {
                // get the returned data
                Bundle extras = data.getExtras();
                // get the cropped bitmap
                Bitmap selectedBitmap = extras.getParcelable("data");
    
                masterUserImg.setImageBitmap(selectedBitmap);
            }
        }
    }
    
    private void performCrop(Uri picUri) {
        try {
    
            Intent cropIntent = new Intent("com.android.camera.action.CROP");
            // indicate image type and Uri
            cropIntent.setDataAndType(picUri, "image/*");
            // set crop properties
            cropIntent.putExtra("crop", "true");
            // indicate aspect of desired crop
            cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);
            // indicate output X and Y
            cropIntent.putExtra("outputX", 128);
            cropIntent.putExtra("outputY", 128);
            // retrieve data on return
            cropIntent.putExtra("return-data", true);
            // start the activity - we handle returning in onActivityResult
            startActivityForResult(cropIntent, PIC_CROP);
        }
        catch (Exception ex) {
            // display an error message
            String errorMessage = "Crop failed";
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }
    

    }

2 个答案:

答案 0 :(得分:0)

请试试Cropper Library。它将满足您的要求。

它支持您需要的功能。

答案 1 :(得分:0)

对于没有提示裁剪的活动,您可以尝试CROP意图的查询活动并使用其中一个。有关特定意图的查询活动,请参阅document

对于裁剪区域的圆形形状,它是处理作物意图的活动的实现,如果您不知道如何配置它,则无法改变它使用正确的额外参数。

但正如@CommonsWare所说,没有com.android.camera.action.CROP意图,手机没有活动来处理它。

所以你应该使用裁剪库来处理它,比如android-crop。 最后,根据您的要求,您可以尝试droid-image-picker,它可以让您从图库和相机中选择图像,还可以为裁剪部分集成Android裁剪。

披露:我是机器人图像选择器的开发者

相关问题