0

当我不应用裁剪活动时,我有一堆代码可以正常工作,但我想将裁剪应用于所选图像并使用 Rest API 将其发送到服务器

TypedFile typedFile = new TypedFile("multipart/form-data",savedFileDestination);
initiateProgressDialog();

如何设置裁剪的 URI 而不是savedFileDestination将裁剪的图像路径作为文件?

4

3 回答 3

0

使用以下方法进行裁剪:

public static Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {
    Bitmap bm = null;

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    bm = BitmapFactory.decodeFile(path, options);

    return bm;
}



public static int calculateInSampleSize(

        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }

    return inSampleSize;
}
于 2017-07-24T13:39:56.450 回答
0

感谢您的所有回答和评论,我找到了我的解决方案,我必须首先将裁剪的图像存储到文件目录中,因为裁剪的图像存储在缓存中,我们需要文件路径并将文件路径发送到服务器进行存储它.....再次感谢

于 2017-07-25T05:48:15.327 回答
0

点击相机

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File outPutFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "Path of your custom directory);
                if (!outPutFile.exists()) {
                    outPutFile.mkdirs();
                }
                Uri capturedImageUri = Uri.fromFile(File.createTempFile("Your app directory name" + System.currentTimeMillis(), ".jpg", outPutFile));
                Logg.e(getClass().getSimpleName(), "Captured_Pic ===== " + Uri.fromFile(outPutFile));
                intent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
                startActivityForResult(intent, Util.REQUEST_CAMERA);

点击图库

CropImage.startPickImageActivity(HomeActivity.this);

活动结果

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_CANCELED) {
        switch (requestCode) {
            case Util.REQUEST_CAMERA: // Camera request
                startCropImageActivity(capturedImageUri);
                break;

            case CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE: // Crop
                CropImage.ActivityResult result = CropImage.getActivityResult(data);
                try {
                    if (resultCode == RESULT_OK) {
                        resultUri = result.getUri();
                        mProfileView.setImageURI(Uri.parse(resultUri.toString())); // this is my imageview, where I'll set that cropped image Uri.
                    } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                        Exception error = result.getError();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            case CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE: // Gallery request
                try {
                    Uri selectedImageUri = CropImage.getPickImageResultUri(this, data);
                    startCropImageActivity(selectedImageUri);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}

此方法将根据您的要求将属性设置为裁剪图像工具

private void startCropImageActivity(Uri imageUri) {
    CropImage.activity(imageUri)
            .setGuidelines(CropImageView.Guidelines.ON)
            .setCropShape(CropImageView.CropShape.RECTANGLE)
            .setActivityMenuIconColor(ContextCompat.getColor(HomeActivity.this, R.color.app_blue))
            .setGuidelinesColor(ContextCompat.getColor(HomeActivity.this, R.color.app_blue))
            .setScaleType(CropImageView.ScaleType.FIT_CENTER)
            .setFixAspectRatio(true)
            .setBorderCornerColor(ContextCompat.getColor(HomeActivity.this, R.color.app_blue))
            .setBorderLineColor(ContextCompat.getColor(HomeActivity.this, R.color.app_blue))
            .start(this);
}
于 2017-07-25T05:48:34.897 回答