让用户裁剪图像

时间:2015-08-13 06:09:40

标签: android bitmap crop

我目前拥有用户从图库中拍摄图像的位置。然后将其转换为位图并显示在imageview中并发送到我的服务器。我目前裁剪的方式是吼叫。但是,我希望用户有一个可以移动的300x300盒子,当他们按下上传时,它会上传300x300视图内的内容

account_name debit_balance credit_balance
abc            100          50
pqr            80           100
xyz             150          90

这是我的第一个申请,所以我仍在赚钱。感谢您提前花时间和耐心。

1 个答案:

答案 0 :(得分:0)

这是一个裁剪所需大小图像的示例函数。请检查它。您可以从onActivityResult()调用此'performCrop()'函数。

public void performCrop() {

        try {
            // call the standard crop action intent (the user device may not
            // support it)
            Intent cropIntent = new Intent("com.android.camera.action.CROP");
            // indicate image type and Uri
            cropIntent.setDataAndType(selectedImageUri, "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", 300); //your desired size here 
            cropIntent.putExtra("outputY", 300); //your desired size here
            // retrieve data on return
            cropIntent.putExtra("return-data", true);
            File f = createNewFile("CROP_");
            try {
                f.createNewFile();
            } catch (IOException ex) {

            } catch (NullPointerException ex) {

            }

            CropImagedUri = Uri.fromFile(f);
            cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, CropImagedUri);

            // start the activity - we handle returning in onActivityResult
            startActivityForResult(cropIntent, 2);
        } catch (ActivityNotFoundException anfe) {
            // display an error message
            String errorMessage = "Whoops - your device doesn't support the crop action!";
            Toast toast = Toast
                    .makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                System.out.println("Image Path : " + selectedImagePath);
                PerformCrop();
                // Img.setImageURI(selectedImageUri);
            } else if (requestCode == 2) {
                CropImagePath = getRealPathFromURI(CropImagedUri);
            }
}